如何删除所有监听器添加addTextChangedListener [英] How to remove all listeners added with addTextChangedListener

查看:3674
本文介绍了如何删除所有监听器添加addTextChangedListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ListView ,其中每行有一个的EditText 控制。我想补充一个 TextChangedListener 的每一行;一个包含额外的数据,称该行的EditText 在的问题是, getView 被调用,多 TextWatchers 添加;因为 convertView 已经有一个 TextWatcher (和一个指向不同的行)。

I have a ListView where each row has an EditText control. I want to add a TextChangedListener to each row; one that contains extra data which says which row the EditText was in. The problem is that as getView gets called, multiple TextWatchers are added; because the convertView already having a TextWatcher (and one that points to a different row).

MyTextWatcher watcher = new MyTextWatcher(currentQuestion);
EditText text = (EditText)convertView.findViewById(R.id.responseText);
text.addTextChangedListener(watcher);

MyTextWatcher 是我的类实现 TextWatcher ;并处理文本事件。 CurrentQuestion让我知道哪一行,我在演戏。当我在框中键入; TextWatcher 的多个实例调用。

MyTextWatcher is my class that implements TextWatcher; and handles the text events. CurrentQuestion lets me know which row I'm acting upon. When I type in the box; multiple instances of TextWatcher are called.

有什么办法添加新的之前删除 TextWatchers?我看 removeTextChangedListener 的方法,但是这需要一个特定的 TextWatcher 来传递的,我不知道该怎么得到的指针 TextWatcher 这是已经存在。

Is there any way to remove the TextWatchers before adding the new one? I see the removeTextChangedListener method, but that requires a specific TextWatcher to be passed in, and I don't know how to get the pointer to the TextWatcher that is already there.

推荐答案

有没有办法直接做到这一点使用电流的EditText 接口。我看到两种可能的解决方案:

There is no way to do this using current EditText interface directly. I see two possible solutions:

  1. 重新设计你的应用程序,所以你总是知道 TextWatcher 添加到特定的的EditText 实例。
  2. 扩展的EditText 并添加可能性清除所有观察家。
  1. Redesign your application so you always know what TextWatcher are added to particular EditText instance.
  2. Extend EditText and add possibility to clear all watchers.

下面是第二个方法的一个例子 - ExtendedEditText

Here is an example of second approach - ExtendedEditText:

public class ExtendedEditText extends EditText
{   
    private ArrayList<TextWatcher> mListeners = null;

    public ExtendedEditText(Context ctx)
    {
        super(ctx);
    }

    public ExtendedEditText(Context ctx, AttributeSet attrs)
    {
        super(ctx, attrs);
    }

    public ExtendedEditText(Context ctx, AttributeSet attrs, int defStyle)
    {       
        super(ctx, attrs, defStyle);
    }

    @Override
    public void addTextChangedListener(TextWatcher watcher)
    {       
        if (mListeners == null) 
        {
            mListeners = new ArrayList<TextWatcher>();
        }
        mListeners.add(watcher);

        super.addTextChangedListener(watcher);
    }

    @Override
    public void removeTextChangedListener(TextWatcher watcher)
    {       
        if (mListeners != null) 
        {
            int i = mListeners.indexOf(watcher);
            if (i >= 0) 
            {
                mListeners.remove(i);
            }
        }

        super.removeTextChangedListener(watcher);
    }

    public void clearTextChangedListeners()
    {
        if(mListeners != null)
        {
            for(TextWatcher watcher : mListeners)
            {
                super.removeTextChangedListener(watcher);
            }

            mListeners.clear();
            mListeners = null;
        }
    }
}

这里是如何使用 ExtendedEditText 在XML布局:

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

    <ua.inazaruk.HelloWorld.ExtendedEditText 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:text="header"
        android:gravity="center" /> 

</LinearLayout>

这篇关于如何删除所有监听器添加addTextChangedListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆