EditText限制输入数字,精确到小数点后1位的设置方法
时间:2021-01-22 12:27:19|栏目:Android代码|点击: 次
在EditText的布局文件里面设置:
android:inputType="numberDecimal"
在代码中:
/** 输入框小数的位数*/ private static final int DECIMAL_DIGITS = 1;
InputFilter lengthfilter = new InputFilter() { public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { // 删除等特殊字符,直接返回 if ("".equals(source.toString())) { return null; } String dValue = dest.toString(); String[] splitArray = dValue.split("//."); if (splitArray.length > 1) { String dotValue = splitArray[1]; int diff = dotValue.length() + 1 - DECIMAL_DIGITS; if (diff > 0) { return source.subSequence(start, end - diff); } } return null; } }; et_content.setFilters(new InputFilter[] { lengthfilter });