Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Infinite loop error EditText

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?

like image 776
Twinsens Avatar asked Apr 27 '13 23:04

Twinsens


2 Answers

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

}
like image 164
Hoan Nguyen Avatar answered Oct 18 '22 13:10

Hoan Nguyen


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...

like image 38
Twinsens Avatar answered Oct 18 '22 13:10

Twinsens