Check if string is number in android

I have three text fields that are numbers or decimal.

They are accepted as strings that are subsequently converted.

However, I want to check these fields so that only numbers or decimal numbers are accepted.

The easiest way to ensure that these fields are only 0-9 characters.

I would prefer an if statement, but any solution would be nice!

+6
source share
2 answers

Use the string.matches method, which takes a regular expression as an argument.

 if(string.matches("\\d+(?:\\.\\d+)?")) { System.out.println("Matches"); } else { System.out.println("No Match"); } 
+18
source

If you inflate these text images from xml, you can simply use:

 android:inputType="number" 
+1
source

Source: https://habr.com/ru/post/981830/


All Articles