如何删除所有添加了 addTextChangedListener 的侦听器

我有一个 ListView,其中每一行有一个 EditText控件。我希望向每一行添加一个 TextChangedListener; 一个包含额外数据的 TextChangedListener,这些数据说明 EditText位于哪一行。问题是,当 getView被调用时,会添加多个 TextWatchers; 因为 convertView已经有了一个 TextWatcher(并且一个 TextWatcher指向另一行)。

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

MyTextWatcher是我的类,它实现 TextWatcher; 并处理文本事件。Currentquestions 让我知道要对哪一行执行操作。当我在框中键入时,会调用多个 TextWatcher实例。

在添加新的 TextWatchers之前有什么方法可以删除 TextWatchers吗?我看到了 removeTextChangedListener方法,但是这需要传入一个特定的 TextWatcher,我不知道如何获得指向已经存在的 TextWatcher的指针。

61638 次浏览

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

  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.

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

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>

As you can see here: CodeSearch of TextView there is no way of removing all listeners. The only way is to provide the watcher you used to register it.

I do not yet fully understand why there are other listeners already registered. However you can subclass the EditText, override the addTextChangedListener(..) and in it keep a copy of all added references yourself and then delegate to the superclass implementation. You then can also provide an additional method that removes all listeners.

Get in touch if you need further explanations.

You can remove TextWatcher from your EditText. First of all I suggest you to move TextWatcher declaration outside the the editText.addTextChangedListener(...):

protected TextWatcher yourTextWatcher = new TextWatcher() {


@Override
public void afterTextChanged(Editable s) {
// your logic here
}


@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// your logic here
}


@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// your logic here
}
};

After that you will be able to set TextWather little bit simpler:

editText.addTextChangedListener(yourTextWatcher);

Than you can remove TextWatcher like this:

editText.removeTextChangedListener(yourTextWatcher);

and set another if you want.

Save the current textwatcher in viewholder and you can find the one you want to remove.

I also spent a lot of time finding the solution and finally ended up solving with the help of tag like below. It would remove previous TextWatcher instances by getting references from tag of the convertView. It perfectly solves the problem. In your CustomAdapter file, set a new inner class like below:

private static class ViewHolder {


private TextChangedListener textChangedListener;
private EditText productQuantity;


public EditText getProductQuantity() {
return productQuantity;
}


public TextChangedListener getTextChangedListener() {
return textChangedListener;
}


public void setTextChangedListener(TextChangedListener textChangedListener) {
this.textChangedListener = textChangedListener;
}
}

Then in your overrided public View getView(int position, View convertView, ViewGroup parent) method implement the logic like below:

@Override
public View getView(int position, View convertView, ViewGroup parent) {


EditText productQuantity;
TextChangedListener textChangedListener;


if(convertView==null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.cart_offer_item, parent, false);


productQuantity=(EditText)convertView.findViewById(R.id.productQuantity);
addTextChangedListener(viewHolder, position);
convertView.setTag(viewHolder);
}
else
{
ViewHolder viewHolder=(ViewHolder)convertView.getTag();
productQuantity=viewHolder.getProductQuantity();
removeTextChangedListener(viewHolder);
addTextChangedListener(viewHolder, position);
}


return convertView;
}






private void removeTextChangedListener(ViewHolder viewHolder)
{
TextChangedListener textChangedListener=viewHolder.getTextChangedListener();
EditText productQuantity=viewHolder.getProductQuantity();
productQuantity.removeTextChangedListener(textChangedListener);
}


private void addTextChangedListener(ViewHolder viewHolder, int position)
{
TextChangedListener textChangedListener=new TextChangedListener(position);
EditText productQuantity=viewHolder.getProductQuantity();
productQuantity.addTextChangedListener(textChangedListener);
viewHolder.setTextChangedListener(textChangedListener);
}

Then implement TextWatcher class as below:

private class TextChangedListener implements TextWatcher
{
private int position;
TextChangedListener(int position)
{
this.position=position;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}


@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
Log.d("check", "text changed in EditText");


}
}

It would remove previous TextWatcher instances by getting references from tag of the convertView

What I did to remove text watchers is very simple. I created an array to put my textwatchers:

final TextWatcher[] textWatchers = new TextWatcher[3];

I added them in:

final int CURRENT_PIN_CHECK = 0, NEW_PIN = 1, CONFIRM_PIN_CHECK = 2;


textWatchers[CURRENT_PIN_CHECK] = returnTextWatcherCheckPIN(CURRENT_PIN_CHECK);
textWatchers[NEW_PIN] = returnTextWatcherCheckPIN(NEW_PIN);
textWatchers[CONFIRM_PIN_CHECK] = returnTextWatcherCheckPIN(CONFIRM_PIN_CHECK);

My returnTextWatcherCheckPIN method instantiates a textWatcher with a different checker (switchMethod to check all four editTexts) on afterTextChanged.

Then whenever I remove a text watcher I just referenced the one from the array:

etPin4.removeTextChangedListener(textWatchers[CURRENT_PIN_CHECK]);

Check the listeners size of the editText on debug:

enter image description here

It's removed! That solved my problem!

I had the same problem with xamarin/C# and I wrote for this a class to manage click events inside a ListView where the item view will be "recycled":

 public class ViewOnClickEventHandler: Java.Lang.Object
{
private List<EventHandler> EventList { get; set; }


public void SetOnClickEventHandler(View view, EventHandler eventHandler)
{
if (view.Tag != null)
{
ViewOnClickEventHandler holder = ((ViewOnClickEventHandler)view.Tag);


foreach (EventHandler evH in holder.EventList)
view.Click -= evH;


for (int i = 0; i < holder.EventList.Count; i++)
holder.EventList[i] = null;


holder.EventList.Clear();
}


EventList = new List<EventHandler>();
EventList.Add(eventHandler);
view.Click += eventHandler;
view.Tag = this;
}
}

You can use it in your ListView BaseAdapter GetItem method this way:

       TextView myTextView = convertView.FindViewById<TextView>(Resource.Id.myTextView);


ViewOnClickEventHandler onClick = new ViewOnClickEventHandler();
onClick.SetOnClickEventHandler(myTextView, new EventHandler(delegate (object sender, EventArgs e)
{
// Do whatever you want with the click event
}));

The ViewOnClickEventHandler class will care about multiple events on your textview. You can also change the class for textchange events. It's the same princip. I hope this will help.

bye, nxexo007

I resolved this situation without extend TextView class.

private ArrayList<TextWatcher> mEditTextWatcherList = new ArrayList<>();
private TextWatcher mTextWatcher1;
private TextWathcer mTextWatcher2;


mTextWathcer1 = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}


@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}


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


mTextWathcer2 = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}


@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}


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


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);


setListener(mTextWatcher1);
setListener(mTextWatcher2);


removeListeners();
}


private setListener(TextWatcher listener) {
mEditText.addTextChangedListener(listener);
mEditTextWatcherList.add(listener);
}


private removeListeners() {
for (TextWatcher t : mEditTextWatcherList)
mEditText.removeTextChangedListener(t);


mEditTextWatcherList.clear();
}

I struggled with a similar problem. I solved it by saving references to my textWatchers in an ArrayList:

private final List<TextWatcher> textWatchersForProfileNameTextBox = new ArrayList<>();


public void addTextWatcherToProfileNameTextBox(TextWatcher textWatcher){
textWatchersForProfileNameTextBox.add(textWatcher);
getProfileNameTextView().addTextChangedListener(textWatcher);
}


public void removeAllTextWatchersFromProfileNameTextView(){
while (!textWatchersForProfileNameTextBox.isEmpty())
getProfileNameTextView().removeTextChangedListener(textWatchersForProfileNameTextBox.remove(0));
}

If one, like me, deals with ViewHolder, then simply saving a reference to a text watcher upon its creation will not help. Upon reuse the view will get to some other ViewHolder which would not have a reference to that old text watcher, thus one won't be able to delete it.

Personally i chose to solve problem like @inazaruk, though updated code to Kotlin + renamed class to better reflect it's purpose.

class EditTextWithRemovableTextWatchers(context: Context?, attrs: AttributeSet?) : TextInputEditText(context, attrs) {


private val listeners by lazy { mutableListOf<TextWatcher>() }


override fun addTextChangedListener(watcher: TextWatcher) {
listeners.add(watcher)
super.addTextChangedListener(watcher)
}


override fun removeTextChangedListener(watcher: TextWatcher) {
listeners.remove(watcher)
super.removeTextChangedListener(watcher)
}


fun clearTextChangedListeners() {
for (watcher in listeners) super.removeTextChangedListener(watcher)
listeners.clear()
}
}

I struggled with a similar problem with a lot of EditTexts in RecyclerView. I solved it by reflection. Call ReflectionTextWatcher.removeAll(your_edittext) before bind views. This piece of code finds all TextWatchers and removes them from the local EditText's list called "mListeners".

public class ReflectionTextWatcher {
public static void removeAll(EditText editText) {
try {
Field field = findField("mListeners", editText.getClass());
if (field != null) {
field.setAccessible(true);
ArrayList<TextWatcher> list = (ArrayList<TextWatcher>) field.get(editText); //IllegalAccessException
if (list != null) {
list.clear();
}
}
} catch (Exception e) {
e.printStackTrace();
}


}


private static Field findField(String name, Class<?> type) {
for (Field declaredField : type.getDeclaredFields()) {
if (declaredField.getName().equals(name)) {
return declaredField;
}
}
if (type.getSuperclass() != null) {
return findField(name, type.getSuperclass());
}
return null;
}
}

I hope, this will help someone.

It has been long since this question was asked, but someone might find this useful. The problem with TextWatcher in Recyclerview is that we have to make sure it is removed before the view is recycled. Otherwise, we loss the instance of the TextWatcher, and calling removeTextChangedListener(textWatcher) in the OnBindViewHolder() will only remove the current instance of TextWatcher.

The way I solve this problem is to add the TextChangedListener inside a FocusChangedListener:

editText.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
editText.addTextChangedListener(textWatcher)
}
else{
editText.removeTextChangedListener(textWatcher)
}
}
});

This way I am sure when the editText doesn't have focus then the textwatcher is removed, and added again when it has focus. So, when the recyclerview is recycled the editText will have any textChangeListener removed.

Why not attach the textwatcher reference to the edittext itself with setTag()

                    if(etTagValue.getTag(R.id.textWatcherTag)!=null){
etTagValue.removeTextChangedListener((TextWatcher) etTagValue.getTag());
}
etTagValue.setText(myValue);
TextWatcher textWatcher = new DelayedTextWatcher(text -> meta.setDescription(text.toString()));
etTagValue.addTextChangedListener(textWatcher);
etTagValue.setTag(R.id.textWatcherTag,textWatcher);

In ids.xml under /values

<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="textWatcherTag" type="id" />
</resources>

I've run into the issue when using editText in viewHolder in recyclerView item, and it was causing error of inifite loop, when viewHolder was binding, cause the textWatcher added in previous bind call was called, hence, neverending loop..

The only working solution for that was to store textWatchers in the list, and then in onBindViewHolder, go trough that list and remove textWatcher from the editText.

    private val textWatchers: MutableList<TextWatcher> = mutableListOf()

add textWatcher to list before assigning it to editText

    textWatchers.add(textWatcher1)
vh.moneyAmount.editText?.addTextChangedListener(textWatcher1)

remove them when binding the item, going to trough the entire textWatcherList.

    private fun removeTextWatcher(vh:MoneyItemViewHolder) {
textWatchers.forEach {vh.moneyAmount.editText?.removeTextChangedListener(it)}
}

There isn't any other way to remove the textWatchers from editText, than passing the textWatcher object, hence it needs to be stored somewhere is we plan to remove it later.