You can assign a TextWatcher
to your EditText
and listen for text changes there, for example:
public void afterTextChanged(Editable s) {
try {
int val = Integer.parseInt(s.toString());
if(val > 60) {
s.replace(0, s.length(), "60", 0, 2);
} else if(val < 1) {
s.replace(0, s.length(), "1", 0, 1);
}
} catch (NumberFormatException ex) {
// Do something
}
}
As mentioned by Devunwired, notice that calls to s.replace()
will call the TextWatcher again recursively.
It is typical to wrap these changes with a check on a boolean "editing" flag so the recursive calls skip over and simply return while the changes that come from within.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…