用的EditText分配输入过滤/小数分隔符隐藏在横向模式小数、横向、分配、模式

2023-09-06 14:50:45 作者:淋雨一直走

我面临在我正在写一个应用程序分配了输入过滤一个EditText一个很奇怪的问题。我们的想法是,有一个EditText,只接受十进制数为具有最大的小数点后两位,并根据用户的区域设置小数点分隔符输入。以下是code的相关部分。

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

在我的布局中的EditText

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" >

的实施输入过滤

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 "";
        }
    }
}

而输入过滤的相关活动分配给我的EditText

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

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

的 decimalSeparator 的是从区域设置读取,而的getValue()的只是调用的 findViewById(...)的一些铸造到正确的类型。

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

在我一般执行似乎工作完全像预想的那样,但它显示了一种奇怪的现象。如果已经输入了小数点分隔符和屏幕方向改变为横向模式,它是隐藏的,所有的数字保持可见。只要取向改回肖像模式,它再次示出。我甚至可以进入在横向模式下的小数点分隔符,虽然它不会在EditText上显示,直到设备放回肖像模式。

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.

我可以复制我的Andr​​oid 4.3设备上,并与API级别模拟器18和10取出机器人的行为:数字和/或Android:从EditText上inputType下,也不会改变这种行为,其他的EditText小部件不输入过滤器表现完全正常,所以我想这在某种程度上与该过滤器。

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.

研究几个小时并没有带来什么有用的东西,所以在有些帮助是AP preciated :)如果你需要更多的信息,或code,只是让我知道。

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.

问候,

汉斯

推荐答案

好像你正面临着​​一个已知的问题(的 HTTPS://$c$c.google.com/p/android/issues/detail ID = 2626 ),谷歌忽略了从较长时间修复。

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.

在风景模式下,输入法默认为全屏或技术上,ExtractEditText不兑现的区域设置。

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

您可以通过可能禁用ExtractEditText如下解决这个问题:机器人:imeOptions =flagNoExtractUi

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

 
精彩推荐
图片推荐