Android - How can I initiate validation for edittext field

I need to implement an edittext field that allows the user to enter from 20 to 60. If the user enters a number that is out of range, a dialog box appears and forces the user to enter it again.

Thus, the text observer is not useful because it cannot prevent the user from entering a number less than 20.

OnFocusChangedListener is not, if the user clicks the "done" button, the edittext will not lose focus, so the trigger does not work.

In addition, the edittext is inside the tab view, so when the user clicks on another tab, the trigger fires, but the user can no longer enter a value for this edittext.

If you have any idea about this, plz share with me.

Thank you very much.

+4
source share
3 answers

Alvin is right ... this is my code to do something with the text when it was entered, but could be just as easily a check:

smsMsgBody_editText.addTextChangedListener(new TextWatcher() { public void onTextChanged(CharSequence s, int start, int before, int count) { // Do something fancy public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void afterTextChanged(Editable s) { } }); 
+2
source

How about tracking an EditText field using onTextChanged?

+1
source

use these

 android:maxLength="2" android:numeric="integer" 

then when you get a number like: number.gettext (), you will perform checks, for example, if the number is> 60 and below 20, you can do number.setHint("number no valid");

+1
source

All Articles