本文介绍了CountDownTimer取消()不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid开发,并试图使小游戏。
CountDownTimer.cancel()不是为我工作。

任何想法?

感谢您的回答!

  CountDownTimer CDT =新CountDownTimer(120000,1000){            公共无效onTick(长millisUntilFinished){
                MAXTIME =(int)的(millisUntilFinished / 1000);
                timer.setText(将String.valueOf(MAXTIME));
            }            公共无效onFinish(){            }
        };        如果(startTimer所== TRUE){
            cdt.start();
        }其他{
            cdt.cancel();
        }


解决方案

我要在这里做一个假设,因为在code没有太多表现!显然你正在使用的 countDownTimer 你的的onCreate 作为一个内部类,这样会触发计时器时,<$ C $内C> startTimer所==真,它会不管创建的对象!我想这将是更好的创建 CountDownTimer 的一个全局对象。

和写code是这样的:

 如果(startTimer所==真){
    CDT =新CountDownTimer(120000,1000){
        公共无效onTick(长millisUntilFinished){
            MAXTIME =(int)的(millisUntilFinished / 1000);
            timer.setText(将String.valueOf(MAXTIME));
        }        公共无效onFinish(){        }
    }。开始(); //启动countdowntimer
}
其他{
    cdt.cancel();
}

I am new to Android Development and trying to make little Game.CountDownTimer.cancel() is not working for me.

Any Idea?

Thank Your for your Answer!

CountDownTimer cdt = new CountDownTimer(120000, 1000) {

            public void onTick(long millisUntilFinished) {
                maxTime = (int) (millisUntilFinished / 1000);
                timer.setText(String.valueOf(maxTime));
            }

            public void onFinish() {

            }
        };

        if (startTimer == true) {
            cdt.start();
        } else {
            cdt.cancel();
        }
解决方案

I have to do an assumption right here because the code doesn't show much! apparently you are using the countDownTimer inside your onCreate as an inner class so that will trigger the timer when startTimer == true and it would create the object no matter what! I guess it would be better to create a global object of CountDownTimer.

And write your code in this way:

if(startTimer == true) {
    cdt = new CountDownTimer(120000, 1000) {
        public void onTick(long millisUntilFinished) {
            maxTime = (int) (millisUntilFinished / 1000);
            timer.setText(String.valueOf(maxTime));
        }

        public void onFinish() {

        }
    }.start(); //start the countdowntimer
}
else{
    cdt.cancel();
}

这篇关于CountDownTimer取消()不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 01:06