1.方法
/**
*
* @Title: setRegion
* @Description: TODO(限制EditText只能输入设定范围内数字)
* @param: @param et
* @return: void
* @throws
*/
private void setRegion(final EditText et) {
et.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (start > 1){
if (MIN_MARK != -1 && MAX_MARK != -1){
int num = Integer.parseInt(s.toString());
if (num > MAX_MARK) {
s = String.valueOf(MAX_MARK);
et.setText(s);
}else if(num < MIN_MARK)
s = String.valueOf(MIN_MARK);
return;
}
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
if (s != null && !s.equals("")) {
if (MIN_MARK != -1 && MAX_MARK != -1) {
int markVal = 0;
try {
markVal = Integer.parseInt(s.toString());
}catch (NumberFormatException e){
markVal = 0;
}
if (markVal > MAX_MARK) {
Toast.makeText(getBaseContext(), "输入数字不能超过110", Toast.LENGTH_SHORT).show();
et.setText(String.valueOf(MAX_MARK));
}
return;
}
}
}
});
}
2.引用
this.setRegion(edt_time);