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

How to close the virtual keyboard from a Jetpack Compose TextField?

我正在使用 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 }
)

alpha-12 发布后编辑: 请参阅@azizbekian 回复。

前 alpha-12 响应

我找到了解决方案 here :)

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

可以使用LocalSoftwareKeyboardControllerclass控制当前软键盘,然后使用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()

此代码关闭键盘,从 TextField 移除焦点。

从 compose 1.0.0-alpha12 开始(并且在 compose 1.2.0 中仍然有效)onImeActionPerformed 已弃用,建议的方法是使用 keyboardActions 与 [=15 的组合=]:

    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() 将负责关闭软键盘。

点击按钮时隐藏键盘

添加,如果你想有条件地隐藏键盘,比如在点击按钮后,使用这个:

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()
                }
        ...

1.0.0 中,您可以使用 SoftwareKeyboardControllerFocusManager 来执行此操作。

本回答着重于他们的不同点。


设置:

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()

这只会关闭键盘,但不会从任何聚焦的 TextField 中清除焦点(注意光标和粗下划线)。


焦点管理器:

基于 @azizbekians 答案。

val focusManager = LocalFocusManager.current

// TODO =
focusManager.clearFocus()

这将关闭键盘并从 TextField 中清除焦点。

implementation '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
                }
            )
        )
    }

我在CoreTextField中找到了关闭他的方法,使用TextInputService来控制开关

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
)