kotlin 如何从Jetpack编写TextField中关闭虚拟键盘?

5anewei6  于 2022-11-30  发布在  Kotlin
关注(0)|答案(7)|浏览(261)

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

vkc1a9a21#

按一下按钮时隐藏键盘

要添加Gabriele Mariotti's solution,如果你想有条件地隐藏键盘,比如说在一个按钮点击后,使用这个:

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

nlejzf6q2#

    • alpha-12发布后编辑:**参见@azizbekian回复。
    • α 12前React**

我找到了解决方案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 }
)
ffx8fchx

ffx8fchx3#

我在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
)
p8ekf7hl

p8ekf7hl4#

实现“androidx.compose.material3:材料3:1.0.0-alpha 02”

具有“输入时隐藏键盘”操作的文本字段

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

ubof19bj

ubof19bj5#

您可以使用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中删除焦点。

4nkexdtk

4nkexdtk6#

从compose 1.0.0-alpha12开始(在compose 1.3.1中仍然有效),不赞成使用onImeActionPerformed,建议使用keyboardActionskeyboardOptions的组合:

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

pinkon5k

pinkon5k7#

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

相关问题