第一步
写内部类
/**
* 获取验证码倒计时设置
*/
private class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
//计时过程
@Override
public void onTick(long l) {
//防止计时过程中重复点击
tv_getyanzhengma.setClickable(false);
tv_getyanzhengma.setText(l / 1000 + "s");
}
//计时完毕的方法
@Override
public void onFinish() {
//重新给Button设置文字
tv_getyanzhengma.setText("重新获取验证码");
//设置可点击
tv_getyanzhengma.setClickable(true);
}
}
第二步
在点击事件中调用即可
//new倒计时对象,总共的时间,每隔多少秒更新一次时间
MyCountDownTimer myCountDownTimer = new MyCountDownTimer(180000, 1000);
myCountDownTimer.start();