Jetpack编写文本字段捕获键盘Enter-Input

Jetpack Compose TextField capture keyboard Enter-input(Jetpack编写文本字段捕获键盘Enter-Input)

本文介绍了Jetpack编写文本字段捕获键盘Enter-Input的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,Jetpack Compose Textfield有一个用于捕获键盘操作的API,但我不知道哪个API可以捕获Enter-Input

此捕获Enter输入的用例是启用按Enter并尝试转到下一个文本字段并保持键盘打开

OutlinedTextField(
    value = username.value,
    onValueChange = {
        username.value = it
        },
    keyboardActions = KeyboardActions(
        onDone = {},
        onGo = {},
        onNext = {},
        onPrevious ={},
        onSearch ={},
        onSend = {}
        )
)

推荐答案

您可以使用如下内容:

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

这篇关于Jetpack编写文本字段捕获键盘Enter-Input的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Jetpack编写文本字段捕获键盘Enter-Input

基础教程推荐