46

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

6 回答 6

91

您可以使用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()

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

于 2020-09-01T22:03:25.653 回答
55

从 compose 开始1.0.0-alpha12(并且在 compose 中仍然有效1.1.0onImeActionPerformed已弃用,建议的方法是使用以下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()将负责关闭软键盘。

于 2021-02-18T11:33:56.953 回答
10

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 中清除焦点。

于 2021-08-04T10:55:54.180 回答
3

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

Pre-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 }
)
于 2019-12-02T12:27:26.903 回答
3

单击按钮时隐藏键盘

要添加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()
                }
        ...
于 2021-08-04T07:48:11.410 回答
0

实现'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
                }
            )
        )
    }

在 Ime 操作上带有隐藏键盘的文本字段

于 2022-01-04T03:17:20.973 回答