Android Kotlin Jetpack Compose - 追踪键盘的向下箭头按钮

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

这可能是重复的问题,或者可能已被问过多次。我尝试通过其他堆栈溢出答案搜索解决方案,但我似乎找不到适合我的确切解决方案。我有一个 OutlineTextField,我正在尝试使用向下箭头按钮,我只是想要的是,当我按下向下箭头时,它也应该从文本字段中删除焦点。它只是关闭软键盘。我正在尝试找到一个侦听器或任何覆盖的方法,在其中我可以触发向下箭头按钮并放置我的代码

focusManager.clearFocus()
。我在Done KeyboardAction 上有类似的操作。如有任何帮助,我们将不胜感激。

我尝试过 BackHandler 和 onKeyEvent 重写方法以及 textField 修饰符的 onKeyEvent 方法,但单击向下箭头时似乎没有触发任何内容。

android kotlin android-jetpack-compose android-keypad
1个回答
0
投票

试试这个,在 KeyboardActions 中使用你的代码

 var text by remember { mutableStateOf("") }
 val focusManager = LocalFocusManager.current
 OutlinedTextField(
    value = text,
    onValueChange = { text = it },
    label = { Text("Label") },
    keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
    keyboardActions = KeyboardActions(onNext = {
        if (!focusManager.moveFocus(FocusDirection.Down))
            focusManager.clearFocus()
    }),
    singleLine = true
 )
© www.soinside.com 2019 - 2024. All rights reserved.