How to check EditText to process numbers only in Android?

How can I check text representation in android to handle positive integers?

I do not want him to accept any characters or signs, etc.

+4
source share
5 answers

Have you looked at the EditText inputType attribute? You can set up a whole bunch of different input types that EditText should restrict to the user.

From the sounds of this, you are probably looking for something like:

 <EditText .... android:inputType="number" ... /> 
+7
source

You can use regular expressions. See: http://developer.android.com/reference/java/util/regex/Pattern.html

Your template may look like ^[0-9]{1,10}$ which means that the entered value can consist only of digits (minimum 1, maximum 10)

+2
source

IMO's best way is to use inputType in xml

 <EditText ... android:inputType="phone" /> 

You must remember that the β€œ+” is also part of the phone number.

+2
source

Use XML Edittext to Limit Any Range of Values

 android:digits="0123456789" 
+2
source

You can use xml

 <EditText ... android:inputType="phone" /> 

or programmatically

 EditText editText = (EditText) findViewById(R.id.yourId); editText.setInputType(InputType.TYPE_CLASS_NUMBER); 
0
source

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


All Articles