简体   繁体   中英

"," is showing as " .-" in android soft keyboard

I am trying to show "," instead of "." in android soft keyboard for supporting decimal numbers in Europe countries. I tried to add a decimal separator by setting a key listener programmatically. But when I tried to set the separator, it is showing ".-" instead of ".".

How can I show only the dot?

This is how I tried to show the separator.

char separator = DecimalFormatSymbols.getInstance().getDecimalSeparator();
mWeightEditText.setKeyListener(DigitsKeyListener.getInstance("0123456789" + separator));     

If you want to have "," instead of the ".-" you need to set the input type of your editext to

mWeightEditText.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER);

EDIT I forgot to add you the inputFilter

public static InputFilter[] setPriceFilters() {
    return new InputFilter[]{new InputFilter() {
        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            int digitsBeforeZero = 20;
            int digitsAfterZero = 2;
            String res = dest.toString().replace(" ", "");
            if (res.isEmpty() && source.toString().equals(".")) {
                return "";
            } else if (res.contains(",") && source.toString().equals(".")) {
                return "";
            } else if (res.isEmpty() && source.toString().equals("0")){
                return "";
            }
            Matcher matcher = Pattern.compile("[0-9]{0," + (digitsBeforeZero - 1) + "}+((\\,[0-9]{0," + (digitsAfterZero - 1) + "})?)?").matcher(res);
            if (!matcher.matches())
                return "";
            return null;
        }
    }};
}

In my version I have a digit after and before zero check, you can remove it on the matcher if you don't need it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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