当用户导航到可组合文件时,我希望通过自动请求将焦点放在jetpack compose中的文本字段上来弹出键盘。到目前为止,这是我尝试过的,但似乎不起作用
val feedbackContent = remember { mutableStateOf(TextFieldValue()) }
val focusRequester = remember { FocusRequester() }
OutlinedTextField(
modifier = Modifier
.clickable {
focusRequester.requestFocus()
}
.fillMaxWidth()
.focusRequester(focusRequester)
.focusable()
)
您可以使用以下内容
val focusRequester = remember { FocusRequester() }
val keyboardController = LocalSoftwareKeyboardController.current
OutlinedTextField(
value = text,
onValueChange = { text = it},
modifier = Modifier
.fillMaxWidth()
.focusRequester(focusRequester)
.onFocusChanged {
if (it.isFocused) {
keyboardController?.show()
}
}
)
LaunchedEffect(Unit) {
focusRequester.requestFocus()
}