https://kotlinlang.org logo
#compose
Title
# compose
t

Tony Kazanjian

01/20/2021, 10:16 PM
Hello folks. I'm wondering if anyone might have a more elegant solution to handling focus for multiple TextFields when triggering the ImeAction. I basically have a column of TextFields, and it seems like each field in the column scope must have its own
FocusRequester
. But perhaps
FocusOrderModifier
is what this is for...anyone have an example of how to use that? Here's what I have working so far:
Copy code
val firstNameFocus = remember { FocusRequester()}
    val lastNameFocus = remember { FocusRequester() }
    val emailFocus = remember { FocusRequester() }
    val passwordFocus = remember{ FocusRequester() }

    Column(Modifier.padding(16.dp)) {
        onActive {
            firstNameFocus.requestFocus()
        }
        CommonTextField(
            value = firstName,
            onTextChange = onFirstNameChange,
            modifier = Modifier.padding(8.dp).fillMaxWidth().focusRequester(firstNameFocus),
            label = { Text("First Name") },
            onImeAction = {lastNameFocus.requestFocus()}
        )
        CommonTextField(
            value = lastName,
            onTextChange = onLastNameChange,
            modifier = Modifier.padding(8.dp).fillMaxWidth().focusRequester(lastNameFocus),
            label = { Text("Last Name") },
            onImeAction = {emailFocus.requestFocus()}
        )

        CommonTextField(
            value = email,
            onTextChange = onEmailChange,
            modifier = Modifier.padding(8.dp).fillMaxWidth().focusRequester(emailFocus),
            label = { Text("Email") },
            onImeAction = {passwordFocus.requestFocus()}
        )
        PasswordTextField(
            value = password,
            onTextChange = onPasswordChange,
            modifier = Modifier.padding(8.dp).fillMaxWidth().focusRequester(passwordFocus),
            label = { Text("Password") },
            isPasswordVisible = remember { mutableStateOf(true) },
            onImeAction = {viewModel.registerUser(firstName, lastName, email, password)}
        )
m

manueldidonna

01/21/2021, 3:15 AM
There is an api to move focus programmatically, take a look
s

Siyamed

01/21/2021, 6:00 AM
unfortunately it is not there yet, but we are thinking/planning to reduce the code when the ime action is defined to be next previous etc.
👍 1
t

Tony Kazanjian

01/21/2021, 6:05 PM
Awesome. As always I'll be looking forward to the next release 🙂
s

Samir Basnet

03/16/2021, 3:34 PM
@Siyamed Any updates on this?
5 Views