How to remove all listeners added with addTextChangedListener

前端 未结 11 1569
谎友^
谎友^ 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条回答
  •  Happy的楠姐
    2020-11-27 17:07

    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.

提交回复
热议问题