【问题标题】:CountDownTimer in android - how to restart itAndroid中的CountDownTimer - 如何重新启动它
【发布时间】:2013-11-15 09:24:46
【问题描述】:

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

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


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

我创建了一个新柜台,但旧柜台还在继续工作。请帮我解决。

【问题讨论】:

  • 尝试使用cornometer ...你可以esailt重置
  • 你可以: timer.cancel() 然后 timer.start()

标签: android counter


【解决方案1】:

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

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
    }

}

【讨论】:

  • mCountDownTimer.cancel(); // 不重置计数器
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    测验的倒计时

     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();
    

    【讨论】:

    • 请解释你的答案。
    【解决方案4】:

    只需再次调用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()
    【解决方案5】:
    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()
    }
    

    【讨论】:

      猜你喜欢
      • 2022-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多