gpt4 book ai didi

android - 如何从 Jetpack Compose TextField 关闭虚拟键盘?

转载 作者:行者123 更新时间:2023-12-03 17:41:37 25 4
gpt4 key购买 nike

我正在使用 Jetpack Compose TextField我想在用户按下操作按钮(imeActionPerformed 参数)时关闭虚拟键盘。

val text = +state { "" }
TextField(
value = text.value,
keyboardType = KeyboardType.Text,
imeAction = ImeAction.Done,
onImeActionPerformed = {
// TODO Close the virtual keyboard here <<<
}
onValueChange = { s -> text.value = s }
)

最佳答案

您可以使用 LocalSoftwareKeyboardController 类来控制当前的软件键盘,然后使用 hide 方法:

var text by remember { mutableStateOf(TextFieldValue("Text")) }
val keyboardController = LocalSoftwareKeyboardController.current

TextField(
value = text,
onValueChange = {
text = it
},
label = { Text("Label") },
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
keyboardActions = KeyboardActions(
onDone = {keyboardController?.hide()})
)
此解决方案关闭键盘 没有 从当前 TextField 中移除焦点.
只是为了突出差异:
val focusManager = LocalFocusManager.current
focusManager.clearFocus()
此代码关闭键盘,从 中移除焦点。文本字段 .

关于android - 如何从 Jetpack Compose TextField 关闭虚拟键盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59133100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com