需求是输入框里的数值,不能大于12,不能小于1,设置值之后发现会崩溃。特此记录此崩溃问题的处理。
mEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
String value = mValidityMonth.getText().toString().trim();
if(!TextUtils.isEmpty(value)){
int dataNum = Integer.valueOf(value);
if (dataNum <= 0) {
dataNum = 1;
ToastUtil.show(CActivity.this, "最小值为1");
mEditText.setText(String.format("%s", dataNum));
} else if (dataNum > 12) {
dataNum = 12;
ToastUtil.show(CActivity.this, "最大值不能超过12");
mEditText.setText(String.format("%s", dataNum));
return;
}
mEditText.setSelection(value.length());
}
}
});
网友评论