gpt4 book ai didi

java - 在 Android 中暂停和恢复倒数计时器和进度条?

转载 作者:行者123 更新时间:2023-11-30 01:15:20 24 4
gpt4 key购买 nike

I am making a simple app which uses a count down timer,Circular progress bar and 3 buttons start, Pause and Resume.What I am trying to do is when a particular activity starts and I press pause it stores the time at which the timer was paused and resume from that point forward.But the problem is the count down timmer doesn't stop so if I pause at say 7 sec the progress bar stops at 7sec but when I press resume it starts from whatever the count down timer value is at that moment.This is the code I am trying to implement.

  btnPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//When user request to pause the CountDownTimer
isPaused = true;

//Enable the resume and cancel button
btnResume.setEnabled(true);
//Disable the start and pause button
btnStart.setEnabled(false);
btnPause.setEnabled(false);
}
});



btnResume.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Disable the start and resume button

btnStart.setEnabled(false);
btnResume.setEnabled(false);
//Enable the pause and cancel button
btnPause.setEnabled(true);

//Specify the current state is not paused and canceled.
isPaused = false;
isCanceled = false;
mCountDownTimer.start();
}
});

这是倒数计时器的代码。

 private void neck_rotations()
{
neckRotation = true;
mCountDownTimer = new CountDownTimer(NECK_ROTATION_TIME, 1000)
{
@Override
public void onTick(long millisUntilFinished)
{

if (isPaused || isCanceled)
{
paused_at = (int) (millisUntilFinished / 1000);

} else
{
timeRemaining = (int) (millisUntilFinished / 1000);
donutProgress.setMaxValue(NECK_ROTATION_TIME / 1000);
donutProgress.setValueAnimated((int) timeRemaining);
counter.setText(String.valueOf(timeRemaining));

}

}

@Override
public void onFinish()
{
response = "Jumps";
rest(response);

}
};
mCountDownTimer.start();
}

我是编程新手,所以非常感谢任何帮助或建议。谢谢。

最佳答案

请引用:

How to clear CountDownTimer from onTick method?

我不确定您是否可以通过内部 onTick() 方法禁用计时器继续倒计时。

为 CountDownTimer 制作一个全局引用,并只从该引用的 clickListeners 调用 start() 或 cancel()。

关于java - 在 Android 中暂停和恢复倒数计时器和进度条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37938278/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com