非常强大 贴网址使用自己看吧
下午用setOnKeyListener 监听按键输入,发现字母跟数字无法监听,估计这些都不是key吧。。。
于是乎转用TextWatcher 药到病除上代码:
//--新增 设置 EditText 范围检查
public ViewHolder setEditTextCheck(int viewID, Object object) {
final MaterialEditText editText = getView(viewID);
DeviceParameterBean deviceParameterBean = (DeviceParameterBean) object;
;
final int[] scope = deviceParameterBean.getScope();
if (deviceParameterBean != null) {
editText.setText(deviceParameterBean.getValue());
editText.setFloatingLabelText(deviceParameterBean.getName());
if (scope != null && scope.length == 2)
editText.setHelperText(String.format("请输入范围在[%s,%s]", scope[0], scope[1]));
}
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
try {
float tempValue = Float.parseFloat(editText.getText().toString());
if (scope != null && scope.length == 2) {
if (tempValue > scope[1] || tempValue < scope[0]) {
editText.setError(String.format("非法输入\n请输入范围在[%s,%s]", scope[0], scope[1]));
}
}
} catch (Exception e) {
if (scope != null && scope.length == 2)
editText.setError(String.format("非法输入\n请输入范围在[%s,%s]", scope[0], scope[1]));
}
}
});
return this;
}