如何恢复 CountDownTimer?
How to resume 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() {}
}
已编辑:
在Class中:
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() {}
}
}
已经一年多了
我刚开始学习 Kotlin
我可以成功获得暂停和恢复功能,但是调用 onFinish 函数有很大的滞后
我没有重新创建计时器对象,单击我调用 timer.cancel() 的按钮并将标志设置为 true
再次单击按钮时,我检查标志的值,如果为真,我调用 timer.start() 并将标志设置为 false
我可以使用 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() {}
}
已编辑:
在Class中:
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() {}
}
}
已经一年多了 我刚开始学习 Kotlin 我可以成功获得暂停和恢复功能,但是调用 onFinish 函数有很大的滞后 我没有重新创建计时器对象,单击我调用 timer.cancel() 的按钮并将标志设置为 true 再次单击按钮时,我检查标志的值,如果为真,我调用 timer.start() 并将标志设置为 false