Android Oreo上的TextWatcher重复字符

时间:2018-08-07 18:25:21

标签: android android-edittext android-textwatcher

在我的应用程序中,我有一个带有EditText的片段,该片段实现了textWatcher来设置遮罩...它在27(Oreo)之前的Android上运行良好,但是在Oreo上我注意到有时它无法正常工作...就像叫了两次

即使我只是removeTextChangedListener,settext和addTextChangedListener,有时也会重复一个字符。如果我删除一个字符,它也会发生。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/editText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="textNoSuggestions" />

        <Button
            android:id="@+id/btnSearch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:text="Search" />

    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rvResult"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

xml

08-07 15:22:23.053 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
08-07 15:22:23.093 1850-1850/com.google.android.inputmethod.latin W/GInputConnectionWrapper: Fixing selection indices to 1, 1
08-07 15:22:23.119 1850-1850/com.google.android.inputmethod.latin I/native: input-context-store.cc:113 Using roll back input context operation: [AbortComposing] reason: client [1219] vs decoder [1220]
08-07 15:22:23.897 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
08-07 15:22:23.898 1850-1850/com.google.android.inputmethod.latin I/native: input-context-store.cc:113 Using roll back input context operation: [AbortComposing] reason: client [1226] vs decoder [1227]
08-07 15:22:23.907 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
getTextBeforeCursor on inactive InputConnection
getTextAfterCursor on inactive InputConnection
08-07 15:22:23.910 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
08-07 15:22:23.912 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
08-07 15:22:23.913 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
getTextBeforeCursor on inactive InputConnection
08-07 15:22:23.915 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection
08-07 15:22:23.916 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
08-07 15:22:23.917 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
beginBatchEdit on inactive InputConnection
getTextBeforeCursor on inactive InputConnection
08-07 15:22:23.918 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection
getSelectedText on inactive InputConnection
08-07 15:22:23.919 1850-1850/com.google.android.inputmethod.latin I/native: input-context-store.cc:113 Using roll back input context operation: [AbortComposing] reason: client [1231] vs decoder [1232]
08-07 15:22:23.920 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
08-07 15:22:23.922 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
08-07 15:22:23.923 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
getTextAfterCursor on inactive InputConnection
getSelectedText on inactive InputConnection
endBatchEdit on inactive InputConnection
08-07 15:22:23.926 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: beginBatchEdit on inactive InputConnection
getTextBeforeCursor on inactive InputConnection
getTextAfterCursor on inactive InputConnection
getSelectedText on inactive InputConnection
08-07 15:22:23.927 7158-7158/com.alelo.mobile.gestorauto.dev W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection
08-07 15:22:23.930 1850-1850/com.google.android.inputmethod.latin I/native: input-context-store.cc:113 Using roll back input context operation: [AbortComposing] reason: client [1235] vs decoder [1236]
08-07 15:22:23.949 1850-1850/com.google.android.inputmethod.latin W/GInputConnectionWrapper: Fixing selection indices to 3, 3

示例:  如果我在键盘上按击键,则在edittext randonly上将显示为或屁股

在日志屏幕上,我得到了:

{{1}}

以前有人遇到过这个问题吗?有人曾经遇到过这个问题吗?

1 个答案:

答案 0 :(得分:0)

这是我要在评论中说的一个小例子。

示例

if (etCPF == nul) {
    etCPF = (EditText) findViewById(R.id.editText)
    etCPF.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            etCPF.removeTextChangedListener(this);
            etCPF.setText(charSequence);
            etCPF.setSelection(etCPF.getText().length());
            etCPF.addTextChangedListener(this);
         }

         @Override
         public void afterTextChanged(Editable editable) { }
    });
}

这是为什么,因为我认为我们处于生命周期方法,而不是通过示例旋转Activity,然后您花了很多时间TextWatcher来重复这种方法。因此,这可以解释为什么他们很多TextWatcher