5

首先看我的代码:

这些在我的活动中;

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


    }



    }
}

目标:当用户在 text1 区域输入一些值时,我想触发 text2 区域。但是当用户在 text1 区域输入值时,会触发 text2 的 MyTextWatcher。有一个无限循环。我该如何解决这个问题?

4

2 回答 2

6

更改文本时需要删除文本更改侦听器。

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

}
于 2013-04-27T23:14:21.160 回答
6

我解决了我的问题。那真的很简单。(:

只需在 setText() 之前检查 isFocused();

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

对不起你的时间。并感谢您的回复...

于 2013-04-27T23:25:48.897 回答