How to move focus from Compose Button to Compose TextField by using the arrow key?
When I have moved focus from Compose Button to Compose TextField by using the arrow key(KeyEvent.KEYCODE_DPAD_UP),
the focus moves twice for that key press. (when I use page-up-key)
I hope that the focus moves once for that key press.
Please see GIF animation for details
Is there a way to fix this?
(I use up-key and down-key in moving between TextFields
using moveFocus method of focusManager in onKeyEvent)
@Composable
fun Screen() {
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally
) {
val focusManager = LocalFocusManager.current
FocusMoveTextField(focusManager)
FocusMoveTextField(focusManager)
FocusMoveTextField(focusManager)
OutlinedButton(
onClick = {}
) {
Text("Button")
}
}
}
@Composable
private fun FocusMoveTextField(
focusManager: FocusManager
) {
OutlinedTextField(
modifier = Modifier
.fillMaxWidth()
.onKeyEvent { keyEvent ->
when (keyEvent.nativeKeyEvent.keyCode) {
KEYCODE_DPAD_DOWN -> {
focusManager.moveFocus(FocusDirection.Down)
true
}
KEYCODE_DPAD_UP -> {
focusManager.moveFocus(FocusDirection.Up)
true
}
else -> {
false
}
}
},
value = "",
onValueChange = {},
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Number,
imeAction = ImeAction.Done
)
)
}
