我怎么能在外面停止计时器。就像我点击countdowntimer方法之外的按钮?

时间:2013-01-25 08:57:14

标签: android countdowntimer

如果我点击此方法之外的buttonstop,我怎么能停止倒数计时器? 我正在制作有计时器的游戏,当我点击按钮提交时,如果答案确定答案是正确的,计时器将停止。

请帮帮我们。我需要它到我的项目。

       public void timer1(){
    timer = (TextView)findViewById(R.id.timer);

    final CountDownTimer Counter2 = new CountDownTimer(120000 , 1000) {

        public void onTick(long millisUntilFinished) {
            timer.setText( formatTime(millisUntilFinished));
        }

        public void onFinish() {
            timer.setText("GAME OVER");

        }
        };


        Context mcontext=this; 

    final AlertDialog.Builder alert = new AlertDialog.Builder(
          mcontext);

  alert.setTitle("LEVEL 1");
  alert.setPositiveButton("Start",
          new DialogInterface.OnClickListener() {

              @Override
              public void onClick(DialogInterface dialog,
                   int which) {
                  dialog.cancel();
                  Counter2.start();

                  question();
              }
          });
      alert.show();
}
public void timer2(){
    timer = (TextView)findViewById(R.id.timer);

    final CountDownTimer Counter1 = new CountDownTimer(120000 , 1000) {

        public void onTick(long millisUntilFinished) {
            timer.setText( formatTime(millisUntilFinished));
        }

        public void onFinish() {
            timer.setText("GAME OVER");

        }
        };


        Context mcontext=this; 

    final AlertDialog.Builder alert = new AlertDialog.Builder(
          mcontext);

  alert.setTitle("CORRECT");
  alert.setPositiveButton("OK",
          new DialogInterface.OnClickListener() {

              @Override
              public void onClick(DialogInterface dialog,
                      int which) {
                  dialog.cancel();
                  Counter1.start();

                  question();
              }
          });
      alert.show();
}   

}

2 个答案:

答案 0 :(得分:2)

在按钮内使用Counter2.cancel点击...

答案 1 :(得分:1)

timer.cancel();

以上代码方式来停止你的计时器。