Home > database >  How to set the inputType for a TextField as a binary (0 and 1) in Jetpack Compose?
How to set the inputType for a TextField as a binary (0 and 1) in Jetpack Compose?

Time:10-27

I'm trying to set input type of the TextField as a Binary but there is no KeyboardOptions KeyboardType as Binary.

So how can I accomplish this ?

  TextField(
        value = text,
        keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
        onValueChange = {
            text = it
        },
        label = { Text("Enter Binary") }
    )

CodePudding user response:

There is no such input options, but you can filter invalid values out:

TextField(
    value = text,
    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
    onValueChange = {
        text = it.filter { it == '0' || it == '1' }
    },
    label = { Text("Enter Binary") }
)
  • Related