歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

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

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved