When I try to assign one onTextChanged event to one EditText so that the value from this EditText is written to another EditText, everything works well. But when I do three onTextChanged on each of the three EditText separately to each, the application crashes. What to do? Thank you in advance

package com.romatopotatos.physicalformulas; import android.app.Activity; import android.os.Bundle; import android.widget.EditText; import android.text.TextWatcher; import android.text.Editable; public class FDvigActivity extends Activity { EditText editS; EditText editV; EditText editT; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_f_dvig); editS = (EditText) findViewById(R.id.sEdit); editV = (EditText) findViewById(R.id.vEdit); editT = (EditText) findViewById(R.id.tEdit); editS.addTextChangedListener(new TextWatcher(){ @Override public void beforeTextChanged(CharSequence ch, int i, int i1, int i2){ } @Override public void onTextChanged(CharSequence ch, int i, int i1, int i2){ String s = editS.getText().toString(); editT.setText(s); editV.setText(s); } @Override public void afterTextChanged(Editable ed){ } }); editV.addTextChangedListener(new TextWatcher(){ @Override public void beforeTextChanged(CharSequence ch, int i, int i1, int i2){ } @Override public void onTextChanged(CharSequence ch, int i, int i1, int i2){ String v = editV.getText().toString(); editT.setText(v); editS.setText(v); } @Override public void afterTextChanged(Editable ed){ } }); editS.addTextChangedListener(new TextWatcher(){ @Override public void beforeTextChanged(CharSequence ch, int i, int i1, int i2){ } @Override public void onTextChanged(CharSequence ch, int i, int i1, int i2){ String t = editT.getText().toString(); editS.setText(t); editV.setText(t); } @Override public void afterTextChanged(Editable ed){ } }); } } 

In the application, when you enter a value in one field, the values ​​in the other fields should change. But while I made it so that when you enter a value in one field, the same value appears in other fields. But when you try to enter text, the application does not respond, the on-screen keyboard "stuck" for a while Screenshot test application

  • one
    Attach the logs of the fall, the code, so to speak, is quite difficult what exactly you are doing wrong. There is a suspicion that a looping occurs and you change the value of another EditText from one onTextChanged and onTextChanged is called on the same EditText in a circle. - Nikita Remnev

1 answer 1

Yes, it seems like looping: editV.setText (t) calls the onTextChanged method on editV, which calls onTextChanged on the other two, and exponentially. In addition, you have an error in the code - editS.addTextChangedListener you add twice, the second time you have to add a listener to editT.

It is necessary to add three boolean variables stopV, stopS, stopT to lock the loop. Code checked, works!

 public class MainActivity extends AppCompatActivity { EditText editS; EditText editV; EditText editT; boolean stopV; boolean stopS; boolean stopT; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editS = (EditText) findViewById(R.id.editS); editV = (EditText) findViewById(R.id.editT); editT = (EditText) findViewById(R.id.editV); stopS=true; stopV=true; stopT=true; editS.addTextChangedListener(new TextWatcher(){ @Override public void beforeTextChanged(CharSequence ch, int i, int i1, int i2){ } @Override public void onTextChanged(CharSequence ch, int i, int i1, int i2) { if (stopS) { String sw = editS.getText().toString(); stopT = false; stopV=false; editT.setText(sw); editV.setText(sw); } stopS=true; } @Override public void afterTextChanged(Editable s) { } }); editV.addTextChangedListener(new TextWatcher(){ @Override public void beforeTextChanged(CharSequence ch, int i, int i1, int i2){ } @Override public void onTextChanged(CharSequence ch, int i, int i1, int i2) { if (stopV) { stopS = false; stopT = false; String v = editV.getText().toString(); editT.setText(v); editS.setText(v); } stopV=true; } @Override public void afterTextChanged(Editable ed){ } }); editT.addTextChangedListener(new TextWatcher(){ @Override public void beforeTextChanged(CharSequence ch, int i, int i1, int i2){ } @Override public void onTextChanged(CharSequence ch, int i, int i1, int i2){ if (stopT) { stopV=false; stopS=false; String t = editT.getText().toString(); editS.setText(t); editV.setText(t); } stopT=true; } @Override public void afterTextChanged(Editable ed){ } }); } 

Xml:

 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="mihail_lagarnikov.ru.shadowtest1.MainActivity"> <EditText android:id="@+id/editV" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:ems="10" android:inputType="textPersonName" android:text="Name" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/editS" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:ems="10" android:inputType="textPersonName" android:text="Name" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/editV" /> <EditText android:id="@+id/editT" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:ems="10" android:inputType="textPersonName" android:text="Name" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/editS" /> 

  • Mikhail, your method does not help - RomatoPotato
  • Changed the answer, the code checked - it works - Michael
  • Michael, could you show your xml code? Maybe I did something wrong there. - RomatoPotato
  • Added, the only difference we have is the id editText differently named. - Michael
  • Michael, thank you, it works! I understood my mistake. - RomatoPotato