Hello, can't seem to wrap my head around this,
... all i want to do is append a hyphen(a form of separator) after every double digit entered.
this's part of my custom textfield
input = numberValidationState.text,
onValueChange = {
numberValidationState.text = numberValidationState.autoHyphenate(it)
numberValidationState.enableShowErrors()
},
textFieldState = numberValidationState,
and this is my extension function to do the trick
fun String.appendHyphen(): String {
val formattedInput = StringBuilder()
this.filter { it.isDigit() }.forEachIndexed { index, char ->
formattedInput.append(char)
if ((index + 1) % 2 == 0) {
formattedInput.append("-")
}
}
return formattedInput.toString()
}
just that this trick is being tricky.
The cursor appears behind the separator when its appended and entering another number reverses whatever the user intends to input. So 45 becomes 54 and this mistake gets carried on.
Somebody kindly help me.