简体   繁体   中英

Onclick validate edittext if user enters an int with a string

I have in my application an EditText which validates that the user cannot enter a integer when the user clicks a button.

This is done by an if statement on a condition where word is a String:

(word.matches("[0-9]*")

How can I change this if a user enters a word with a string as well.

Thanks

An EditText inherits from a TextView , which has a method called setInputType . This method accepts an int defined as InputType . So you can set the InputType dynamically depending on what you want/the user clicks

To do this kind of filter in EditText, the better way is to specify the android:inputType . This allow you to specify what you expect and also to display the right keyboard. Another option is to use android:digit to specify the autorized characters. If you need numeric, enter

android:digit="0123456789"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM