2

i am using EditText. when i use setText(). TextWatcher Events are calling. i don't need to call it? can anyone help me?

        txt_qty.addTextChangedListener(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) {
            }
        });

Thanks.

bhautikmewada191
  • 633
  • 1
  • 8
  • 20

1 Answers1

7

You could unregister the watcher, and then re-register it.

To unregister the watcher use this code:

txt_qty.removeTextChangedListener(yourTextWatcher);

to re-register it use this code:

txt_qty.addTextChangedListener(yourTextWatcher);

Alternatively, you could set a flag so that your watcher knows when you have just changed the text yourself (and therefore should ignore it).

define one flag in your activity is: boolean isSetInitialText = false;

and when you are calling txt_qty.settext(yourText) make isSetInitialText = true before calling set text,

And then update your watcher as:

txt_qty.addTextChangedListener(new TextWatcher() {
            @Override 
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
          if (isSetInitialText){
                isSetInitialText = false;
          } else{
                  // perform your operation
          }

            @Override 
            public void onTextChanged(CharSequence s, int start, int before, int count) {
              if (isSetInitialText){
                   isSetInitialText = false;
               } else{
                 // perform your operation
               }
            } 

            @Override 
            public void afterTextChanged(Editable s) {
                 if (isSetInitialText){
                      isSetInitialText = false;
                 } else{
                     // perform your operation
                 }
            } 
        }); 
Anjali
  • 3,135
  • 1
  • 13
  • 22