0 votes
1 view
in Java by (10.2k points)

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?

1 Answer

0 votes
by (46.1k points)

You can attach an analysis to entirely clear when the text in the field is not blank (i.e when the length is changed than 0).

field1.addTextChangedListener(new TextWatcher() {

   @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(s.length() != 0)

        field2.setText("");

   }

  });

field2.addTextChangedListener(new TextWatcher() {

   @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(s.length() != 0)

         field1.setText("");

   }

  });

Documentation for TextWatcher here.

Also please recognize naming conventions.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...