4

I want the keyboard to pop up by an auto requesting focus on a text field in jetpack compose when the user navigates to a composable. As of now, this is what I have tried but it doesn't seem to work

val feedbackContent = remember { mutableStateOf(TextFieldValue()) }
val focusRequester = remember { FocusRequester() }

OutlinedTextField(
                modifier = Modifier
                    .clickable {
                        focusRequester.requestFocus()
                    }
                    .fillMaxWidth()
                    .focusRequester(focusRequester)
                    .focusable()
)
4

1 回答 1

4

你可以使用类似的东西:

val focusRequester = FocusRequester()
val keyboardController = LocalSoftwareKeyboardController.current

OutlinedTextField(
    value = text,
    onValueChange = { text = it},
    modifier = Modifier
        .fillMaxWidth()
        .focusRequester(focusRequester)
        .onFocusChanged {
            if (it.isFocused) {
                keyboardController?.show()
            }
        }
)

DisposableEffect(Unit) {
    focusRequester.requestFocus()
    onDispose { }
}
于 2021-07-06T11:00:04.537 回答