导航到 Jetpack Compose 中的可组合项时如何自动请求焦点到文本字段

How to auto request focus to a text field when navigated to a composable in jetpack compose

我希望当用户导航到可组合项时,通过自动请求将焦点放在 Jetpack Compose 中的文本字段上来弹出键盘。截至目前,这是我尝试过的方法,但似乎不起作用

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

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

您可以使用类似的东西:

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 { }
}