Reputation: 598
I have 3 EditText named editTextA , editTextB and editTextC . I want to do when text change on either one edit text, the other two will update. t I could done with single addTextChangedListener changing editTextA and updating editTextB. But when I add another addTextChangedListener to editTextB, it do not work. My app look hanged but not crashed. my logcat show something like this many times
Background partial concurrent mark sweep GC freed 23189(905KB) AllocSpace objects, 0(0B) LOS objects, 40% free, 23MB/38MB, paused 28.101ms total 157.246ms
Can I actually use different listener for different editText view change?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputa = (EditText) findViewById(R.id.inputa);
inputb = (EditText) findViewById(R.id.inputb);
editTextA = (EditText) findViewById(R.id.a);
editTextB = (EditText) findViewById(R.id.b);
editTextC = (EditText) findViewById(R.id.c);
editTextA.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) {
String entera = editTextA.getText().toString();
double usera = Double.parseDouble(entera);
String stringofa = inputa.getText().toString();
double a = Double.parseDouble(stringofa);
String stringofb = inputb.getText().toString();
double b = Double.parseDouble(stringofb);
double resultb = usera * b / a;
editTextB.setText(String.format("%.3f", resultb));
double usertotal = usera + resultb;
editTextC.setText(String.format("%.3f", usertotal));
}
@Override
public void afterTextChanged(Editable s) {
}
});
editTextB.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) {
String enterb = editTextB.getText().toString();
double userb = Double.parseDouble(enterb);
String stringofa = inputa.getText().toString();
double a = Double.parseDouble(stringofa);
String stringofb = inputb.getText().toString();
double b = Double.parseDouble(stringofb);
double resulta = userb*a/b;
editTextA.setText(String.format("%.3f", resulta));
double usertotal = userb + resulta;
editTextC.setText(String.format("%.3f",usertotal));
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
Upvotes: 0
Views: 1682
Reputation: 8231
Your code suggests you are creating a recursive loop, whereby changing the text in editTextA
changes the text in editTextB
, which then changes the text in editTextA
, and so on ad infinitum. This eventually result in an out of memory error.
ditTextA.addTextChangedListener(new TextWatcher() {
//...
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//...
editTextB.setText(String.format("%.3f", resultb));
}
//...
editTextB.addTextChangedListener(new TextWatcher() {
//...
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//...
editTextA.setText(String.format("%.3f", resulta);
//...
One solution could involve creating a boolean
switch to ensure that each setText()
is called only if the switch is true
Edit: Verify text has changed before updating.
e1.addTextChangedListener(new TextWatcher() {
//...
@Override
public void afterTextChanged(Editable editable) {
if(!editable.equals(e2.getText())) {
e2.setText(editable);
}
}
});
e2.addTextChangedListener(new TextWatcher() {
//...
@Override
public void afterTextChanged(Editable editable) {
if(!editable.equals(e1.getText())) {
e1.setText(editable);
}
}
});
Upvotes: 1