Android CountDownTimer - 在多个计时器运行时添加时间结果 [英] Android CountDownTimer - adding time results in multiple timers running

查看:24
本文介绍了Android CountDownTimer - 在多个计时器运行时添加时间结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力让我的计时器在我每次按下按钮时增加 5 秒.我了解到我需要取消前一个计时器并创建一个新计时器,让它按照我想要的方式工作.当我按下按钮一次时,计时器增加了 5 秒,一切正常.当我多次按下按钮时,我的问题出现了.计时器将在许多不同的计时器之间闪烁,而不是停留在最新的计时器上.每次我按下按钮,另一个计时器在显示屏上闪烁.它几乎就像程序不会取消以前的计时器而只是每次都创建一个新的计时器.我真的很感激这方面的一些帮助.谢谢各位!

Im trying to make it so that my timer adds 5 seconds every time I press the button. I've learnt that I need to cancel the previous timer and create a new one for it to work the way I want it to. When I press the button once, the timer adds 5 seconds and everything works fine as its supposed to. My problem arises when I press the button multiple times. The timer will flicker between many different timers instead of staying on the latest one. Every time I press the button, another timer is flickering on the display. Its almost as if the program doesnt cancel the previous timers and just creates a new every time. I'd really appreciate some help on this. Thanks guys!

import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    int scoreTeamA = 0;
    String countDownTimer;
    long millisUntilFinishedInt = 5000;
    long milliseconds;
    long seconds;
    long totalAddedTime = 0;
    TextView text1;

    MyCount counter = new MyCount(millisUntilFinishedInt + totalAddedTime,17);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text1=(TextView)findViewById(R.id.team_a_score);
        counter.start();
    }

    public class MyCount extends CountDownTimer {

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

        @Override
        public void onTick(long millisUntilFinished) {

            millisUntilFinishedInt = millisUntilFinished;
            seconds = millisUntilFinishedInt/1000;
            milliseconds = millisUntilFinishedInt-(millisUntilFinishedInt/1000)*1000;
            countDownTimer = "TIME: " + seconds + "." + milliseconds ;
           text1.setText(countDownTimer);
        }

        @Override
        public void onFinish() {
            countDownTimer = "TIME'S UP!";
            text1.setText(countDownTimer);
        }
    }

    public void timerCreation (){
        counter.cancel();
        MyCount counter = new MyCount(millisUntilFinishedInt + 5000,1);
        counter.start();
    }

    //method that is called when button is pressed
    public void threePoints (View v) {
        timerCreation();
    } 
}

推荐答案

改变这个

public void timerCreation (){
    counter.cancel();
    MyCount counter = new MyCount(millisUntilFinishedInt + 5000,1);
    counter.start();
}

进入这个

public void timerCreation (){
    counter.cancel();
    counter = new MyCount(millisUntilFinishedInt + 5000,1);
    counter.start();
}

在您当前的实现中,您正在取消您的成员变量 counter.然后创建一个同名的局部变量并启动它.下次您按下按钮时,您的成员变量 counter 再次取消(已取消),以便创建一个新的 MyCount 对象并启动该对象.这就是为什么你会得到多个计时器

With your current implementation, you are cancelling your member variable counter. Then you create a local variable with the same name and start that one. The next time you press your button, your member variable counter gets cancelled again (which is already cancelled) in order to create a new MyCount object and start that one. That is why you are ending up with multiple timers

这篇关于Android CountDownTimer - 在多个计时器运行时添加时间结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆