android - 倒数计时器没有取消

时间:2017-01-04 16:01:52

标签: java android

在我的应用中,当用户在5秒内点击一个按钮时,该用户就会得到一个点。之后,应取消计时器并重新启动。我遇到的问题是计时器计数倒计时直到它重新启动。是否设置错误,或者cancel();根本没有停止计时器?

public class MainActivity extends AppCompatActivity {

    Button btn;
    TextView text;
    TextView scoretv;
    private static final String FORMAT = "%02d:%02d";
    public int score = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        scoretv = (TextView) findViewById(R.id.textView3);
        btn = (Button) findViewById(R.id.button1);
        text = (TextView) findViewById(R.id.textView2);
    }


    public void onClick(View view) {
        score++;
        scoretv.setText(String.valueOf(score));
        load();
    }

    private void load() {
        // TODO Auto-generated method stub

        new CountDownTimer(5000, 10) { // adjust the milli
            // seconds here
            public void onTick(long millisUntilFinished) {
                text.setText(""
                        + String.format(
                        "%02d:%03d",
                        TimeUnit.MILLISECONDS
                                .toSeconds(millisUntilFinished)
                                - TimeUnit.MINUTES
                                .toSeconds(TimeUnit.MILLISECONDS
                                        .toMinutes(millisUntilFinished)),
                        TimeUnit.MILLISECONDS
                                .toMillis(millisUntilFinished)
                                - TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS
                                .toSeconds(millisUntilFinished))));

            }

            public void onFinish() {
                text.setText("GameOver.");
                cancel();
            }
        }.start();
    }
}

2 个答案:

答案 0 :(得分:3)

由于您在cancel()中呼叫onFinish,因此当用户点击该按钮时,计时器不会停止。相反,该按钮将启动5秒CountDownTimer,并且在计时器结束时,将调用cancel()。但是,当计时器已经完成时取消计时器的重点是什么?

要解决此问题,我建议创建CountDownTimer对象的全局实例,在onCreate方法中对其进行实例化,然后在onClick方法中取消它。 / p>

首先,将其添加到您的全局范围

CountDownTimer timer;

然后,将load方法之前的原始内容添加到onCreate

timer = new CountDownTimer(5000, 10) { // adjust the milli
        // seconds here
        public void onTick(long millisUntilFinished) {
            text.setText(""
                    + String.format(
                    "%02d:%03d",
                    TimeUnit.MILLISECONDS
                            .toSeconds(millisUntilFinished)
                            - TimeUnit.MINUTES
                            .toSeconds(TimeUnit.MILLISECONDS
                                    .toMinutes(millisUntilFinished)),
                    TimeUnit.MILLISECONDS
                            .toMillis(millisUntilFinished)
                            - TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS
                            .toSeconds(millisUntilFinished))));

        }

        public void onFinish() {
            text.setText("GameOver.");
            //cancel(); <-this is redundant
        }
    }.start();

timer.cancel()方法中调用onClick

public void onClick(View view) {
    score++;
    scoretv.setText(String.valueOf(score));
    //load(); <-unnecessary
    timer.cancel();
}

最后,我建议删除load,因为此时此类不必要。

答案 1 :(得分:0)

定义变量

private final long timeLeftInMillis=60000;

创建课程

public void startCountDown() {
    countDownTimer = new CountDownTimer(timeLeftInMillis, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {

            //Edit text set with time remaining
            int seconds = (int) (millisUntilFinished / 1000);
            int minutes = seconds / 60;
            seconds = seconds % 60;
            etime.setText(  String.format("%02d", minutes)
                    + ":" + String.format("%02d", seconds));



        }
        @Override
        public void onFinish() {


        }
    }.start();
}

我在测验应用程序中使用此命令在给出答案时重置计时器。因此,我从用来添加新问题的类中调用了计时器。

private void newQuestion(){
    if (countDownTimer!=null){
        countDownTimer.cancel();
    }

getNextQuestion();
}