我的要求是当我在输入后在文本字段中输入 1 位数字时,它应该转移到下一个文本字段。我到处得到这样的答案。
简而言之,我正在寻找类似的功能,如 android 中的 onTextChangeListener
这段代码应该给出一个基本的想法。
代码
@Composable
fun OtpScreen() {
val focusManager = LocalFocusManager.current
val (digit1, setDigit1) = remember {
mutableStateOf("")
}
val (digit2, setDigit2) = remember {
mutableStateOf("")
}
val (digit3, setDigit3) = remember {
mutableStateOf("")
}
val (digit4, setDigit4) = remember {
mutableStateOf("")
}
LaunchedEffect(
key1 = digit1,
) {
if (digit1.isNotEmpty()) {
focusManager.moveFocus(
focusDirection = FocusDirection.Next,
)
}
}
LaunchedEffect(
key1 = digit2,
) {
if (digit2.isNotEmpty()) {
focusManager.moveFocus(
focusDirection = FocusDirection.Next,
)
}
}
LaunchedEffect(
key1 = digit3,
) {
if (digit3.isNotEmpty()) {
focusManager.moveFocus(
focusDirection = FocusDirection.Next,
)
}
}
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceAround,
) {
OutlinedTextField(
value = digit1,
onValueChange = {
setDigit1(it)
},
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.NumberPassword,
imeAction = ImeAction.Next,
),
modifier = Modifier.width(64.dp),
)
OutlinedTextField(
value = digit2,
onValueChange = {
setDigit2(it)
},
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.NumberPassword,
imeAction = ImeAction.Next,
),
modifier = Modifier.width(64.dp),
)
OutlinedTextField(
value = digit3,
onValueChange = {
setDigit3(it)
},
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.NumberPassword,
imeAction = ImeAction.Next,
),
modifier = Modifier.width(64.dp),
)
OutlinedTextField(
value = digit4,
onValueChange = {
setDigit4(it)
},
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.NumberPassword,
imeAction = ImeAction.Done,
),
modifier = Modifier.width(64.dp),
)
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句