开发者

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

开发者 https://www.devze.com 2023-03-04 21:31 出处:网络
I need to implement an edittext field that just allows user i开发者_如何学Pythonnput from 20 to 60. If user input a number that is out of range, a dialog will display and force user to input again.

I need to implement an edittext field that just allows user i开发者_如何学Pythonnput 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.


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) { }
});


How about keeping track of the EditText field with onTextChanged?


use these

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

then when you get the number like: number.gettext() you make validations like if number>60 and lower then 20 you can do number.setHint("number no valid");

0

精彩评论

暂无评论...
验证码 换一张
取 消