简体   繁体   English

Android EditText-不允许单独使用小数点分隔符

[英]Android edittext - do not allow decimal separator alone

I have an Edittext, where I want to allow only numbers and decimal numbers (max 2 decimals after separator eg 125.50). 我有一个Edittext,我只允许数字和十进制数字(分隔符后最多2个小数,例如125.50)。

I implemented a filter for this: 我为此实现了一个过滤器:

 final EditText  field1 = (EditText)findViewById(R.id.field1);
 field1.setFilters(new InputFilter[] { filter });

    InputFilter filter = new InputFilter() {
        final int maxDigitsBeforeDecimalPoint=5;
        final int maxDigitsAfterDecimalPoint=2;

        @Override
        public CharSequence filter(CharSequence source, int start, int end,
                                   Spanned dest, int dstart, int dend) {
            StringBuilder builder = new StringBuilder(dest);
            builder.replace(dstart, dend, source
                    .subSequence(start, end).toString());
            if (!builder.toString().matches(
                    "(([0-9]{1})([0-9]{0,"+(maxDigitsBeforeDecimalPoint-1)+"})?)?(\\.[0-9]{0,"+maxDigitsAfterDecimalPoint+"})?"

            )) {
                if(source.length()==0)
                    return dest.subSequence(dstart, dend);
                return "";
            }

            return null;

        }
    };

This is working fine , but the problem is, that if user inserts decimal separator ALONE, I've got java.lang.NumberFormatException: For input string: "." 这工作正常 ,但是问题是,如果用户插入小数点分隔符ALONE,则将出现java.lang.NumberFormatException:对于输入字符串:“。”

In my ontextChanged I tried this: 在我的ontextChanged中,我尝试了以下操作:

 if(field1.getText().toString().equals("[.]")){field1.setText(0);}

also this 还有这个

if(field1.getText().toString().equals(".")){field1.setText(0);}

but did not work. 但没有用。

How can I restrict the decimal separator alone, but allow it with the numbers? 如何仅限制小数点分隔符,但允许带数字呢?

If this: 如果这:

if(field1.getText().toString().equals(".")){field1.setText(0);}

is your exact code, then for sure it failed because 0 is not a string, it's an integer considered to be a resource id. 是您的确切代码,然后确定它失败了,因为0不是字符串,它是一个整数,被视为资源ID。
So first try this: 因此,首先尝试:

if(field1.getText().toString().equals(".")){field1.setText("0");}

If it fails again then consider that a NumberFormatException can be resolved by try/catch like this: 如果再次失败,则考虑可以通过try/catch来解决NumberFormatException ,如下所示:

double value = 0.0;
try {
    value = Double.parseDouble(field1.getText().toString());
} catch (NumberFormatException e) {
    field1.setText("0");
    e.printStackTrace();
}

For References, you can use this link if you want to use InputFilter: https://stackoverflow.com/a/5368816/10396176 对于参考,如果要使用InputFilter,可以使用此链接: https ://stackoverflow.com/a/5368816/10396176

For other references, if you want using want to try using textWatcher: https://stackoverflow.com/a/16684661/10396176 对于其他参考,如果您想使用,请尝试使用textWatcher: https ://stackoverflow.com/a/16684661/10396176

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM