我的计时器每次重新开始时触发得越来越快。

huangapple go评论85阅读模式
英文:

My timer start triggering faster and faster every time I start it again

问题

这似乎是多个定时器同时运行的情况。每次点击特定按钮时,我调用这个 realTimer 函数。我该如何解决?

以下是代码部分:

public void realTimer(){
    running = true;
    final Handler handler = new Handler();
    handler.post(new Runnable() {
        @Override
        public void run() {
            int hours = counter/3600;
            int minutes = (counter%3600)/60;
            int secs = counter%60;

            String time = String.format("%d:%02d:%02d", hours, minutes, secs);
            timerTextView.setText(time);

            if(running){
                counter++;
             }
            handler.postDelayed(this, 1000);
        }

    });
}
英文:

It seems like there are multiple timers running together. I‘m calling this realTimer function each time a certain button is clicked. How do I solve it?

this is the code:

public void realTimer(){
        running = true;
        final Handler handler = new Handler();
        handler.post(new Runnable() {
            @Override
            public void run() {
                int hours = counter/3600;
                int minutes = (counter%3600)/60;
                int secs = counter%60;

                String time = String.format("%d:%02d:%02d", hours, minutes, secs);
                timerTextView.setText(time);

                if(running){
                    counter++;
                 }
                handler.postDelayed(this, 1000);
                }

        });
}

答案1

得分: 1

我认为代码应该像这样:

// 顶层代码
final Handler handler = new Handler();
// ... 其他函数放在这里
public void realTimer(){
    running = true;
    counter = 0;
    handler.removeCallbacksAndMessages(null);

    Runnable callback =  new Runnable() {
        @Override
        public void run() {
            int hours = counter/3600;
            int minutes = (counter%3600)/60;
            int secs = counter%60;

            String time = String.format("%d:%02d:%02d", hours, minutes, secs);
            timerTextView.setText(time);

            if(running){
                counter++;
            }
            handler.postDelayed(this, 1000);
        }

    };

    handler.post(callback);
}

每次调用realTime()函数时,计数器被设置为0,并且从处理程序中移除回调。

英文:

I think the code should be like this :

    //top level code
    final Handler handler = new Handler();
    //... other functions goes here
    public void realTimer(){
        running = true;
        counter = 0;
        handler.removeCallbacksAndMessages(null);

        Runnable callback =  new Runnable() {
            @Override
            public void run() {
                int hours = counter/3600;
                int minutes = (counter%3600)/60;
                int secs = counter%60;

                String time = String.format("%d:%02d:%02d", hours, minutes, secs);
                timerTextView.setText(time);

                if(running){
                    counter++;
                }
                handler.postDelayed(this, 1000);
            }

        };

        handler.post(callback);

}

The counter is set to 0 each time realTime() function is called, and callbacks are removed from the handler

huangapple
  • 本文由 发表于 2020年4月8日 03:37:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/61088082.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定