倒数计时器问题

发布于 2024-11-07 05:02:55 字数 952 浏览 2 评论 0原文

我正在使用这个 Android 类来实现倒计时:http://developer.android。 com/reference/android/os/CountDownTimer.html

我创建了一个自己的类 My Count:

public class MyCount extends CountDownTimer{

public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}

@Override
public void onFinish() {
tv_test.setText("");
}

@Override
public void onTick(long millisUntilFinished) {
tv_test.setText("d"+ (millisUntilFinished/(3600000*24))+ " h: "+  (millisUntilFinished/3600000)%24  +" m: " +  (millisUntilFinished/60000) % 60 + " sec: "+   (millisUntilFinished/1000) % 60);

}

}

在我的活动的 onStart() 中,我有以下代码:

MyCount counter = new MyCount(time, 1000);
        counter.start();

现在的问题是:如果我导航到下一个活动并返回,倒计时开始再次进行两次倒计时。

我不能将它放在 onCreate() 中,因为时间变量可以由用户更改。他在其他活动中更改了它

I am using this Android class to implement a countdown: http://developer.android.com/reference/android/os/CountDownTimer.html

I have created an own class My Count:

public class MyCount extends CountDownTimer{

public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}

@Override
public void onFinish() {
tv_test.setText("");
}

@Override
public void onTick(long millisUntilFinished) {
tv_test.setText("d"+ (millisUntilFinished/(3600000*24))+ " h: "+  (millisUntilFinished/3600000)%24  +" m: " +  (millisUntilFinished/60000) % 60 + " sec: "+   (millisUntilFinished/1000) % 60);

}

}

in onStart() of my activity I have this code:

MyCount counter = new MyCount(time, 1000);
        counter.start();

The problem is now: If I navigate to next activity and come back, the countdowntimer is started again and there run two countdowns.

I can not put it in onCreate() because the time variable can be changed by the user. He changes it on an other activity

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彩扇题诗 2024-11-14 05:02:55

尝试添加一个布尔值
示例:

booelan counterIsLaunched = false;
boolean timeChanged = false;

在计数器的实现中添加以下内容:

@Override
public void onFinish() { tv_test.setText("");
counterIsLaunched = false;
if(timeChanged) start();
}
@Override
public void onTick(long millisUntilFinished) {
tv_test.setText("d"+ (millisUntilFinished/(3600000*24))+ " h: "+  (millisUntilFinished/3600000)%24  +" m: " +  (millisUntilFinished/60000) % 60 + " sec: "+   (millisUntilFinished/1000) % 60);
counterIsLaunched = true;
if(timeChanged) start();
}

并在 onStart() 中尝试测试变量

if(counter.getCounterIsLaunched() == false ) {
counter.start();
}

,当您更改计时器时,执行如下操作:

counter.getTimeChanged() = true;

希望它对人有帮助:)

try to add a boolean
example :

booelan counterIsLaunched = false;
boolean timeChanged = false;

and in your implementation of the counter add this :

@Override
public void onFinish() { tv_test.setText("");
counterIsLaunched = false;
if(timeChanged) start();
}
@Override
public void onTick(long millisUntilFinished) {
tv_test.setText("d"+ (millisUntilFinished/(3600000*24))+ " h: "+  (millisUntilFinished/3600000)%24  +" m: " +  (millisUntilFinished/60000) % 60 + " sec: "+   (millisUntilFinished/1000) % 60);
counterIsLaunched = true;
if(timeChanged) start();
}

and in your onStart() try to test the variable

if(counter.getCounterIsLaunched() == false ) {
counter.start();
}

and when you changed the timer , do something like this :

counter.getTimeChanged() = true;

hope it helps man :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文