Android Compose TextField falls into infinite onValueChange loop when try to accept IME composition

42 views Asked by At

I have a Compose password TextField, which I don't want the IME composition, which shows a visual underline. But when I try to accept the IME composition by passing composition = null, it falls into infinite loop.

onValueChange: TextFieldValue(text='g', selection=TextRange(1, 1), composition=TextRange(0, 1)), old: TextFieldValue(text='g', selection=TextRange(1, 1), composition=null)
onValueChange: TextFieldValue(text='g', selection=TextRange(1, 1), composition=TextRange(0, 1)), old: TextFieldValue(text='g', selection=TextRange(1, 1), composition=null)
onValueChange: TextFieldValue(text='g', selection=TextRange(1, 1), composition=TextRange(0, 1)), old: TextFieldValue(text='g', selection=TextRange(1, 1), composition=null)

Remove composition = null works fine, but with a visual underline.

Here is the sample code:

var textFieldValue by remember {
  mutableStateOf(
    TextFieldValue(text = "", selection = TextRange(0))
  )
}

TextField(
  value = textFieldValue,
  onValueChange = { value ->
    println("onValueChange: $value, old: $textFieldValue")
    val newText = value.text.filter { it.isLetterOrDigit() }.take(6)
    textFieldValue = value.copy(text = newText, selection = TextRange(newText.length), composition = null)
  }
)

I submitted a Google issue here: https://issuetracker.google.com/issues/320130081 (Please vote if you have similar issues), but meanwhile I'd like to know if there're any workarounds?

0

There are 0 answers