EditText infinite loop error

First look at my code:

It is in my work;

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")); } } } 

Purpose: when the user enters a value in the text1 area, I want to call the text2 area. But when the user enters a value in the text1 area, text2 MyTextWatcher starts. There is an infinite loop. How can I solve this problem?

+4
source share
2 answers

I solved my problem. It is really easy. (

just check isFocused () before 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 answers ...

+6
source

When changing text, you need to remove the text message listener.

 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)); } 
+3
source

All Articles