如何修改Android CountDownTimer实现计时递增而非递减?
I want to make CountDownTimer count up instead of down. I know I need to modify the
mTimeInMillisvariable, 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
mTimeInMillisvariable 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
CountDownTimerstill runs fromstartTimedown to 0, but instead of usingmillisUntilFinished(remaining time), we calculatestartTime - millisUntilFinishedwhich 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

