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

问题描述 投票:0回答:7

我正在使用 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 }
)
android kotlin android-jetpack-compose android-input-method android-compose-textfield
7个回答
184
投票

可以使用

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
.


87
投票

从撰写

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


19
投票

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 的焦点。


7
投票

点击按钮隐藏键盘

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

3
投票

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

1
投票
我在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 )
    

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

© www.soinside.com 2019 - 2024. All rights reserved.