Android:使用NumberFormat

时间:2017-03-31 05:48:12

标签: android number-formatting android-input-filter input-filter

在我的Android应用程序中,我使用decimal输入过滤器来阻止小数seperator(.)之前的数字位数为14,小点数位数为4,同时使用带有美国语言环境的NumberFormat格式化用户输入的金额。我在下面尝试这个时发现了2个问题。

  • 当有14个整数位时,不能输入十进制数字。当少于14个decimal位数时,可以输入Integer
  • 当更改大于999的金额时,用户输入的第一个数字没有注册,我注意到任何大于999的数字都将用逗号格式化,所以当用新金额更改这些金额时,输入的第一个数字将不来,为什么如此。假设Edittext包含金额4,000.00然后改为9500然后只显示500.但是当我们改变450.00这样的金额时没有逗号然后没有问题,所有数字都会来.Below是我的代码。

    NumberFormat formatUsTwo = NumberFormat.getInstance(Locale.US);

    edtAmount=(EditText) v.findViewById(R.id.edtAmount);
    edtAmount.setFilters(new InputFilter[] {new DecimalDigitsInputFilter(14,4)});
    edtAmount.setText(formatUsTwo.format(9000));
    
    edtAmount.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean hasFocus) {
    if(!hasFocus){
    Double  change=Double.parseDouble(edtAmount.getText().toString().trim().replace(",", ""));
                        edtAmount.setText(formatUsTwo.format(change));
                    }else{
                        onFocus(view, holder);
                    }
                }
            });
    
    
    
    public class DecimalDigitsInputFilter implements InputFilter {
    Pattern mPattern;
    public DecimalDigitsInputFilter(int digitsBeforeZero, int digitsAfterZero) {
                    mPattern = Pattern.compile("[0-9]{0," + (digitsBeforeZero - 1) + "}+((\\.[0-9]{0," + (digitsAfterZero - 1) + "})?)||(\\.)?");
                }
    
    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                    Matcher matcher = mPattern.matcher(dest);
                    if (!matcher.matches())
                        return "";
                    return null;
                }
            }
    

1 个答案:

答案 0 :(得分:0)

最后我在花了一半的时间后解决了这个问题。我改变了我的DecimalInputDegitsFilter类屁股,解决了这个问题。         public class DecimalDigitsInputFilter实现InputFilter {         模式mPattern;         int digitsBeforeZero,digitsAfterZero;         public DecimalDigitsInputFilter(int digitsBeforeZero,int digitsAfterZero){             this.digitsBeforeZero = digitsBeforeZero;             this.digitsAfterZero = digitsAfterZero;             mPattern = Pattern.compile(" [0-9] {0," +(digitsBeforeZero - 1)+"} +((\\。[0-9] {0,&# 34; +(digitsAfterZero - 1)+"})?)||(\\。)?");         }         @覆盖         public CharSequence filter(CharSequence source,int start,int end,Spanned dest,int dstart,int dend){             Matcher matcher = mPattern.matcher(dest.toString()。replace(",",""));             if(!matcher.matches()){                 if(dest.toString()。contains("。")){                     if(dest.toString()。substring(dest.toString()。indexOf("。"))。length()> digitsAfterZero){                         返回"&#34 ;;                     }                     return null;                 } else if(!Pattern.compile(" [0-9] {0," +(digitsBeforeZero - 1)+"}")。matcher(dest).matches( )){                     if(!dest.toString()。contains("。")){                         if(source.toString()。equalsIgnoreCase("。")){                             return null;                         }                     }                     返回"&#34 ;;                 }                 返回"&#34 ;;             }             return null;         }     }