如何打开一个以闪烁光标为焦点的 BasicTextField?
How to open a BasicTextField focused with blinking cursor in it?
我的观点之一是BasicTextField
。我默认显示软键盘,当我开始在键盘上键入字母时,BasicTextField
中没有显示任何内容,因为它没有光标。
为了使我的键盘操作可见,我必须点击 TextField,使光标可见。现在,当我点击键盘时,我会在 BasicTextField
.
中看到结果
如何打开其中有活动闪烁光标的 BasicTextField?
编辑: 对我不起作用
val focusRequester = FocusRequester()
val keyboardController = LocalSoftwareKeyboardController.current
//..
.focusRequester(focusRequester)
.onFocusChanged {
if (it.isFocused) {
keyboardController?.show()
}
}
既没有激活光标也没有让键盘出现。除此之外
DisposableEffect(Unit) {
focusRequester.requestFocus()
onDispose { }
}
导致崩溃:
java.lang.IllegalStateException:
FocusRequester is not initialized. Here are some possible fixes:
1. Remember the FocusRequester: val focusRequester = remember { FocusRequester() }
2. Did you forget to add a Modifier.focusRequester() ?
3. Are you attempting to request focus during composition? Focus requests should be made in
response to some event. Eg Modifier.clickable { focusRequester.requestFocus() }
at androidx.compose.ui.focus.FocusRequester.requestFocus(FocusRequester.kt:54)
我成功了:
val focusRequester = FocusRequester()
//..
.focusRequester(focusRequester)
而不是
DisposableEffect(Unit) {
focusRequester.requestFocus()
onDispose { }
}
我用过
LaunchedEffect(Unit) {
if(isFocused){
focusRequester.requestFocus()
}
}
我的观点之一是BasicTextField
。我默认显示软键盘,当我开始在键盘上键入字母时,BasicTextField
中没有显示任何内容,因为它没有光标。
为了使我的键盘操作可见,我必须点击 TextField,使光标可见。现在,当我点击键盘时,我会在 BasicTextField
.
如何打开其中有活动闪烁光标的 BasicTextField?
编辑:
val focusRequester = FocusRequester()
val keyboardController = LocalSoftwareKeyboardController.current
//..
.focusRequester(focusRequester)
.onFocusChanged {
if (it.isFocused) {
keyboardController?.show()
}
}
既没有激活光标也没有让键盘出现。除此之外
DisposableEffect(Unit) {
focusRequester.requestFocus()
onDispose { }
}
导致崩溃:
java.lang.IllegalStateException: FocusRequester is not initialized. Here are some possible fixes:
1. Remember the FocusRequester: val focusRequester = remember { FocusRequester() } 2. Did you forget to add a Modifier.focusRequester() ? 3. Are you attempting to request focus during composition? Focus requests should be made in response to some event. Eg Modifier.clickable { focusRequester.requestFocus() } at androidx.compose.ui.focus.FocusRequester.requestFocus(FocusRequester.kt:54)
我成功了:
val focusRequester = FocusRequester()
//..
.focusRequester(focusRequester)
而不是
DisposableEffect(Unit) {
focusRequester.requestFocus()
onDispose { }
}
我用过
LaunchedEffect(Unit) {
if(isFocused){
focusRequester.requestFocus()
}
}