如何删除使用 addTextChangedListener 添加的所有侦听器 [英] How to remove all listeners added with 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:
- 重新设计您的应用程序,以便您始终知道将什么
TextWatcher
添加到特定的EditText
实例. - 扩展
EditText
并添加清除所有观察者的可能性.
- Redesign your application so you always know what
TextWatcher
are added to particularEditText
instance. - 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;
}
}
}
这里是如何在 xml 布局中使用 ExtendedEditText
:
And here is how you can use ExtendedEditText
in xml layouts:
<?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屋!