Android - 如何只允许一定数量的小数位

edd*_*ddy 6 java android android-edittext

您是否知道有任何方法可以确保用户只能输入最大小数位数的数字.我不知道如何解决这个问题.在MS SQL数据库中我将​​从我的应用程序发送数据我已经有这种类型的列decimal(8,3) 现在考虑最终将存储我要在Android中验证的值的列的数据类型,我考虑过这些两种情况:

  1. 如果用户输入没有小数的数字,则最大位数必须为8
  2. 如果用户输入带小数的数字,则最大位数必须为8(包括小数点右边的数字)

现在我肯定第一种情况,但不是第二种情况.保持最大位数固定是否正确(例如,始终为8)?或者我应该考虑允许小数点左边最多8位数,小数点右边3位数?

无论哪种方式,这都是我在Android中尝试过的:

mQuantityEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                String str = mQuantityEditText.getText().toString();
                DecimalFormat format = (DecimalFormat) DecimalFormat
                        .getInstance();
                DecimalFormatSymbols symbols = format.getDecimalFormatSymbols();
                char sep = symbols.getDecimalSeparator();

                int indexOFdec = str.indexOf(sep);

                if (indexOFdec >= 0) {
                    if (str.substring(indexOFdec, str.length() - 1).length() > 3) {                     
                        s.replace(0, s.length(),
                                str.substring(0, str.length() - 1));                        
                    }
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {

            }
        });
Run Code Online (Sandbox Code Playgroud)

即使上面的代码处理最大小数位数.它不限制EditText中允许的总位数.

您是否认为可以帮助我改进我的代码,以便它处理最大小数位数和EditText中允许的总位数(考虑小数点左侧和右侧的数字)

编辑

好吧,现在我正在尝试JoãoSousa建议的内容,这就是我尝试过的内容:

1)我定义了一个实现InputFilter的类

public class NumberInputFilter implements InputFilter {
    private Pattern mPattern;   

    public NumberInputFilter(int precision, int scale) {        
        String pattern="^\\-?(\\d{0," + (precision-scale) + "}|\\d{0," + (precision-scale) + "}\\.\\d{0," + scale + "})$";
        this.mPattern=Pattern.compile(pattern);

    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned destination, int destinationStart, int destinationEnd) {
         if (end > start) {
             // adding: filter   
             // build the resulting text
             String destinationString = destination.toString();
             String resultingTxt = destinationString.substring(0, destinationStart) + source.subSequence(start, end) + destinationString.substring(destinationEnd);
             // return null to accept the input or empty to reject it
             return resultingTxt.matches(this.mPattern.toString()) ? null : "";
         }
         // removing: always accept
         return null;
    }

}
Run Code Online (Sandbox Code Playgroud)

2)试图使用这样的类:

mQuantityEditText.setFilters(new InputFilter[] { new NumberInputFilter(8,3)} );
Run Code Online (Sandbox Code Playgroud)

Joa*_*usa 9

我会在编辑文本中使用正则表达式的力量去过滤器.首先是正则表达式:

^\-?(\d{0,5}|\d{0,5}\.\d{0,3})$
Run Code Online (Sandbox Code Playgroud)

也许有多种方法可以改善这种表达方式,但这确实很有用.

现在只需在edittext中设置一个输入过滤器,如下所示:

final String regex = "^\-?(\d{0,5}|\d{0,5}\.\d{0,3})$";
((EditText)rootView.findViewById(R.id.editText1)).setFilters(new InputFilter[] {
    new InputFilter() {
        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned destination, int destinationStart, int destinationEnd) {
            if (end > start) {
                // adding: filter   
                // build the resulting text
                String destinationString = destination.toString();
                String resultingTxt = destinationString.substring(0, destinationStart) + source.subSequence(start, end) + destinationString.substring(destinationEnd);
                // return null to accept the input or empty to reject it
                return resultingTxt.matches(regex) ? null : "";
            }
            // removing: always accept
            return null;
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

顺便说一句,我刚刚测试了这段代码,它的作用是:

  1. 用户最多可输入8位数字;
  2. 一旦用户输入'.',允许的最大小数为8.

我是否正确理解了您描述的问题?

- 编辑

好的,我快到了.据我了解,小数(8,3)是指最多8位,包括数字到小数点的左边或右边,从-99999.99999999.999.至少这是我从这句话中理解的Standard SQL requires that DECIMAL(5,2) be able to store any value with five digits and two decimals, so values that can be stored in the salary column range from -999.99 to 999.99.即使它来自MySQL文档,MSSQL文档似乎也是如此.