Fixed
Status Update
Comments
ap...@google.com <ap...@google.com> #2
Any news about this issue?
ap...@google.com <ap...@google.com> #3
Any news about this issue?
ap...@google.com <ap...@google.com> #4
I have the same issue.
Jetpack Compose version: 1.0.2
My code:
@ExperimentalComposeUiApi
@Composable
fun Code(modifier: Modifier = Modifier, onCodeInputted: (String) -> Unit = {}) {
val passcodeLength = 6
val codeArray = remember {
Array(passcodeLength) {
mutableStateOf(TextFieldValue(text = ""))
}
}
val focusRequesters = remember {
(0 until passcodeLength).map { FocusRequester() }
}
Row(modifier = modifier) {
repeat(passcodeLength) { textFieldIndex ->
OutlinedTextField(
modifier = Modifier
.weight(1f)
.onPreviewKeyEvent {
if (it.type == KeyEventType.KeyDown && it.key == Key.Backspace &&
textFieldIndex != 0 && codeArray[textFieldIndex].value.text.isEmpty()
) {
codeArray[textFieldIndex - 1].value =
codeArray[textFieldIndex - 1].value.copy(text = "")
focusRequesters[textFieldIndex - 1].requestFocus()
}
false
}
.focusRequester(focusRequesters[textFieldIndex]),
value = codeArray[textFieldIndex].value,
textStyle = LocalTextStyle.current.copy(
textAlign = TextAlign.Center
),
singleLine = true,
keyboardOptions = KeyboardOptions.Default.copy(
keyboardType = KeyboardType.Number,
imeAction = ImeAction.Next
),
onValueChange = { newTextFieldValue ->
val newString = newTextFieldValue.text
if (newString.isDigitsOnly() && codeArray[textFieldIndex].value.text != newString) {
codeArray[textFieldIndex].value =
if (newString.length > 1) {
newTextFieldValue.copy(text = newString[0].toString())
} else newTextFieldValue
if (newString != "") {
val nextEmpty =
codeArray.indices.firstOrNull { codeArray[it].value.text == "" }
if (nextEmpty != null) {
focusRequesters[nextEmpty].requestFocus()
} else {
onCodeInputted(codeArray.joinToString(separator = "") { it.value.text })
}
}
}
}
)
if (textFieldIndex != passcodeLength - 1) {
Spacer(Modifier.width(10.dp))
}
}
}
}
Description
Usecases: