在注冊(cè)獲取驗(yàn)證碼時(shí)候需要一個(gè)倒計(jì)時(shí)按鈕钞澳。
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ā)送驗(yàn)證碼");
}
};
@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 總時(shí)長(zhǎng)所禀, countDownInterval 時(shí)間間隔
public final synchronized void cancel () // 取消倒計(jì)時(shí)
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)部實(shí)現(xiàn)是通過Handler發(fā)送消息劲室;
public abstract class CountDownTimer {
/**
* Millis since epoch when alarm should stop.
執(zhí)行的總時(shí)間
*/
private final long mMillisInFuture;
/**
* The interval in millis that the user receives callbacks
時(shí)間間隔
*/
private final long mCountdownInterval;
// 停止時(shí)間
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ù)幕帆,總時(shí)間,時(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;
}
// 停止時(shí)間 = 系統(tǒng)啟動(dòng)時(shí)間 + 總計(jì)時(shí)間
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) {
// 計(jì)算剩余總時(shí)間
final long millisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime();
// 小于等于 0 虑粥,回調(diào) onFinish
if (millisLeft <= 0) {
onFinish();
} else if (millisLeft < mCountdownInterval) { // 小于計(jì)時(shí)間隔 如孝,delayed 一個(gè)消息
// 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);
}
}
}
};
}