Android 7.0的onTextChanged TextWatcher问题

问题描述 投票:0回答:1

我的手机刚刚从Android 6.0更新到7.0。更新后,我注意到我的应用程序中的某个功能无法正常工作,也就是说,EditText将不接受输入的字符,而是重复以前输入的字符。控制键盘设置为CapCharacter,因此有帽子。如果我将其设置为大写锁定,它可以正常工作。

以下是相关的代码段

    <EditText
    android:id="@+id/etEntry"
    style="@android:style/Widget.EditText"
    android:digits="cvABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789*?.,^+[](|){}\\~-"
    android:hint="@string/searchterm"
    android:imeOptions="actionSearch|flagForceAscii"
    android:inputType="textCapCharacters|textNoSuggestions"
    android:singleLine="true"
    android:visibility="visible"
    tools:hint="Search Term" />

控件有一个TextWatcher,并且stype值为3(模式),因此它会绕过修改输入的部分。

        private boolean mWasEdited = false;
    @Override
    public void afterTextChanged(Editable s) {
        if (mWasEdited){
            mWasEdited = false;
            return;
        }
        mWasEdited = true;
        String enteredValue  = s.toString();
        if (stype.getSelectedItemPosition() != 3) { // not pattern
            String newValue = enteredValue.replaceAll("[cv0123456789.,^+-]", "");
            int caret = etTerm.getSelectionStart();
            if (stype.getSelectedItemPosition() != 0 && // not anagram
                    stype.getSelectedItemPosition() != 3) { // and not pattern
                newValue = newValue.replaceAll("[*]", "");
            }
            if (Arrays.asList(2,7,8).contains(stype.getSelectedItemPosition())) { // hooks, begins, ends
                newValue = newValue.replaceAll("[?]", "");
            }
            etTerm.setText(newValue);
            etTerm.setSelection(Math.min(newValue.length(), caret)); // if first char is invalid
        }
    }
};

我假设我要么必须配置控件的键盘方面,要做onTextChanged。这是一个谜。

android android-softkeyboard android-textwatcher
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.