使用正则表达式怎么样..
public class DecimalDigitsFilter implements InputFilter {
Pattern pattern;
public DecimalDigitsFilter(int digitsBeforeZero,int digitsAfterZero) {
pattern=Pattern.compile("[0-9]{0," + (digitsBeforeZero-1) + "}+((\\.[0-9]{0," + (digitsAfterZero-1) + "})?)||(\\.)?");
}
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned destination, int destinationStart, int destinationEnd) {
Matcher matcher=pattern.matcher(destination);
if(!matcher.matches())
return "";
return null;
}
}
然后称它为..
editText.setFilters(new InputFilter[] {new DecimalDigitsFilter(5,1)}); //5 before point and 1 after point.Change it as your need