【问题标题】:Why FocusManager doesn't work inside AlertDialog?为什么 FocusManager 在 AlertDialog 中不起作用?
【发布时间】:2021-09-20 05:51:12
【问题描述】:

有谁知道如何在AlertDialog 中显示或隐藏键盘?

focusManager.clearFocus()AlertDialog 中不起作用。

textInputService?.hideSoftwareKeyboard()softwareKeyboardController?.hide() 相同。

例如:

AlertDialog(
    onDismissRequest = {
        openDialog.value = false
    },
    text = {
        TextField(...)
    }
    buttons = {
        Button(
            modifier = Modifier.fillMaxWidth(),
            onClick = { focusManager.clearFocus() }
        ) {
            Text("Update")
        }
    }
)

【问题讨论】:

  • 请添加您的代码。你是如何隐藏键盘的?
  • @SK.Fuad 添加了示例代码,谢谢。

标签: android kotlin android-jetpack-compose


【解决方案1】:

AlertDialog 与任何其他 Dialog 一样,有自己的 LocalFocusManager 以及其他一些局部常量。

您在AlertDialog 之外捕获它的值,而不是您需要在内部捕获它:

buttons = {
    val focusManager = LocalFocusManager.current
    Button(
        modifier = Modifier.fillMaxWidth(),
        onClick = { focusManager.clearFocus() }
    ) {
        Text("Update")
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-09
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 2017-09-03
    • 2016-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多