TextWatcher events are being fired multiple times

后端 未结 7 2117
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-05 12:57

I have an annoying problem with TextWatcher. i\'ve been searching the web but couldnt find anything. appreciate if someone could assist me.

For some reason t

7条回答
  •  囚心锁ツ
    2020-12-05 13:26

    u can use a boolean check, like:

        inputBoxNumberEt.addTextChangedListener(new TextWatcher() {
    
            boolean ignoreChange = false;
    
            @Override
            public void afterTextChanged(Editable s) {
            }
    
            @Override
            public void beforeTextChanged(CharSequence s, int start,
                                          int count, int after) {
            }
    
            @Override
            public void onTextChanged(CharSequence s, int start,
                                      int before, int count) {
                if (!ignoreChange) {
                  ///Do your checks                    
                    ignoreChange = true;
                    inputBoxNumberEt.setText(string);
                    inputBoxNumberEt.setSelection(inputBoxNumberEt.getText().length());
                    ignoreChange = false;
                }
            }
        });
    

提交回复
热议问题