首页 > 解决方案 > Android Oreo 上的 TextWatcher 重复字符

问题描述

在我的应用程序中,我有一个带有 EditText 的片段,它实现了一个 textWatcher 来设置掩码......它在 27 (奥利奥)之前运行良好的 Android,但在奥利奥上我注意到有时它不能正常工作......就像它被调用了两次

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

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) { }
});

xml

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

示例:如果我在键盘上按 as,则在 edittext 上随机出现 as 或 ass

在日志屏幕中我得到了这个:

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

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

标签: androidandroid-edittextandroid-textwatcher

解决方案


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

例子

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


推荐阅读