How to remove all listeners added with addTextChangedListener

前端 未结 11 1594
谎友^
谎友^ 2020-11-27 16:24

I have a ListView where each row has an EditText control. I want to add a TextChangedListener to each row; one that contains extra dat

11条回答
  •  孤街浪徒
    2020-11-27 16:50

    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:

    It's removed! That solved my problem!

提交回复
热议问题