How to allow EditText with only two decimals?

0

I want to make the EditText accept only two decimals. If for example I put 23.89, do not let me add another decimal.

    
asked by Raúl Borgarello 14.01.2018 в 06:33
source

1 answer

1

what you should do is create an InputFilter like this:

editText.setFilters(new InputFilter[]{new InputFilter() {

            DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols();

            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                int indexPoint = dest.toString().indexOf(decimalFormatSymbols.getDecimalSeparator());

                if (indexPoint == -1)
                    return source;

                int decimals = dend - (indexPoint+1);
                return decimals < 2 ? source : "";
            }
        }
        });
    
answered by 14.01.2018 / 07:36
source