Android EditText 增加自定义过滤
在Android中,可以通过对EditText设置setFilters方法,用代码控制EditText的输入长度,或控制输入小数的位数等。
1.设置EditText的输入长度
inputEdit.setFilters(new InputFilter[] { new InputFilter.LengthFilter(length)});
// 设置小数位数控制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 - digLength;if (diff > 0) {return source.subSequence(start, end - diff);}}return null;}};inputEdit.setFilters(new InputFilter[] { lengthfilter });