我正在使用 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()
此代码关闭键盘removeing focus from the
TextField
.
1.0.0-alpha12
开始(并且在撰写1.3.3
中仍然有效)onImeActionPerformed
已被弃用,建议的方法是使用keyboardActions
与keyboardOptions
的组合:
val focusManager = LocalFocusManager.current
OutlinedTextField(
value = ...,
onValueChange = ...,
label = ...,
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
keyboardOptions = KeyboardOptions.Default.copy(imeAction = ImeAction.Done, keyboardType = KeyboardType.Password),
)
focusManager.clearFocus()
将负责关闭软键盘。
在
1.0.0
中,您可以使用SoftwareKeyboardController
或FocusManager
来做到这一点。
本回答着重于他们的区别。
var text by remember { mutableStateOf("")}
TextField(
value = text,
onValueChange = { text = it },
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
keyboardActions = KeyboardActions(onDone = { /* TODO */ }),
)
基于
@Gabriele Mariottis
答案。
val keyboardController = LocalSoftwareKeyboardController.current
// TODO =
keyboardController?.hide()
这只会关闭键盘,但不会NOT清除任何聚焦的文本字段的焦点(注意光标和粗下划线)。
基于
@azizbekians
答案。
val focusManager = LocalFocusManager.current
// TODO =
focusManager.clearFocus()
这将关闭键盘并清除 TextField 的焦点。
添加 Gabriele Mariotti 的解决方案,如果你想有条件地隐藏键盘,比如在单击按钮后,使用这个:
keyboardController?.hide()
例如点击添加按钮后隐藏键盘:
var newWord by remember { mutableStateOf("") }
val keyboardController = LocalSoftwareKeyboardController.current
// Setup the text field with keyboard as provided by Gabriele Mariotti
...
Button(
modifier = Modifier
.height(56.dp),
onClick = {
if (!newWord.trim().isNullOrEmpty()) {
wordViewModel.onAddWord(newWord.trim())
newWord = ""
keyboardController?.hide()
}
...
alpha-12 发布后编辑: 请参阅@azizbekian 回复。
前 alpha-12 响应
我在这里找到了解决方案:)
fun hideKeyboard(activity: Activity) {
val imm: InputMethodManager = activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
var view = activity.currentFocus
if (view == null) {
view = View(activity)
}
imm.hideSoftInputFromWindow(view.windowToken, 0)
}
我只需要从我的组件调用上面的函数:
// getting the context
val context = +ambient(ContextAmbient)
// textfield state
val text = +state { "" }
TextField(
value = text.value,
keyboardType = KeyboardType.Text,
imeAction = ImeAction.Done,
onImeActionPerformed = {
if (imeAction == ImeAction.Done) {
hideKeyboard(context as Activity)
}
}
onValueChange = { s -> text.value = s }
)
val focus = LocalTextInputService.current
var text by remember{ mutableStateOf("")}
TextField(
value = text,
onValueChange = { text = it },
keyboardOptions = KeyboardOptions.Default.copy(imeAction = ImeAction.Done, keyboardType = KeyboardType.Text),
keyboardActions = KeyboardActions(onDone = { focus?.hideSoftwareKeyboard() }),
singleLine = true
)
实现'androidx.compose.material3:material3:1.0.0-alpha02'
在 Ime 操作中隐藏键盘的文本字段
@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun TextFieldWithHideKeyboardOnImeAction() {
val keyboardController = LocalSoftwareKeyboardController.current
var text by rememberSaveable { mutableStateOf("") }
TextField(
value = text,
onValueChange = { text = it },
label = { Text("Label") },
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
keyboardActions = KeyboardActions(
onDone = {
keyboardController?.hide()
// do something here
}
)
)
}