CountDownTimer倒计时

倒计时原来都用handler+timer写,现在发现Android 已将封装好了一个类CountDownTimer

  public CountDownTimer startCount(Button button, int time) {
        //由于CountDownTimer有一定的延迟,所以这里设置3400
        CountDownTimer countDownTimer = new CountDownTimer(time + 400, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                button.setText("跳过(" + millisUntilFinished / 1000 + "s)");
            }

            @Override
            public void onFinish() {
                button.setText("跳过(" + 0 + "s)");
                SplashPresenter.this.mvpView.countEnd();
            }
        }.start();
        return countDownTimer;
    }

cancle在onTick()中是不起作用的,所以可以在外面进行取消

你可能感兴趣的:(Android)