Firstly look my code :
These are in my activity;
EditText text1,text2; (Are defined corretly not problem)
text1.addTextChangedListener(new MyTextWatcher(onePercent));
text2.addTextChangedListener(new MyTextWatcher(twoPercent));
.. ..
.. ..
.. ..
private class MyTextWatcher implements TextWatcher {
private View view;
private MyTextWatcher(View view) {
this.view = view;
}
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2)
{}
public void afterTextChanged(Editable editable) {
String text = editable.toString();
switch(view.getId()){
case R.id.dis_one_percent: (this is text1)
if(!text.equel(""))
text2.setText(Double.toString(text));
break;
case R.id.dis_one_number: (and text2)
if(!text.equel(""))
text1.setText(Double.toString(text+"LOL"));
}
}
}
Goal: when the user enters the some value in the text1 area, I want to trigger text2 area. But when user enters a value to text1 area, text2's MyTextWatcher was triggered. There is an infinite loop. How could I solve this problem?
You need to remove text change listener when you change text.
public void afterTextChanged(Editable editable) {
text1.removeTextChangedListener(onePercent);
text2.removeTextChangedListener(twoPercent);
String text = editable.toString();
switch(view.getId()){
case R.id.dis_one_percent: (this is text1)
if(!text.equel(""))
text2.setText(Double.toString(text));
break;
case R.id.dis_one_number: (and text2)
if(!text.equel(""))
text1.setText(Double.toString(text+"LOL"));
}
text1.addTextChangedListener(new MyTextWatcher(onePercent));
text2.addTextChangedListener(new MyTextWatcher(twoPercent));
}
i solved my problem. Thats really simple. (:
just check the isFocused() before the setText();
case R.id.dis_one_percent: (this is text1)
if(!text.equals(""))
{
if( text1.isFocused())
text2.setText(Double.toString(text));
}
break;
case R.id.dis_one_number: (and text2)
if(!text.equals(""))
{
if( text2.isFocused())
text1.setText(Double.toString(text+"LOL"));
}
Sorry for your time. And thanks for your replies...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With