EditText with InputFilter assigned / Decimal separ

2019-06-06 23:32发布

I'm facing a very strange problem with an EditText with assigned InputFilter in an application I'm writing. The idea is, to have an EditText, which only accepts decimal numbers as input with a maximum of two decimal places and a decimal separator according to the user's locale settings. Here are the relevant pieces of code

The EditText in my layout

 <EditText
        android:id="@+id/value"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:digits="01234567890,."
        android:inputType="number|numberDecimal"
        android:nextFocusForward="@+id/category" >

The InputFilter implementation

public class MoneyFilter implements InputFilter {
    private String testRegex;

    public MoneyFilter(String decimalSep) {
        testRegex = "^\\d+\\" + decimalSep + "?\\d{0,2}$";
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end,
        Spanned dest, int dstart, int dend) {
        String s = source.toString();
        String d = dest.toString();

        String r = d.substring(0, dstart) + s.substring(start, end)
            + d.substring(dend);

        if (r.matches(testRegex)) {
            return null;
        } else {
            return "";
        }
    }
}

And the assignment of the InputFilter to my EditText in the related Activity

getValue().setFilters(
            new InputFilter[] { new MoneyFilter(decimalSeperator) });

decimalSeparator is read from the locale settings, and getValue() just calls findViewById(...) with some casting to the correct type.

In general my implementation seems to work exactly as supposed, but it shows one strange behaviour. When the decimal separator is already entered and the screen orientation is changed to landscape mode, it is hidden, all numbers stay visible. As soon as the orientation is changed back to portrait mode, it is shown again. I can even enter the decimal separator in landscape mode, although it will not be shown in the EditText, until the device is put back to portrait mode.

I could reproduce the behavior on my Android 4.3 device and in the emulator with API Level 18 and 10. Removing android:digits and/or android:inputType from the EditText, won't change this behavior, other EditText widgets without InputFilter behave completely normal, so I guess it's somehow related to that filter.

Several hours of research didn't bring anything useful up, so some help is appreciated :) If you need more information, or code, just let me know.

regards,

Hans

1条回答
姐就是有狂的资本
2楼-- · 2019-06-06 23:38

Seems like you are facing a known issue (https://code.google.com/p/android/issues/detail?id=2626) that Google is neglecting to fix from long time.

In Landscape mode, IME defaults to full screen or technically, ExtractEditText which doesn't honor the locale settings.

You can possibly resolve this issue by disabling ExtractEditText as follows : android:imeOptions="flagNoExtractUi"

查看更多
登录 后发表回答