如何扩展 CountDownTimer 添加暂停和恢复方法?

发布于 2025-01-04 18:37:12 字数 983 浏览 0 评论 0原文

我尝试扩展 ConuntDownTimer 以这种方式添加暂停和恢复方法:

public class CountDown extends CountDownTimer {

    private long resume;
    private long millisInFuture;
    private long countDownInterval;

    public CountDown(long millisInFuture, long countDownInterval) {
        super(millisInFuture,countDownInterval);    
        resume = millisInFuture;
        this.millisInFuture = millisInFuture;
        this.countDownInterval = countDownInterval;
    }

    public void play() {

        // start
        if( millisInFuture == resume ) {
            super.start();

        // restart
        } else {
            CountDown cd = new CountDown(resume, countDownInterval);
            cd.play();    
        }               
    }

    @Override
    public void onTick(long millisUntilFinished) {
        resume = millisUntilFinished;
        // other code

    }
}

问题是播放方法中的“重新启动”,因为通过这种方式,我创建了另一个显示错误秒数的 CountDown 实例,因为几乎有两个事件“打勾”。我能解决这个问题吗? (我希望我的英语能被理解)

I trying to extend ConuntDownTimer to add the methods pause and resume in this way:

public class CountDown extends CountDownTimer {

    private long resume;
    private long millisInFuture;
    private long countDownInterval;

    public CountDown(long millisInFuture, long countDownInterval) {
        super(millisInFuture,countDownInterval);    
        resume = millisInFuture;
        this.millisInFuture = millisInFuture;
        this.countDownInterval = countDownInterval;
    }

    public void play() {

        // start
        if( millisInFuture == resume ) {
            super.start();

        // restart
        } else {
            CountDown cd = new CountDown(resume, countDownInterval);
            cd.play();    
        }               
    }

    @Override
    public void onTick(long millisUntilFinished) {
        resume = millisUntilFinished;
        // other code

    }
}

The problem is "restart" in the play method because, in this way, I created another istance of CountDown that display the wrong seconds because there are almost two event "onTick". Could I solve this problem? (I hope my English was understandable)

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

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

发布评论

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

评论(1

江南烟雨〆相思醉 2025-01-11 18:37:12

我看不出有什么方法可以通过扩展课程来实现您想要的目的。最好自己编写。 这是它的来源。

I don't see any way to do what you want by extending the class. You'll be best served writing your own. Here's the source for it.

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