3

So I need to allow only numbers to by typed in the field.

I configured TextField with:

keyboardOptions = KeyboardOptions.Default.copy(
    keyboardType = KeyboardType.Number
)

But still it allows me to type decimal separators (comma and dot)

So I don't see any difference between KeyboardType.Number and KeyboardType.Decimal, they work exactly the same...

Number

@Stable
public final val Number: KeyboardType
A keyboard type used to request an IME that is capable of inputting digits. IME may provide inputs other than digits but it is not guaranteed.

Decimal

@Stable
public final val Decimal: KeyboardType
A keyboard type used to request an IME that is capable of inputting decimals. IME should explicitly provide a decimal separator as input, which is not assured by KeyboardType.Number.

Why does it happen?

user924
  • 8,146
  • 7
  • 57
  • 139

2 Answers2

5

As explained in this issue:

Most keyboards show a decimal separator when keyboard type is set as Number. However, it is possible that a keyboard expects TYPE_NUMBER_FLAG_DECIMAL flag in inputType to actually show a key for decimal separator.
This change adds a new KeyboardType called Decimal that explicitly sets the required flag. Number and Decimal would essentially behave the same for most keyboards and OEMs.

You can restrict the allowed characters using a regex pattern.

Something like:

val pattern = remember { Regex("^\\d+\$") }

TextField(
    value = text,
    onValueChange = {
        if (it.isEmpty() || it.matches(pattern)) {
            text = it
        }
    },
    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
)
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
1

Use KeyboardType.NumberPassword to display only numbers to users which is used for entering numbers between 0 and 9 only, and very convenient for entering amounts with no decimal points. This prevents showing user comma or dash which they are not allowed to enter anyway.

Using a pattern is only required if you wish to know this is exactly an amount to prevent entries like 04, or 001. And in this case regular expression should be ^[1-9]\d*$

https://stackoverflow.com/a/5661603/5457853

Thracian
  • 43,021
  • 16
  • 133
  • 222