You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Android CountDownTimer实现计时递增而非递减?

How to make CountDownTimer count up instead of down in Kotlin?

I want to make CountDownTimer count up instead of down. I know I need to modify the mTimeInMillis variable, but I don't know the specific modification method and location. Please help. Here's my code:

private var startTime = 100000L 
private var increaseTime = +10000L 
private var mTimer : CountDownTimer? = null 
private var mTimerRunnning = false 
private var mTimeInMillis = startTime 
private fun updateCountDownText(){ 
    var minutes = (mTimeInMillis/1000).toInt()/60 
    var seconds = (mTimeInMillis/1000).toInt()%60 
    var timeFormatted : String = String.format("%02d:%02d", minutes, seconds) 
    tvTime.text = timeFormatted 
} 
private fun startTimer() { 
    if(!mTimerRunnning){ 
        mTimer?.start() 
    } 
    mTimer = object : CountDownTimer(mTimeInMillis,1000){ 
        override fun onTick(millisUntilFinished: Long) { 
            mTimeInMillis= millisUntilFinished 
            mTimeInMillis++ 
            updateCountDownText() 
        } 
    }

How and where should I modify the mTimeInMillis variable to achieve an increasing timer? Thank you.

Great question! The default CountDownTimer is built to count down from a given time to 0, but we can tweak it to count up instead by reversing how we use the millisUntilFinished value and adjusting our variables. Here's how to fix your code step by step:

1. Adjust Variable Initialization

First, reset our tracking variable to start from 0 (since we're counting up from the beginning), and keep startTime as the total duration we want to count up to:

private var startTime = 100000L // Total time we want to count up to (100 seconds here)
private var increaseTime = 10000L // Optional: for adding extra time later
private var mTimer: CountDownTimer? = null
private var mTimerRunning = false
private var mTimeInMillis = 0L // Start counting from 0 instead of startTime

2. Fix the startTimer() Logic

Your original code had a small bug: you tried to start the timer before creating it. We'll reorder the logic, adjust the CountDownTimer to use startTime as the total duration, and calculate elapsed time in the onTick callback instead of using remaining time:

private fun startTimer() {
    if (!mTimerRunning) {
        // Cancel any existing timer first to avoid duplicate timers
        mTimer?.cancel()
        
        mTimer = object : CountDownTimer(startTime, 1000) {
            override fun onTick(millisUntilFinished: Long) {
                // Calculate elapsed time (total time minus remaining time)
                mTimeInMillis = startTime - millisUntilFinished
                updateCountDownText()
            }

            override fun onFinish() {
                mTimerRunning = false
                // Optional: Do something when the timer reaches the target time
                tvTime.text = "Done!"
            }
        }
        
        mTimer?.start()
        mTimerRunning = true
    }
}

3. (Optional) Add Functionality to Increase Timer Duration

If you want to use your increaseTime variable to add extra time while the timer is running, you can add this helper function:

fun addExtraTime() {
    if (mTimerRunning) {
        // Cancel the current timer
        mTimer?.cancel()
        // Increase the total target time
        startTime += increaseTime
        // Restart the timer with the new total duration
        startTimer()
    }
}

Why This Works

  • The CountDownTimer still runs from startTime down to 0, but instead of using millisUntilFinished (remaining time), we calculate startTime - millisUntilFinished which gives us the elapsed time since the timer started—this is our increasing value.
  • We fixed the order of creating and starting the timer, so it actually triggers when you call startTimer().
  • We added a cancel() call before creating a new timer to prevent multiple timers from running simultaneously.

Your updateCountDownText() function doesn't need any changes—it will correctly format the increasing mTimeInMillis value into minutes and seconds.

内容的提问来源于stack exchange,提问作者elpaso

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:36:32