Jetpack Compose TextField 捕获键盘 Enter-input
Posted
技术标签:
【中文标题】Jetpack Compose TextField 捕获键盘 Enter-input【英文标题】:Jetpack Compose TextField capture keyboard Enter-input 【发布时间】:2021-08-26 01:00:18 【问题描述】:据我了解,Jetpack Compose Textfield 有一个 API 用于捕获键盘操作,但我不知道哪些 API 可以捕获 Enter-Input
此捕获输入输入的用例是启用单击 Enter 并尝试转到下一个 TextField 并在保持键盘打开的同时保持
OutlinedTextField(
value = username.value,
onValueChange =
username.value = it
,
keyboardActions = KeyboardActions(
onDone = ,
onGo = ,
onNext = ,
onPrevious =,
onSearch =,
onSend =
)
)
【问题讨论】:
【参考方案1】:你可以使用类似的东西:
val (focusRequester) = FocusRequester.createRefs()
TextField(
value = text,
onValueChange =
text = it
,
singleLine = true,
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
keyboardActions = KeyboardActions(
onDone = focusRequester.requestFocus()
),
modifier = Modifier.onKeyEvent
if (it.nativeKeyEvent.keyCode == KeyEvent.KEYCODE_ENTER)
focusRequester.requestFocus()
true
false
)
TextField(
value = text2,
onValueChange =
text2 = it
,
modifier = Modifier.focusRequester(focusRequester),
)
【讨论】:
感谢您,我尝试将其添加到第二个文本字段singleLine = true, keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done), keyboardActions = KeyboardActions( onDone = focusRequester.requestFocus() ), modifier = Modifier.onKeyEvent if (it.nativeKeyEvent.keyCode == KeyEvent.KEYCODE_ENTER) focusRequester.requestFocus() true false
到第二个文本字段并改用 focusRequester.freeFocus()
但不知何故它并没有消除对第二个文本字段的关注任何想法关于这个?
@ArcRuler 使用val focusManager = LocalFocusManager.current
并在第二个字段的Modifier.onKeyEvent
中:focusManager.clearFocus()
以上是关于Jetpack Compose TextField 捕获键盘 Enter-input的主要内容,如果未能解决你的问题,请参考以下文章
Jetpack Compose 在 TextField 中对齐输入文本
Jetpack Compose 中的 Android 基本 TextField 对齐
在 Jetpack Compose TextField 中操作/更改键盘输入
Kotlin jetpack compose 文本输入框ExitText/TextField remember 居然可以传两个参数