如何恢复CountDownTimer? [英] How to resume CountDownTimer?

查看:314
本文介绍了如何恢复CountDownTimer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过使用cancel()(timer.cancel())函数成功停止计时器.但是如何恢复呢?我搜索了很多代码,但所有内容都使用Java.我在Kotlin需要它.你能给我建议吗?我使用代码:

I can successfully stop timer by using cancel() (timer.cancel()) function. But how to resume it? I searched a lot for various codes but everything was in Java. I need it in Kotlin. Can you give me suggestions? I use code:

val timer = object : CountDownTimer(60000, 1000) {
        override fun onTick(millisUntilFinished: Long) {
            textView3.text = (millisUntilFinished / 1000).toString() + ""
            println("Timer  : " + millisUntilFinished / 1000)
        }

        override fun onFinish() {}
    }

课堂上:

var currentMillis: Long = 0 // <-- keep millisUntilFinished

    // First creation of your timer
    var timer = object : CountDownTimer(60000, 1000) {
        override fun onTick(millisUntilFinished: Long) {

            currentMillis = millisUntilFinished // <-- save value

            textView3.text = (millisUntilFinished / 1000).toString() + ""
            println("Timer  : " + millisUntilFinished / 1000)
        }

        override fun onFinish() {}
    }

在onCreate()中:

In onCreate():

  timer.start()

            TextView2.setOnClickListener {
                //Handle click
                timer.cancel()

            }

            TextView3.setOnClickListener {
                //Handle click
                timer = object : CountDownTimer(currentMillis, 1000) {
                    override fun onTick(millisUntilFinished: Long) {
                        currentMillis = millisUntilFinished
                        textView3.text = (millisUntilFinished / 1000).toString() + ""
                        println("Timer  : " + millisUntilFinished / 1000)
                    }

                    override fun onFinish() {}
                }
            timer.start()
}

推荐答案

我的建议:保留millisUntilFinished值并将其用于重新创建CountDownTimer

My suggestion : keep the millisUntilFinished value and use it for recreating CountDownTimer


var currentMillis: Long // <-- keep millisUntilFinished

// First creation of your timer
var timer = object : CountDownTimer(60000, 1000) {
   override fun onTick(millisUntilFinished: Long) {

     currentMillis = millisUntilFinished // <-- save value

     textView3.text = (millisUntilFinished / 1000).toString() + ""
     println("Timer  : " + millisUntilFinished / 1000)
   }

   override fun onFinish() {}
   }
}

...

// You start it
timer.start()

...

// For some reasons in your app you pause (really cancel) it
timer.cancel()

...

// And for reasuming
timer = object : CountDownTimer(currentMillis, 1000) {
   override fun onTick(millisUntilFinished: Long) {
     currentMillis = millisUntilFinished
     textView3.text = (millisUntilFinished / 1000).toString() + ""
     println("Timer  : " + millisUntilFinished / 1000)
   }

   override fun onFinish() {}
   }
}

这篇关于如何恢复CountDownTimer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆