1

如果我在此方法之外单击 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();
}   

}

4

2 回答 2

2

在您的按钮内使用Counter2.cancelonclick ...

于 2013-01-25T09:19:03.353 回答
1
timer.cancel();

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

于 2013-01-25T09:31:25.737 回答