Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Java by (3.9k 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 (46k 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.

Related questions

0 votes
1 answer
asked Sep 9, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
0 votes
1 answer
asked Nov 25, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer

Browse Categories

...