Java 如何在倒计时中实现暂停恢复方法
嗨,我在工作倒计时概念。在这里,当我单击开始按钮时,倒计时将开始。以及停止也工作良好,但我需要实施暂停和恢复方法在我的倒计时计时器。当我点击暂停按钮时,倒计时将进入暂停状态。当我单击开始按钮时,倒计时将从暂停值开始。对于ex:my countdown是0到30,假设我单击pause按钮,值将是pause ex:25或15或其他值。当我点击开始按钮时,倒计时将从25开始。我希望你们都能理解我的问题,我试过了,但我没有得到任何解决办法,你能帮我吗Java 如何在倒计时中实现暂停恢复方法,java,android,countdowntimer,onresume,onpause,Java,Android,Countdowntimer,Onresume,Onpause,嗨,我在工作倒计时概念。在这里,当我单击开始按钮时,倒计时将开始。以及停止也工作良好,但我需要实施暂停和恢复方法在我的倒计时计时器。当我点击暂停按钮时,倒计时将进入暂停状态。当我单击开始按钮时,倒计时将从暂停值开始。对于ex:my countdown是0到30,假设我单击pause按钮,值将是pause ex:25或15或其他值。当我点击开始按钮时,倒计时将从25开始。我希望你们都能理解我的问题,我试过了,但我没有得到任何解决办法,你能帮我吗 public class MainActivit
public class MainActivity extends ActionBarActivity implements OnClickListener {
private CountDownTimer countDownTimer;
private boolean timerHasStarted = false;
private Button startB;
public TextView text;
private final long startTime = 30 * 1000;
private final long interval = 1 * 1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startB = (Button) this.findViewById(R.id.button);
startB.setOnClickListener((OnClickListener) this);
text = (TextView) this.findViewById(R.id.timer);
countDownTimer = new MyCountDownTimer(startTime, interval);
text.setText(text.getText() + String.valueOf(startTime / 1000));
}
public void onClick(View v) {
if (!timerHasStarted) {
countDownTimer.start();
timerHasStarted = true;
startB.setText("STOP");
} else {
countDownTimer.cancel();
timerHasStarted = false;
startB.setText("RESTART");
}
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
@Override
public void onFinish() {
//text.setText("Time's up!");
countDownTimer.start();
}
@Override
public void onTick(long millisUntilFinished) {
text.setText("" + millisUntilFinished / 1000);
}
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rl" >
<TextView
android:id="@+id/timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:paddingRight="10dip"
android:textSize="50dp" />
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Start" />
</RelativeLayout>
公共类MainActivity扩展了ActionBarActivity实现OnClickListener{
私人倒计时;
私有布尔timerHasStarted=false;
专用按钮startB;
公共文本查看文本;
私人最终长起始时间=30*1000;
专用最终长间隔=1*1000;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startB=(按钮)this.findviewbyd(R.id.Button);
setOnClickListener((OnClickListener)this);
text=(TextView)this.findViewById(R.id.timer);
倒计时=新的倒计时(开始时间,间隔);
text.setText(text.getText()+String.valueOf(startTime/1000));
}
公共void onClick(视图v){
如果(!timerHasStarted){
countDownTimer.start();
TIMERHASSSTARTED=真;
startB.setText(“停止”);
}否则{
倒计时。取消();
timerHasStarted=错误;
startB.setText(“重启”);
}
}
公共类MyCountDownTimer扩展了CountDownTimer{
公共MyCountDownTimer(长启动时间、长间隔){
超级(开始时间、间隔);
}
@凌驾
公共无效onFinish(){
//setText(“时间到了!”);
countDownTimer.start();
}
@凌驾
公共void onTick(长毫秒未完成){
text.setText(“+millisuntiltfinished/1000);
}
}
}
我看到的唯一方法是在调用pause时取消计时器,并在要恢复计时器时创建一个新的计时器,如下所示:
public class MainActivity extends ActionBarActivity implements OnClickListener {
private MyCountDownTimer countDownTimer;
private boolean timerHasStarted = false;
private Button startB;
public TextView text;
private final long startTime = 30 * 1000;
private final long interval = 1 * 1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startB = (Button) this.findViewById(R.id.button);
startB.setOnClickListener(this);
text = (TextView) this.findViewById(R.id.timer);
countDownTimer = new MyCountDownTimer(startTime, interval,text );
// Set starting value
text.setText(text.getText() + String.valueOf(startTime / 1000));
}
public void onClick(View v) {
if (!timerHasStarted) {
// Start or resume counter
countDownTimer = countDownTimer.resume();
timerHasStarted = true;
startB.setText("PAUSE");
} else {
// Pause counter
countDownTimer.pause();
timerHasStarted = false;
startB.setText("RESTART");
}
}
public class MyCountDownTimer extends CountDownTimer
{
private long mRemainingTime = 0;
private long mInterval = 0;
private TextView mtvxToUpdate = null
// Use a textview reference to update text on each tick()
public MyCountDownTimer(long startTime, long interval,TextView txv ) {
super(startTime, interval);
this.mInterval = interval;
this.mRemainingTime = startTime;
this.mtvxToUpdate = txv;
}
@Override
public void onFinish() {
mtvxToUpdate.setText("Time's up!");
}
@Override
public void onTick(long millisUntilFinished) {
mRemainingTime = millisUntilFinished;
mtvxToUpdate.setText("" + millisUntilFinished / 1000);
}
public void pause(){
// Stop current timer
this.cancel();
}
public MyCountDownTimer resume(){
// Create a counter with last saved data (just before pause)
MyCountDownTimer newTimer = new MyCountDownTimer(mRemainingTime,mInterval,mtvxToUpdate);
// Start this new timer that start where old one stop
newTimer.start();
// Return this new timer
return newTimer;
}
}
}
当我点击pause按钮时,数字应该停止,当我点击start按钮时,数字应该从pause值开始。你能解释一下,我到底要做什么,我必须在哪里使用你建议的代码,并告诉我如果我像这样使用countDownTimer.resume()的话,如何实现点击方法@Gaëtan Maisse;以及倒计时;我得到这个错误“类型CountDownTimer的方法resume(),pause()未定义”我可以使用((MyCountDownTimer)CountDownTimer.pause()吗;像这样而不是倒计时;是的,您可以,或者更改:private CountDownTimer CountDownTimer ot private MyCountDownTimer CountDownTimer;@Gaëtan Maisse现在我没有错误,但仍然没有得到正确的输出。倒计时。暂停();timerHasStarted=错误;startB.setText(“重启”);这是暂停功能,对吗?但为什么它会重新启动,我需要暂停当我点击暂停按钮对不起这里startB.setText(“暂停”);而不是startB.setText(“重启”);告诉我我能做什么?@rizwan,不知道为什么会重新启动,我从新的角度用解决方案更新我的答案你能添加你的布局文件吗?@Gaëtan Maisse我更新了我的xml文件检查它