CountDownTimer ---实现计数器

1)定义一个类继承CountDownTimer

private class TimeCount extends CountDownTimer {
    public TimeCount(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);//参数依次为总时长,和计时的时间间隔
    }

    @Override
    public void onFinish() {//计时完毕时触发
        getBtn.setText("重新验证");
        getBtn.setClickable(true);
    }

    @Override
    public void onTick(long millisUntilFinished) {//计时过程显示
        getBtn.setClickable(false);
        getBtn.setText(millisUntilFinished / 1000 + "秒");
    }
}

2)实例化该类,并调用start方法

private TimeCount time;
time.start();//开始计时

你可能感兴趣的:(android)