Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if an input in EditText is an integer?

Tags:

Hi I'm a newbie in Android Programming.

I'm trying to build an activity which includes an edittext field and a button. When user type in an integer, the button will lead them to the next activity. However I do not if there's a way to check the type of user's input.

Anyone can help me? Thank you very much!

like image 252
Yansuck Avatar asked Apr 12 '12 08:04

Yansuck


People also ask

How do you check that input is an integer?

Use the isnumeric() Method to Check if the Input Is an Integer or Not. The isnumeric() method of the string returns True if the string only contains numbers. However, it is worth noting that it fails with negative values. This is because it automatically returns False when it encounters the - sign in negative integers.

How can I get my EditText number?

Use android:inputType="number" to force it to be numeric. Convert the resulting string into an integer (e.g., Integer. parseInt(myEditText. getText().

How do I convert int to EditText?

String value= et. getText(). toString(); int finalValue=Integer. parseInt(value);

Which method is used to get the string value from an EditText?

This can be done by using the toString() method.


1 Answers

Since API level 1, Android provides an helper method to do just that (no need to use regex or catching exception) : TextUtils.isDigitsOnly(CharSequence str)

boolean digitsOnly = TextUtils.isDigitsOnly(editText.getText()); 

Note that this method returns true with empty String : Issue 24965

like image 129
jmaffre Avatar answered Sep 29 '22 23:09

jmaffre