CountDownTimer timer = new CountDownTimer(10000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
tv_yzm.setEnabled(false);
tv_yzm.setText("已发送(" + millisUntilFinished / 1000 + ")");
}
@Override
public void onFinish() {
tv_yzm.setEnabled(true);
tv_yzm.setText("获取验证码");
}
}.start();
@Override
protected void onDestroy() {
super.onDestroy();
if (timer!=null){
timer.cancel();
}
}
详细说明参考:https://blog.csdn.net/weixin_42564441/article/details/80967482