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!
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.
Use android:inputType="number" to force it to be numeric. Convert the resulting string into an integer (e.g., Integer. parseInt(myEditText. getText().
String value= et. getText(). toString(); int finalValue=Integer. parseInt(value);
This can be done by using the toString() method.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With