android - 如何恢复CountDownTimer?

标签 android kotlin countdowntimer

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

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()中:
  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


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() {}
   }
}

关于android - 如何恢复CountDownTimer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58944590/

相关文章:

java - 如何将字符串参数传递到 Android XML 文件中?

在 BillingClient.launchBillingFlow 期间 Android Play 商店崩溃

java - 在回收站 View 中切换到下一个 EditText 时,如何保持软键盘向上?

ios - 快速导航 View Controller 时倒计时计时器不起作用?

javascript - 如何停止和重置我的倒数计时器?

java - 在 Gridview 中更新图像时出现问题

kotlin - Kotlin 中的多维 '3D' 矩阵

android - 属性委托(delegate)必须有一个 'getValue(ChatActivity, KProperty<*>)' 方法。以下功能都不适用 :

scala - Kotlin:花括号围绕几个表达式(或语句)

android - CountDownTimer Android停止当前 Activity 并转到下一个