android on Text Change Listener

I have a situation, where there are two fields. field1 and field2. All I want
to do is empty field2 when field1 is changed and vice versa. So at the end only
one field has content on it.

field1 = (EditText)findViewById(R.id.field1);
field2 = (EditText)findViewById(R.id.field2);

field1.addTextChangedListener(new TextWatcher() {

   public void afterTextChanged(Editable s) {}

   public void beforeTextChanged(CharSequence s, int start,
     int count, int after) {
   }

   public void onTextChanged(CharSequence s, int start,
     int before, int count) {
      field2.setText("");
   }
  });

field2.addTextChangedListener(new TextWatcher() {

   public void afterTextChanged(Editable s) {}

   public void beforeTextChanged(CharSequence s, int start,
     int count, int after) {
   }

   public void onTextChanged(CharSequence s, int start,
     int before, int count) {
     field1.setText("");
   }
  });

It works fine if I attach addTextChangedListener to field1 only, but when
I do it for both fields the app crashes. Obviously because they try to change
each other indefinitely. Once field1 changes it clears field2 at this moment
field2 is changed so it will clear field1 and so on…

Can someone suggest any solution?

14 Answers
14

Leave a Comment