Jetpack Compose TextField 捕获键盘 Enter-input

时间:2021-06-09 03:21:57

标签: android kotlin textfield android-jetpack-compose android-jetpack-compose-text

据我所知,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 个答案:

答案 0 :(得分:4)

您可以使用以下内容:

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