Class 倒数计时器未重置
Class countdown timer doesn't reset
我有一个 class 倒数计时器,我想重置它,但是当它重置时它不会再次启动。
基本上当计数器到达 0
(当状态为 "done" 时),设置 x = 1
然后在 activity 检查是否 x = 1
,然后计数器重置。当调用 class 中的重置方法时,它显示它已重置但不会再次开始计数
定时器class:
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 使用计时器 class:
mycounterup = new countdown_timer(startcard, 1000);
xup = mycounterup.GetNumberX();
if (xup == 1) {
mycounterup.Reset();
mycounterup.Start();
感谢您的帮助。
你应该改变你的Reset
方法:
public void Reset() {
millisInFuture = pls;
x=0;
Initialize();
}
尝试改变你的启动方法,还有一件事我不知道它是一个错字还是什么改变mycounter.Start();
到 mycounterup.Start();
public void Start() {
status = true;
Initialize();
}
保存 counter 状态,这样下次如果您必须暂停或恢复操作,您也可以这样做
我有一个 class 倒数计时器,我想重置它,但是当它重置时它不会再次启动。
基本上当计数器到达 0
(当状态为 "done" 时),设置 x = 1
然后在 activity 检查是否 x = 1
,然后计数器重置。当调用 class 中的重置方法时,它显示它已重置但不会再次开始计数
定时器class:
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 使用计时器 class:
mycounterup = new countdown_timer(startcard, 1000);
xup = mycounterup.GetNumberX();
if (xup == 1) {
mycounterup.Reset();
mycounterup.Start();
感谢您的帮助。
你应该改变你的Reset
方法:
public void Reset() {
millisInFuture = pls;
x=0;
Initialize();
}
尝试改变你的启动方法,还有一件事我不知道它是一个错字还是什么改变mycounter.Start();
到 mycounterup.Start();
public void Start() {
status = true;
Initialize();
}
保存 counter 状态,这样下次如果您必须暂停或恢复操作,您也可以这样做