本文共 793 字,大约阅读时间需要 2 分钟。
public class TimeCountUtil extends CountDownTimer { private static final int TIME_TASCK = 1000; private Button button; public TimeCountUtil(long millisInFuture, Button view) { //millisInFuture倒计时总时间 super(millisInFuture, TIME_TASCK); button = view; } @Override public void onFinish() {// 计时完毕 button.setText("发送验证码"); button.setClickable(true); } @Override public void onTick(long millisUntilFinished) {// 计时过程 button.setClickable(false);//防止重复点击 button.setText("("+(millisUntilFinished / TIME_TASCK)+")后重新发送"); }}
//每次调用的时候只需要调用start()方法即可mTimeCount.start();
另外,为了节省资源,在Activity销毁时应该停止倒计时:
@Overrideprotected void onDestroy() { super.onDestroy(); mTimeCount.cancel();}
转载地址:http://dcnba.baihongyu.com/