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 }});

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM