2013-07-18 56 views
0

我在android項目中使用了CountDownTimer,結果發生了什麼,當CountDown處於中途階段時,我的OnFinish()會觸發。CountDownTimer的OnFinish()在計時器倒計時時觸發

下面是代碼:

public void ShowNotice(){ 
     cdt = new CountDownTimer(10000, 1000) { 

      @Override 
      public void onTick(long millisUntilFinished) { 
       // TODO Auto-generated method stub 
       Toast toast = Toast.makeText(con,"Game Starts In :"+String.valueOf(millisUntilFinished/1000), 
         Toast.LENGTH_SHORT); 
       toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0); 
       toast.show(); 
      } 

      @Override 
      public void onFinish() { 
       // TODO Auto-generated method stub 
       RemoveButtonText(); 
      } 
     }; 

     cdt.start(); 
    } 

RemoveButtonText(); //即使當倒計時未結束即吐司顯示「5」,此方法被執行。

注意:cdt被聲明爲一個類型爲CountDownTimer的類中的私有成員變量。

需要幫助:)

回答

0

我解決了這個問題。

public void ShowNotice(){ 
     cdt = new CountDownTimer(20000, 2000) { 

      @Override 
      public void onTick(long millisUntilFinished) { 
       // TODO Auto-generated method stub 
       Toast toast = Toast.makeText(con,"Game Starts In :"+String.valueOf(millisUntilFinished/2000), 
         Toast.LENGTH_SHORT); 
       toast.setGravity(Gravity.BOTTOM, 0, 0); 
       toast.show(); 
      } 

      @Override 
      public void onFinish() { 
       // TODO Auto-generated method stub 
       Toast toast = Toast.makeText(con,"Game Starts Now!", 
         Toast.LENGTH_SHORT); 
       toast.setGravity(Gravity.BOTTOM, 0, 0); 
       toast.show(); 
       RemoveButtonText(); 
      } 
     }; 

     cdt.start(); 
    } 

美中不足的是在得到這個

cdt = new CountDownTimer(20000, 2000) 

權。

謝謝:)