2013-10-12 47 views
0

使用android studio我有一個應用程序與一個倒計時器。它從10秒開始到0,當我按下一個按鈕和一個textview來計算我在一個按鈕上做了多少點擊,到目前爲止這麼好。我想通過按另一個按鈕重新啓動這個所有時間。 你能幫我嗎? 如果有幫助,我把代碼。如何重新啓動CountDownTimer

+0

後一些代碼。 – Blackbelt

+0

my timer.cancel() – Broak

回答

0

你可以做這樣的事情:

static CountDownTimer timer; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    [...] 

    btnCount.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View arg0) { 
      // start timer once when button first click 
      if (!timerStarts[0]) { 
       startCounting(10000); 
       [...] 
      } 

      [...] 
     } 
    }); 

    btnRestart.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View arg0) { 
      //RESTART 
      stopCounting(); 
      startCounting(10000); 
     } 
    }); 
} 

private void startCounting(long totalTime) { 
    timer = new CountDownTimer(totalTime, 1) { 
     public void onTick(long millisUntilFinished) { 
      [...] 
     } 

     public void onFinish() { 
      [...] 
     } 
    }; 
    timer.start(); 
} 

private void stopCounting() { 
    timer.cancel(); 
} 
+0

對不起,我不明白。你能寫出完整的代碼嗎? (我的意思是沒有積分) – Rick