【发布时间】:2022-01-09 22:32:13
【问题描述】:
我有一个显示 CountdownTimer 进度的 ProgressBar。 ProgressBar 工作除了当我关闭应用程序并重新启动它。如果我这样做,ProgressBar 从零开始。它不会从我离开的地方继续。
我有一个变量来保持以毫秒为单位的倒计时。我设法通过在 OnStop 和 OnStart 方法中使用 SharedPreferences 来正确维护它。但是如何正确使用这个变量来维护 ProgressBar 的进度呢?
private fun startCountdown() {
var i = 1
object : CountDownTimer(timeLeftInMilliseconds, 1000) {
override fun onTick(millisUntilFinished: Long) {
timeLeftInMilliseconds = millisUntilFinished
i++
myProgressBar.progress = (i * 100 / (40000 / 1000)).toInt()
//Instead of 40000 here, I have tried to use the variable
//timeLeftInMilliSeconds or millisUntilFinished, but that doesn't work.
}
override fun onFinish() {
progressBarOutOfLikesTimer.progress = 100;
timeLeftInMilliseconds = 40000
}
}
【问题讨论】:
-
显示更多代码,例如您提到的 onStart 和 onStop 函数。我的猜测是你的 onStop 方法没有被调用。
标签: android kotlin progress-bar countdowntimer android-progressbar