2

据我了解,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 = {}
        )
)
4

1 回答 1

6

你可以使用类似的东西:

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),
)
于 2021-06-09T06:20:49.677 回答