The task is that when the first editText changes, the 2nd one changes, and when the 2nd changes, the first one.

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etNum2 = (EditText) findViewById(R.id.editText2); etNum = (EditText) findViewById(R.id.editText); etNum.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) { if(s.toString().length()>0){ h.removeCallbacksAndMessages(null); // Cancels previous call h.postDelayed(new Runnable() { @Override public void run() { num1 = Float.parseFloat(etNum.getText().toString()); vivod = ((1/Float.parseFloat(r))*Float.parseFloat(k))*num1; String str = String.valueOf(vivod); etNum2.setText(str); } }, 10); // 1 second } else{ } } }); 

So far I have succeeded in doing this only if I try to change the second, it’s just zero in both of them and they cannot be changed in any way. I tried to create a new listner, nothing happened, I hope someone will tell me. thank

  • What would you like?. Change 1 edit and similarly change the second? Describe in more detail, then they will answer you faster and more accurately. - Shwarz Andrei
  • What is Mutual Replace ? - VTT
  • @Pandoxa you need each editText to have a TextWatcher in it and there will be logic. You can make a general depends on the same logic. I think soon you will be given a good and revealing answer. The task is simple. - Shwarz Andrei

1 answer 1

TextWatcher responds to any text changes. If you just hang on both - then they get hung up on, infinitely changing each other and reacting to the same changes.
It is necessary to add conditions when the listener should work, and when not.
For example, check whether our EditText in focus (both cannot be in focus right away):

 etNum.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) { if (!etNum.hasFocus()) return; if (s.toString().length() > 0) { num1 = Float.parseFloat(etNum.getText().toString()); vivod = ((1/Float.parseFloat(r))*Float.parseFloat(k))*num1; String str = String.valueOf(vivod); etNum2.setText(str); } } });