1

so I have a OutlinedTextField and I want to have a undeletable prefix in it.

here is my OutlinedTextField:

OutlinedTextField(
    modifier = Modifier
            .focusRequester(focusRequester),
    value = viewModel.phoneNumber.collectAsState().value,
    onValueChange = { viewModel.onPhoneNumberChange(it) },
    label = { Text("phone number") },
    singleLine = true
)

and here is my viewModel:

private val _phoneNumber = MutableStateFlow("+")
val phoneNumber: StateFlow<String> = _phoneNumber

fun onPhoneNumberChange(
    value: String
) {
    viewModelScope.launch {
        if (_phoneNumber.value.length == 1 && value == "") {
            return@launch
        } else
            _phoneNumber.emit(value.take(phoneNumberMaxLength))
    }
}

as you can see I managed to not emit new value when its delete event and it works fine.

but the problem is the coursor goes behind of the prefix when I press back space button sothe question is how can i subscribe on soft keyboard with jetpack compose and manage back space button event?

MohammadBaqer
  • 766
  • 6
  • 32
  • 2
    Don't use `onValueChange` but **`visualTransformation`**. You can check an example here: https://stackoverflow.com/questions/67735208/how-to-prefix-country-code-in-textfield-using-jetpack-compose/67735317#67735317 – Gabriele Mariotti Oct 13 '22 at 11:28

0 Answers0