5

Wanna create TextField for phone number. Need help with set cursor in field always in end of string

What i trying to do: When got new number in field , i get all numbers that already entered, than append underscore( _ ) for have at least 11 string length, and than return string with numbers or "_" by substring:

return "+7(${template.substring(1, 4)}) ${template.substring(4,7)} ${template.substring(7,9)} ${template.substring(9, 11)}" If cursor always in end it works, but if not the numbers order is broken

Full code:

@Composeble

@Composable
fun PhoneNumberEditText(
    phoneNumState: MutableState<String>,
    modifier: Modifier = Modifier,
    imeAction: ImeAction = ImeAction.Done,
    onImeAction: () -> Unit = {}
) {
    TextField(
        shape = RoundedCornerShape(16.dp),
        value = phoneNumState.value,
        onValueChange = { value ->
            phoneNumState.value = value
            val digits = phoneNumState.value.toCharArray().filter { it.isDigit() }
            phoneNumState.value = phoneNumTemplate(digits)
        },
        modifier = modifier
            .clip(RoundedCornerShape(16.dp))
            .size(343.dp, 54.dp),
        singleLine = true,
        placeholder = {
            Text(
                "+7(___)_______", style = passwordTextStyle,
            )
        },
        colors = TextFieldDefaults.textFieldColors(
            backgroundColor = whiteBackground,
            focusedIndicatorColor = Color.Transparent,
            unfocusedIndicatorColor = Color.Transparent,
            cursorColor = greyColor,
            textColor = greyColor
        ),
        leadingIcon = {
            Image(
                painterResource(id = R.drawable.ic_phone),
                stringResource(id = R.string.nomad_contentDescription_icon_phone)
            )
        },
        keyboardOptions = KeyboardOptions(
            imeAction = imeAction,
            keyboardType = KeyboardType.Phone
        ),
    )

}
 

String changer:

fun phoneNumTemplate(chars: List<Char>): String {
    return if (chars.isNotEmpty()) {
        val digits = mutableListOf<Int>()
        chars.forEach {
            digits.add(it.toString().toInt())
        }
        val template = StringBuilder()
        digits.forEach { template.append(it) }
        for (i in 0..11) {
            template.append("_")
        }
        "+7(${template.substring(1, 4)}) ${template.substring(4,7)} ${template.substring(7,9)} ${template.substring(9, 11)}"
    } else {
        "+7(___) ___ __ __"
    }
}

thank you for your help

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Boris Sadakov
  • 617
  • 1
  • 7
  • 18

1 Answers1

12

It is something different but instead of using onValueChange you can try with the visualTransformation property.
Something like:

TextField(
    //your code...,
    visualTransformation = PhoneNumberTransformation()

)

with:

class PhoneNumberTransformation() : VisualTransformation {
    override fun filter(text: AnnotatedString): TransformedText {
        return phoneNumFilter(text)
    }
}

fun phoneNumFilter(text: AnnotatedString): TransformedText {

    // +X(XXX)_XXX_XX_XX
    val trimmed = if (text.text.length >= 11) text.text.substring(0..10) else text.text
    var out = ""
    for (i in trimmed.indices) {
        if (i==0) out += "+"
        if (i==1) out += "("
        out += trimmed[i]
        if (i==3) out +=") "
        if (i==6 || i==8 ) out += " "
       
    }
    
    val phoneNumberOffsetTranslator = object : OffsetMapping {
        override fun originalToTransformed(offset: Int): Int {
            if (offset <= 0) return offset
            if (offset <= 1) return offset +1
            if (offset <= 3) return offset +2
            if (offset <= 7) return offset +4
            if (offset <= 9) return offset +5
            if (offset <= 11) return offset +6
            return 17
            
        }

        override fun transformedToOriginal(offset: Int): Int {
            if (offset <=0) return offset
            if (offset <=2) return offset -1
            if (offset <=7) return offset -2
            if (offset <=12) return offset -4
            if (offset <=15) return offset -5
            if (offset <=18) return offset -6
            return 11
        }
    }

    return TransformedText(AnnotatedString(out), phoneNumberOffsetTranslator)
}

enter image description here

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841