I have a textfield in android jetpack compose, so I want to put limit for numbers, for example; user can write only numbers from 1 to 10, is it possible to do it in jetpack compose?
@Preview(showBackground = true)
@Composable
fun OutlinedTextFieldComposable() {
var text by remember { mutableStateOf("") }
OutlinedTextField(
value = text,
onValueChange = { text = it },
label = { Text("Label") },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number))
}
CodePudding user response:
You can make a condition for that inside onValueChange
like this:
@Preview(showBackground = true)
@Composable
fun OutlinedTextFieldComposable() {
var text by remember { mutableStateOf("") }
val maxNumbers = 10
OutlinedTextField(
value = text,
onValueChange = { if (it.toInt() <= maxNumbers) text = it },
label = { Text("Label") },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number))
}