Android:如何暂停和恢复倒计时器?
我开发了一个倒计时器,但我不知道如何在单击计时器的文本视图时暂停和恢复计时器。单击开始,然后再次单击暂停,要恢复,再次单击计时器的文本视图。
这是我的代码:
Timer = (TextView) this.findViewById(R.id.time); //TIMER
Timer.setOnClickListener(TimerClickListener);
counter = new MyCount(600000, 1000);
}//end of create
private OnClickListener TimerClickListener = new OnClickListener() {
public void onClick(View v) {
updateTimeTask();
}
private void updateTimeTask() {
if (decision == 0) {
counter.start();
decision = 1;
} else if (decision == 2) {
counter.onResume1();
decision = 1;
} else {
counter.onPause1();
decision = 2;
}//end if
}
;
};
class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}//MyCount
public void onResume1() {
onResume();
}
public void onPause1() {
onPause();
}
public void onFinish() {
Timer.setText("00:00");
p1++;
if (p1 <= 4) {
TextView PScore = (TextView) findViewById(R.id.pscore);
PScore.setText(p1 + "");
}//end if
}//finish
public void onTick(long millisUntilFinished) {
Integer milisec = new Integer(new Double(millisUntilFinished).intValue());
Integer cd_secs = milisec / 1000;
Integer minutes = (cd_secs % 3600) / 60;
Integer seconds = (cd_secs % 3600) % 60;
Timer.setText(String.format("%02d", minutes) + ":"
+ String.format("%02d", seconds));
///long timeLeft = millisUntilFinished / 1000;
/}//on tick
}//class MyCount
protected void onResume() {
super.onResume();
//handler.removeCallbacks(updateTimeTask);
//handler.postDelayed(updateTimeTask, 1000);
}//onResume
@Override
protected void onPause() {
super.onPause();
//do stuff
}//onPause
I have developed a Count Down Timer and I am not sure how to pause and resume the timer as the textview for the timer is being clicked. Click to start then click again to pause and to resume, click again the timer's text view.
This is my code:
Timer = (TextView) this.findViewById(R.id.time); //TIMER
Timer.setOnClickListener(TimerClickListener);
counter = new MyCount(600000, 1000);
}//end of create
private OnClickListener TimerClickListener = new OnClickListener() {
public void onClick(View v) {
updateTimeTask();
}
private void updateTimeTask() {
if (decision == 0) {
counter.start();
decision = 1;
} else if (decision == 2) {
counter.onResume1();
decision = 1;
} else {
counter.onPause1();
decision = 2;
}//end if
}
;
};
class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}//MyCount
public void onResume1() {
onResume();
}
public void onPause1() {
onPause();
}
public void onFinish() {
Timer.setText("00:00");
p1++;
if (p1 <= 4) {
TextView PScore = (TextView) findViewById(R.id.pscore);
PScore.setText(p1 + "");
}//end if
}//finish
public void onTick(long millisUntilFinished) {
Integer milisec = new Integer(new Double(millisUntilFinished).intValue());
Integer cd_secs = milisec / 1000;
Integer minutes = (cd_secs % 3600) / 60;
Integer seconds = (cd_secs % 3600) % 60;
Timer.setText(String.format("%02d", minutes) + ":"
+ String.format("%02d", seconds));
///long timeLeft = millisUntilFinished / 1000;
/}//on tick
}//class MyCount
protected void onResume() {
super.onResume();
//handler.removeCallbacks(updateTimeTask);
//handler.postDelayed(updateTimeTask, 1000);
}//onResume
@Override
protected void onPause() {
super.onPause();
//do stuff
}//onPause
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
来源:此要点。
Source : This Gist.
为 CountDownTimer 创建暂停/恢复的一个好方法是为计时器创建一个单独的方法start、pause和resume,如下所示如下所示:
timerStart 有一个很长的参数,因为它将被下面的 resume() 方法重用。请记住存储您的 milliTillFinished (上面为 milliLeft),以便您可以在 resume() 方法中发送它。暂停和恢复方法分别如下:
这是按钮的代码仅供参考:
A nice and simple way to create a Pause/Resume for your CountDownTimer is to create a separate method for your timer start, pause and resume as follows:
The timerStart has a long parameter as it will be reused by the resume() method below. Remember to store your milliTillFinished (above as milliLeft) so that you may send it through in your resume() method. Pause and resume methods below respectively:
Here is the code for the button FYI:
好吧,没有 API 可以暂停或恢复它。您应该做的是
cancel()
计时器并将剩余时间存储在变量中。当再次点击恢复按钮时,使用变量中的值重新启动计时器。您知道您可能会对天文台表感兴趣。
Well there are no API to pause or resume it. What you should do is
cancel()
the timer and store the time remaining in a variable. When the resume button is hit again, restart the timer with the value from the variable.You know Chronometers may be of interest to you.
我在此处编写了一个扩展的 CountDownTimer。让我们尝试一下并留下您的评论:
例如:
I wrote a extended CountDownTimer here. Let's try this and leave your comment:
Eg:
您可以尝试使用我创建的库, Hourglass
使用
hourglass.startTimer(); 启动计时器。
它具有允许暂停和恢复计时器的辅助方法。
和
You can try using a library I created, Hourglass
Use
hourglass.startTimer();
to start the timer.It has helper methods which allow to pause and resume the timer.
AND
我有一个简单的解决方案。您所要做的就是添加一个存储当前时间的额外变量。唯一添加的是顶部和 onTick() 中的 currentMillis。使用cancel()暂停。
PS:我正在使用butterknife库,它用于避免使用findviewbyid和setonclicklisteners。如果您不想使用它,那么您可以使用设置侦听器和 findviewbyid 的基本方法。
}
I have a simple solution. All you have to do is add an extra variable which stores the current time. The only addition is currentMillis at top and in onTick(). Use cancel() to pause.
PS: I'm using butterknife library, it's used to avoid using findviewbyid and setonclicklisteners. If you do not want to use it then you can just use the basic way of setting listeners and findviewbyid.
}
尽管问题不包含 Kotlin 标签,但由于 Android 标签,我仍然认为这个答案属于此处。
我制作了一个非常简单的库,它在内部使用
CountDownTimer
,并受到此答案的启发。到目前为止,它按照我的基本用例预期工作:用法:
源代码:
可恢复CustomTimer.kt(50行)
Although the question doesn't include the Kotlin tag, I still think this answer belongs here due to Android tag.
I've made a very simple library that uses the
CountDownTimer
internally and inspired by this answer. So far, it works as intended with my very basic use case:Usage:
Source code:
Resumable CustomTimer.kt (50 lines)
这是我的代码。我希望这简单而有帮助。
}
Here is my code. I hope this simple and helpful.
}