我试图在EditText上添加一些TextWatchers,但我应该在需要时删除它们。
...
@BindView(R.id.widget_search_edittext)
MaterialEditText mEditText;
private List<TextWatcher> textWatcherList;
...
public void addTextChangedListener(TextWatcher watcher) {
textWatcherList.add(watcher);
mEditText.addTextChangedListener(watcher);
}
public void removeAllTextChangedListeners() {
for (TextWatcher watcher : textWatcherList) {
mEditText.removeTextChangedListener(watcher);
}
textWatcherList.clear();
}
但是没有任何意义吗?
答案 0 :(得分:1)
确保在扩展的TextWather类中覆盖equals()
。因为存储在arrayList abd中的观察者通过调用依赖于equals函数的arraylist.indexOf(watcher)
来移除。
public void addTextChangedListener(TextWatcher watcher) {
if (mListeners == null) {
mListeners = new ArrayList<TextWatcher>();
}
mListeners.add(watcher);
}
/**
* Removes the specified TextWatcher from the list of those whose
* methods are called
* whenever this TextView's text changes.
*/
public void removeTextChangedListener(TextWatcher watcher) {
if (mListeners != null) {
int i = mListeners.indexOf(watcher);
if (i >= 0) {
mListeners.remove(i);
}
}
}
了解textView如何在内部删除Watcher