2013-10-12 97 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