Android - How can I trigger a validation for an edittext field
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");
精彩评论