Android 避免2个相互影响的 EditText 死循环

Android两个EditText同时设置TextChangedListener后,会使程序进入死循环,解决方法如下:

etExchangeRateEn = (EditText) findViewById(R.id.etExchangeRateEn);
  etExchangeRateCn = (EditText) findViewById(R.id.etExchangeRateCn);

twEN = new TextWatcher()
  {

@Override
   public void afterTextChanged(Editable s)
   {
    etExchangeRateCn.addTextChangedListener(twCN);
   }

@Override
   public void beforeTextChanged(CharSequence s, int start, int count, int after)
   {

}

@Override
   public void onTextChanged(CharSequence s, int start, int before, int count)
   {

etExchangeRateCn.setText( "CN");

}

};

twCN = new TextWatcher()
  {

@Override
   public void afterTextChanged(Editable s)
   {
    etExchangeRateEn.addTextChangedListener(twEN);

}

@Override
   public void beforeTextChanged(CharSequence s, int start, int count, int after)
   {
   }

@Override
   public void onTextChanged(CharSequence s, int start, int before, int count)
   {

etExchangeRateEn.setText("EN");

}
  };
  etExchangeRateEn.addTextChangedListener(twEN);
  etExchangeRateCn.addTextChangedListener(twCN);

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/e516b3ec7cfad9834ba16a9b6539f9b2.html