Android EditText 输入金额(小数点后两位)

 1 EditText edit = new EditText(context);
 2 InputType.TYPE_NUMBER_FLAG_DECIMAL //小数点型
 3 InputType.TYPE_CLASS_NUMBER //整数型
 4 
 5 //设置Input的类型两种都要
 6 
 7 edit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL|InputType.TYPE_CLASS_NUMBER);
 8 
 9 //设置字符过滤
10 edit.setFilters(new InputFilter[]{new InputFilter() {
11     @Override
12     public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
13         if(source.equals(".") && dest.toString().length() == 0){
14             return "0.";
15         }
16         if(dest.toString().contains(".")){
17             int index = dest.toString().indexOf(".");
18             int length = dest.toString().substring(index).length();
19             if(length == 3){
20                 return "";
21             }
22         }
23         return null;
24     }
25 }});

 

posted @ 2016-09-20 18:53  90丶  阅读(12145)  评论(0编辑  收藏  举报