英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论