我实现了一个有很多textfield的屏幕,我使用scaffold作为根层。因为我希望我的屏幕是rtl的,所以我使用了Composition Local provider()。
CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl ) {}
我的TextFields在屏幕上是这样的:
B
D C
当我想浏览这些TextFields时:
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Next),
keyboardActions = KeyboardActions(
onNext = { LocalFocusManager.current.moveFocus(FocusDirection.Next) }
)
我预料会发生的事:A ->B→C→D
发生了什么:B ->一个→D→C
(我使用一个列作为父根,但文本字段有行作为父根二乘二(行在列))
有人能帮忙吗?谢谢。
您可以使用.focusOrder
修饰符来实现这一点:
Column() {
val (aFocus, bFocus, cFocus, dFocus) = remember { FocusRequester.createRefs() }
Row {
OutlinedTextField(
value = "A",
onValueChange = {},
modifier = Modifier
.weight(1f)
.focusOrder(aFocus) { next = bFocus },
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Next),
)
OutlinedTextField(
value = "B",
onValueChange = {},
modifier = Modifier
.weight(1f)
.focusOrder(bFocus) { next = cFocus },
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Next)
)
}
Row {
OutlinedTextField(
value = "C",
onValueChange = {},
modifier = Modifier
.weight(1f)
.focusOrder(cFocus) { next = dFocus },
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Next)
)
OutlinedTextField(
value = "D",
onValueChange = {},
modifier = Modifier
.weight(1f)
.focusOrder(dFocus) { },
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Next)
)
}
}