Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android中的倒计时-如何重新启动它_Android_Counter - Fatal编程技术网

android中的倒计时-如何重新启动它

android中的倒计时-如何重新启动它,android,counter,Android,Counter,我必须重新启动倒计时。我在这里读了很多问题,但没有一个答案对我有帮助。 当我使用以下代码时 if(Const.counter != null){ Const.counter.cancel(); Const.counter = null; } Const.counter = new CustomTimerTask(Const.currentLevel.timeGoal * 1000,1000); Const.counter.start(); 我开了一个新的柜台,但旧的也能继续

我必须重新启动倒计时。我在这里读了很多问题,但没有一个答案对我有帮助。 当我使用以下代码时

if(Const.counter != null){
    Const.counter.cancel();
    Const.counter = null;
}


Const.counter = new CustomTimerTask(Const.currentLevel.timeGoal * 1000,1000);
Const.counter.start();

我开了一个新的柜台,但旧的也能继续工作。请帮我解决它。

您可以通过取消并重新启动来实现它。下面的例子应该有用

CountDownTimer mCountDownTimer = new CountDownTimer(500, 1000) {

    @Override
    public void onTick(long millisUntilFinished) {}

    @Override
    public void onFinish() {
        isCounterRunning = false;
    }
};


boolean isCounterRunning  = false;

private void yourOperation() {
    if( !isCounterRunning ){
        isCounterRunning = true;
        mCountDownTimer.start();
    }
    else{
        mCountDownTimer.cancel(); // cancel
        mCountDownTimer.start();  // then restart
    }

}

我在这里玩了一些不同的把戏。希望这对你有帮助

if (myCountDownTimer != null) {
            myCountDownTimer.cancel();
        }
        myCountDownTimer = new MyCountDownTimer(10000, 500);
        myCountDownTimer.start();

测验计时器

 if(countDownTimer!=null)
            {
                countDownTimer.cancel();
                countDownTimer.start();
                }
            else {
               countDownTimer = new CountDownTimer(30000, 1000) {

                    public void onTick(long l) {
                        mtimer.setText("remaining time" + l / 1000);//mtime is a textview
                    }

                    public void onFinish() {//here mnext is the button from which we can get next question.
                        mnext.performClick();//this is used to perform clik automatically

                    }
                }.start();

只需再次调用
start()
方法:

CountDownTimer cdt = new CountDownTimer(30000, 1000) {

    public void onTick(long millisUntilFinished) {
        mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
    }

    public void onFinish() {
        this.start(); //start again the CountDownTimer
    }
};

尝试使用角计…您可以重置计数器计时器。取消();//不重设抗辩权解释你的答案。这不是一个无休止的循环吗?@SairajSawant是的,但他问如何重新开始。如果您想在其他地方启动它,只需调用
cdt.start()
private fun startTimer() {
    var timeInMilliSeconds = 11000L
    val countDownTimer: CountDownTimer = object : CountDownTimer(timeInMilliSeconds, 1000) {
        override fun onFinish() {
            Timber.d("Times Up!")
            setupResult("")
            this.cancel()
            timeInMilliSeconds = 11000L
            this.start()
        }

        override fun onTick(p0: Long) {
            val seconds = (p0 / 1000) % 60
            Timber.d("Timer: $p0")
            timer?.text = "$seconds"
        }
    }
    countDownTimer.start()
}