Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How can I trigger a validation for an edittext field

I need to implement an edittext field that just allows user input from 20 to 60. If user input a number that is out of range, a dialog will display and force user to input again.

So the text watcher is not useful because it cannot prevent user input a number that lower than 20.

The onFocusChangedListener is neither, if user clicks on 'done' button, the edittext doesn't lost focus, so the trigger doesn't fire as well.

Besides, the edittext is inside a tab view, so when user clicks on another tab, the trigger fires but user cannot input value for that edittext any more.

like image 573
thanhbinh84 Avatar asked Oct 11 '22 02:10

thanhbinh84


1 Answers

Alvin is right ... this is my code to do something with text once entered, but could have as easily been a validation sequence:

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) { }
});
like image 99
Bill Mote Avatar answered Oct 20 '22 09:10

Bill Mote