在注冊獲取驗證碼時候需要一個倒計時按鈕。
public class MainActivity extends Activity {
TextView txt;
CountDownTimer timer = new CountDownTimer(60000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
txt.setText(millisUntilFinished/1000 + "秒");
}
@Override
public void onFinish() {
txt.setEnabled(true);
txt.setText("發(fā)送驗證碼");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView) findViewById(R.id.txt);
txt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
view.setEnabled(false);
timer.start();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
timer.cancel();
}
}
CountDownTimer
構(gòu)造器、方法
public CountDownTimer (long millisInFuture, long countDownInterval)// millisInFuture 總時長, countDownInterval 時間間隔
public final synchronized void cancel () // 取消倒計時
public abstract void onFinish () //
Callback fired when the time is up.
public abstract void onTick (long millisUntilFinished)
Callback fired on regular interval.
Parameters
millisUntilFinished
The amount of time until finished.
public final synchronized CountDownTimer start ()
源碼
CountDownTimer 內(nèi)部實現(xiàn)是通過Handler發(fā)送消息;
public abstract class CountDownTimer {
/**
* Millis since epoch when alarm should stop.
執(zhí)行的總時間
*/
private final long mMillisInFuture;
/**
* The interval in millis that the user receives callbacks
時間間隔
*/
private final long mCountdownInterval;
// 停止時間
private long mStopTimeInFuture;
/**
* @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.
兩參數(shù)構(gòu)造函數(shù),總時間,時間間隔
*/
public CountDownTimer(long millisInFuture, long countDownInterval) {
mMillisInFuture = millisInFuture;
mCountdownInterval = countDownInterval;
}
/**
* Cancel the countdown.
取消到timer
*/
public final void cancel() {
mHandler.removeMessages(MSG);
}
/**
* Start the countdown.
開始
*/
public synchronized final CountDownTimer start() {
if (mMillisInFuture <= 0) {
onFinish();
return this;
}
// 停止時間 = 系統(tǒng)啟動時間 + 總計時間
mStopTimeInFuture = SystemClock.elapsedRealtime() + mMillisInFuture;
mHandler.sendMessage(mHandler.obtainMessage(MSG));
return this;
}
/**
* Callback fired on regular interval.
* @param millisUntilFinished The amount of time until finished.
*/
public abstract void onTick(long millisUntilFinished);
/**
* Callback fired when the time is up.
*/
public abstract void onFinish();
private static final int MSG = 1;
// handles counting down
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
synchronized (CountDownTimer.this) {
// 計算剩余總時間
final long millisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime();
// 小于等于 0 ,回調(diào) onFinish
if (millisLeft <= 0) {
onFinish();
} else if (millisLeft < mCountdownInterval) { // 小于計時間隔 ,delayed 一個消息
// no tick, just delay until done
sendMessageDelayed(obtainMessage(MSG), millisLeft);
} else {
long lastTickStart = SystemClock.elapsedRealtime();
onTick(millisLeft);
// take into account user's onTick taking time to execute
long delay = lastTickStart + mCountdownInterval - SystemClock.elapsedRealtime();
// special case: user's onTick took more than interval to
// complete, skip to next interval
while (delay < 0) delay += mCountdownInterval;
sendMessageDelayed(obtainMessage(MSG), delay);
}
}
}
};
}