CountDownTimer不显示00:00

时间:2016-04-12 12:27:41

标签: android

我正在尝试倒计时器我不知道这段代码中有什么不对,但是在00:01到来之后的倒计时在下一秒保持不变..我没有得到00:00。我尝试过很多东西,但没有工作

CDTimer ++>

 public class CDTimer extends CountDownTimer {

        public CDTimer(long startTime, long interval) {
            super(startTime, interval);
        }

        @Override
        public void onTick(long millisUntilFinished) {

            String zero = tv.getText().toString();
            if(millisUntilFinished < 1950){
                tv.setText("00:00");
                }
                long seconds = (millisUntilFinished / 1000);
                long minutes = seconds / 60;
                seconds = seconds % 60;
                tv.setText(String.format("%02d", minutes)
                        + ":" + String.format("%02d", seconds));
            Log.d("Time", String.valueOf(timeRemain));


            timeRemain = millisUntilFinished;

        }

        @Override
        public void onFinish() {
            if (isWarmup) {
                isLow = true;
                isWarmup = false;
                isHigh = false;
                startTime = getSec() * 1000 + 1000 ;
                disp_txt.setText("Low Interval");
                cd = new CDTimer(startTime, interval);
                cd.start();
            } else if (isLow) {
                isLow = false;
                isWarmup = false;
                isHigh = true;
                startTime = getSec() * 1000 + 1000;
                disp_txt.setText("High Interval");
                cd = new CDTimer(startTime, interval);
                cd.start();
            } else if (isHigh) {
                isLow = false;
                isWarmup = true;
                isHigh = false;
                disp_txt.setText("Completed");
                tv.setText("00:00");
                start.setText("START AGAIN");
                btn_chk = 0;
                cd.cancel();

            }


        }
    }

timeSec()方法++&gt;

private int getSec() 
{

        if (isWarmup)
            return warmup;
        else if (isLow)
            return lowint;
        else
            return highint;

    }

4 个答案:

答案 0 :(得分:1)

这是被调用函数的顺序问题。你首先调用tv.setText("00:00");,但接下来,下一个函数覆盖了这个,你只需要做一个else语句:

    @Override
    public void onTick(long millisUntilFinished) {

        String zero = tv.getText().toString();
        if(millisUntilFinished < 1950){
            tv.setText("00:00");
        }else{//put an else statement here
            long seconds = (millisUntilFinished / 1000);
            long minutes = seconds / 60;
            seconds = seconds % 60;
            tv.setText(String.format("%02d", minutes)
                    + ":" + String.format("%02d", seconds));
        Log.d("Time", String.valueOf(timeRemain));
        }

        timeRemain = millisUntilFinished;

    }

你所做的是,你设置了tv.setText("00:00");,但是在下一个毫秒时它会被你的下一个电话覆盖

tv.setText(String.format("%02d", minutes)
                        + ":" + String.format("%02d", seconds));

因为你错过了else语句。这比你的眼睛看得更快:)

答案 1 :(得分:1)

试试这个 只需将counDownInterval更改为100。

     new CountDownTimer(10000, 100) {

                public void onTick(long millisUntilFinished) {
                    Log.d(TAG, "time :" + millisUntilFinished);
                    if(millisUntilFinished <= 100){
                        onFinish();
                    }else {
                        count_down.setText(""+millisUntilFinished / 1000 );
                    }
                }

                public void onFinish() {
                    showCorrect(false);
                }
            }.start();

答案 2 :(得分:0)

if(millisUntilFinished < 1900)

应该是

if(millisUntilFinished < 1000)

答案 3 :(得分:0)

@Override
public void onFinish() {

   tv.setText("00:00");

}