gpt4 book ai didi

java - 类(class)倒数计时器不会重置

转载 作者:行者123 更新时间:2023-11-29 04:20:51 25 4
gpt4 key购买 nike

我有一个类(class)倒数计时器,我想重置它,但是当它重置时它不会再次开始。基本上当计数器到达 0(当状态为“完成”时),设置 x = 1 然后在 Activity 中检查是否 x = 1 ,然后计数器重置。当类中的重置方法被调用时,它显示它已重置但不会再次开始计数

定时器类:

public class countdown_timer {
private long pls;
private long millisInFuture;
private long countDownInterval;
private boolean status;
int x = 0;
public countdown_timer(long pMillisInFuture, long pCountDownInterval) {
this.millisInFuture = pMillisInFuture;
this.countDownInterval = pCountDownInterval;
this.pls = pMillisInFuture;
status = false;
Initialize();
}
public void Stop() {
status = false;
}
public int GetNumberX() {
return x;
}

public void Reset() {
millisInFuture = pls;
x=0;
}
public void Start() {
status = true;
}

public void Initialize() {
final Handler handler = new Handler();
Log.v("status", "starting");
final Runnable counter = new Runnable() {

public void run() {
long sec = millisInFuture / 1000;
if (status) {
if (millisInFuture <= 0) {
Log.v("status", "done");
x = 1;
} else {
Log.v("status", Long.toString(sec) + " seconds remain");
millisInFuture -= countDownInterval;
handler.postDelayed(this, countDownInterval);
}
} else {
Log.v("status", Long.toString(sec) + " seconds remain and timer has stopped!");
handler.postDelayed(this, countDownInterval);
}
}
};

handler.postDelayed(counter, countDownInterval);
}

使用计时器类的 Activity :

mycounterup = new countdown_timer(startcard, 1000);
xup = mycounterup.GetNumberX();
if (xup == 1) {
mycounterup.Reset();
mycounterup.Start();

感谢您的帮助。

最佳答案

尝试改变你的开始方法,还有一件事我不知道它是一个错字还是什么改变了 mycounter.Start(); to mycounterup .开始();

 public void Start() {
status = true;
Initialize();
}

保存counter状态,这样下次如果你必须暂停或恢复你也能做的事情

关于java - 类(class)倒数计时器不会重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49257535/

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