如何将请求焦点转移到 Jetpack 中的下一个文本字段
How shift Request Focus to next textfield in Jetpack compose
我的要求是,当我在文本字段中输入 1 个数字后,应该将其转移到下一个文本字段。我到处都得到这样的答案。
简而言之,我在 android
中寻找类似于 onTextChangeListener 的功能
Request Focus on TextField in jetpack compose
这段代码应该给出了一个基本的概念。
代码
@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),
)
}
}
我的要求是,当我在文本字段中输入 1 个数字后,应该将其转移到下一个文本字段。我到处都得到这样的答案。
简而言之,我在 android
中寻找类似于 onTextChangeListener 的功能Request Focus on TextField in jetpack compose
这段代码应该给出了一个基本的概念。
代码
@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),
)
}
}