【问题标题】:Class countdown timer doesn't reset班级倒计时不重置
【发布时间】:2018-08-21 18:57:16
【问题描述】:

我有一个班级倒计时计时器,我想重置它,但重置后它不会重新开始。 基本上当计数器到达0(当状态为“完成”时)时,设置x = 1,然后在活动中检查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);
}

使用计时器类的活动:

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

感谢您的帮助。

【问题讨论】:

  • 您在Start() 方法中除了更改状态之外什么也没做
  • 我应该调用初始化吗?
  • 是的,我就是这么想的

标签: java android android-studio countdowntimer


【解决方案1】:

你应该改变你的Reset 方法:

public void Reset() {
    millisInFuture = pls;
    x=0;
    Initialize();
}

【讨论】:

  • 问题出在starting 我猜
  • 看问题,在MainActivity中他没有使用Start()方法
  • 这是另一种使用 Start() 方法的方法。我的回答完全根据问题
  • 是的,我可能是错的,这只是我的假设,我已经对你的答案投了赞成票,已经很高兴编码了
【解决方案2】:

尝试更改您的启动方法,还有一件事我不知道是拼写错误还是发生了什么变化mycounter.Start(); to mycounterup.Start();

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-10
    • 1970-01-01
    • 2020-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多