如何让我的CountDown计时器停止滴答?

时间:2014-05-04 18:06:22

标签: android timer countdowntimer numberpicker

我正在使用CountDownTimer和一些NumberPickers在Android中创建一个简单的计时器。现在,当用户点击我的按钮并且计时器没有进入时它将启动,当点击相同的按钮并且计时器倒计时它应该停止。我的问题是它没有停止。我调用CountDownTimer.cancel()但这似乎没有做任何事情,onTick()中的代码仍然继续执行,这意味着NumberPickers仍在减少,就像定时器仍在运行一样。

我不确定这是不是我的代码,或者我只是错误地使用了CountDownTimer。因此,当我调用Cancel()方法时,onTick()事件应该停止触发吗?它们似乎不是这样我怎么能这样做?

我会把我的一些代码放在这里:

我的按钮代码:

@Override
public void onClick(View v) {
    //Calculate total time from NumberPickers in seconds
    long startTime = (numberPicker1.getValue() * 60) * 1000 + numberPicker2.getValue() * 1000;
    Log.i(TAG, "Start time: " + startTime + "");

    //Create CountDownTimer with values from NumberPickers
    countDownTimer = new MyCountDownTimer(startTime, interval);
    text.setText(text.getText() + String.valueOf(startTime / 1000));    //should be removed

    if(!timerHasStarted) {
        countDownTimer.start();
        timerHasStarted = true;

        //Disable the NumberPickers after 'Start' is pressed
        numberPicker1.setEnabled(false);
        numberPicker2.setEnabled(false);

        startButton.setText(R.string.stop);
    } else {
        countDownTimer.cancel();
        //countDownTimer = countDownTimer.pause();
        timerHasStarted = false;

        //Re-enable the NumberPickers after 'Start' is pressed
        numberPicker1.setEnabled(true);
        numberPicker2.setEnabled(true);

        startButton.setText(R.string.restart);
    }
}

我的计时器的onTick()代码:

 @Override
    public void onTick(long millisUntilFinished) {
        text.setText("" + millisUntilFinished / 1000);

        //Decrement the NumberPickers after each second
        if(numberPicker2.getValue() > 0) {
            //When seconds left is greater than 0 decrement seconds
            changeValueByOne(numberPicker2, false);
        }
        else if(numberPicker1.getValue() > 0 && numberPicker2.getValue() <= 0) {
            //Else, if seconds <= 0 and minutes > 0 decrement minutes and reset seconds to 59
            changeValueByOne(numberPicker1, false);
            numberPicker2.setValue(59);
        }
        else if(numberPicker1.getValue() <= 0 && numberPicker2.getValue() <= 0){
            //Else, if both minutes and seconds <= 0 revert back to 0 and finish
            //This should never really happen, but just in case
            numberPicker1.setValue(0);
            numberPicker2.setValue(0);
            Log.i(TAG, "There is a tick when both are 0");
        }

        this.millisUntilFinished = millisUntilFinished;
    }

1 个答案:

答案 0 :(得分:1)

<强>问题:onClick(),您为每次点击创建MyCountDownTimer的新对象,而不检查timerHasStarted的值。

countDownTimer = new MyCountDownTimer(startTime, interval);

然后你在这个新对象上调用cancel()。因此,你的倒数计时器不会停止继续。

<强>解决方案:

仅当MyCountDownTimer的值为false(在if块中)时才创建timerHasStarted对象。

 @Override
 public void onClick(View v) {
    //Calculate total time from NumberPickers in seconds
    long startTime = (numberPicker1.getValue() * 60) * 1000 + numberPicker2.getValue() * 1000;
    Log.i(TAG, "Start time: " + startTime + "");        

    if(!timerHasStarted) {

        //Create CountDownTimer with values from NumberPickers
        countDownTimer = new MyCountDownTimer(startTime, interval); 

        countDownTimer.start();
        timerHasStarted = true;

        //Disable the NumberPickers after 'Start' is pressed
        numberPicker1.setEnabled(false);
        numberPicker2.setEnabled(false);

        startButton.setText(R.string.stop);
    } else {
        countDownTimer.cancel();
        //countDownTimer = countDownTimer.pause();
        timerHasStarted = false;

        //Re-enable the NumberPickers after 'Start' is pressed
        numberPicker1.setEnabled(true);
        numberPicker2.setEnabled(true);

        startButton.setText(R.string.restart);
    }
}