在MainActivity中调用
//执行命令需要传入的控件;倒计时时长为10秒钟
VerifyTime verifyTime=new VerifyTime(verifyButton,timeView,10000,1000);
verifyTime.start();
倒计时工具类
public class VerifyTime extends CountDownTimer {
private TextView textView;
private Button button;
/**
* @param millisInFuture The number of millis in the future from the call
* to {@link #start()} until the countdown is done and {@link #onFinish()}
* is called.
* @param countDownInterval The interval along the way to receive
* {@link #onTick(long)} callbacks.
*/
public VerifyTime(Button button,TextView textView, long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
this.textView=textView;
this.button=button;
}
@Override
public void onTick(long millisUntilFinished) {//倒计时刷新
textView.setClickable(false);
textView.setText(millisUntilFinished / 1000 + "s后重新获取" );
textView.setBackgroundColor(Color.GRAY);
}
@Override
public void onFinish() {//倒计时完成后的操作
button.setVisibility(View.VISIBLE);
textView.setVisibility(View.INVISIBLE);
}
}