Android EditText 输入金额(小数点后两位)
2017-11-14 本文已影响29人
next_discover
EditText edit = new EditText(context);
InputType.TYPE_NUMBER_FLAG_DECIMAL //小数点型
InputType.TYPE_CLASS_NUMBER //整数型
//设置Input的类型两种都要
edit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL|InputType.TYPE_CLASS_NUMBER);
//设置字符过滤
edit.setFilters(new InputFilter[]{new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if(source.equals(".") && dest.toString().length() == 0){
return "0.";
}
if(dest.toString().contains(".")){
int index = dest.toString().indexOf(".");
int length = dest.toString().substring(index).length();
if(length == 3){
return "";
}
}
return null;
}
}});