首页 > 代码库 > 倒计时的CountDownTimer
倒计时的CountDownTimer
定时执行在一段时候后停止的倒计时,在倒计时执行过程中会在固定间隔时间得到通知(译者:触发onTick方法),下面的例子显示在一个文本框中显示一个30s倒计时:
- new CountdownTimer(30000, 1000) {
- public void onTick(long millisUntilFinished) {
- mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
- }
- public void onFinish() {
- mTextField.setText("done!");
- }
- }.start();
onTick的调用是同步的,保证这次调用不会在之前调用完成前发生。这里的同步机制主要是用来:onTick的实现需要很多时间执行比倒计时间隔更重要的事情。
构造函数
public CountDownTimer (long millisInFuture, long countDownInterval)
参数
millisInFuture 从开始调用start()到倒计时完成并onFinish()方法被调用的毫秒数。(译者注:倒计时时间,单位毫秒)
countDownInterval 接收onTick(long)回调的间隔时间。(译者注:单位毫秒)
公共方法
public final void cancel ()
取消倒计时(译者:取消后,再次启动会重新开始倒计时)
public abstract void onFinish ()
倒计时完成时被调用
public abstract void onTick (long millisUntilFinished)
固定间隔被调用
参数
millisUntilFinished 倒计时剩余时间。
public synchronized final CountDownTimer start ()
启动倒计时
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.CountDownTimer;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- import com.test.R;
- public class CountDownTimeActivity extends Activity implements OnClickListener {
- TextView mTextView;
- Button mButton1;
- Button mButton2;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.countdown);
- mTextView = (TextView)findViewById(R.id.textView1);
- mButton1 = (Button)findViewById(R.id.button1);
- mButton2 = (Button)findViewById(R.id.button2);
- mButton1.setOnClickListener(this);
- mButton2.setOnClickListener(this);
- }
- CountDownTimer timer = new CountDownTimer(40000,1000) {
- @Override
- public void onTick(long millisUntilFinished) {
- mTextView.setText("seconds remaining: " + millisUntilFinished / 1000);
- try {
- Thread.sleep(1200);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- Log.e("CountDown",millisUntilFinished+"");
- }
- @Override
- public void onFinish() {
- mTextView.setText("done!");
- }
- };
- @Override
- public void onClick(View v) {
- switch(v.getId()){
- case R.id.button1:
- timer.start();
- break;
- case R.id.button2:
- timer.cancel();
- break;
- }
- }
- }
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activation);
initView();
resendBTCountDownTimer(resendBtn,false);
}
/**
* 重发验证码按钮倒计时操作
* @param resendBtn
* @param resendCode: false初始化并倒计时开始;true点击次数倒计时开始并重新获取验证码操作
*/
public void resendBTCountDownTimer(final Button resendBtn,boolean resendCode){
if(resendCount>5){
resendBtn.setText(R.string.resentcode_5times_tips);
resendBtn.setEnabled(false);
resendBtn.setBackgroundResource(R.drawable.button_no_cliable);
return;
}else{
new CountDownTimer(MILLIS_INFUTURE, COUNTDOWN_INTERVAL) {
@Override
public void onTick(long millisUntilFinished) {
resendBtn.setEnabled(false);
resendBtn.setBackgroundResource(R.drawable.button_no_cliable);
resendBtn.setText(getString(R.string.regetcodeafter30s, millisUntilFinished/1000));
}
@Override
public void onFinish() {
resendBtn.setText(R.string.resend_code);
resendBtn.setEnabled(true);
resendBtn.setBackgroundResource(R.drawable.registbtn);
}
}.start();
if(resendCode){
resendCount++ ;
regetCheckCodeFun();
}
}
}
http://blog.renren.com/blog/573785236/934703413
http://guangzhou.baixing.com/qitafuwu/a417877410.html
http://club.jd.com/CommunityGroupDetail/13057867_1.html
http://club.jd.com/CommunityGroupDetail/13057868_1.html
http://club.jd.com/CommunityGroupDetail/13057869_1.html
http://club.jd.com/CommunityGroupDetail/13057960_1.html
http://club.jd.com/CommunityGroupDetail/13057986_1.html
http://club.jd.com/CommunityGroupDetail/13057988_1.html
http://club.jd.com/CommunityGroupDetail/13058011_1.html
http://club.jd.com/CommunityGroupDetail/13058019_1.html
http://club.jd.com/CommunityGroupDetail/13058020_1.html
http://club.jd.com/CommunityGroupDetail/13058038_1.html
http://club.jd.com/CommunityGroupDetail/13058040_1.html
http://club.jd.com/CommunityGroupDetail/13058045_1.html
http://club.jd.com/CommunityGroupDetail/13058068_1.html
http://club.jd.com/CommunityGroupDetail/13058074_1.html
http://club.jd.com/CommunityGroupDetail/13058079_1.html
http://club.jd.com/CommunityGroupDetail/13058092_1.html
http://club.jd.com/CommunityGroupDetail/13058093_1.html
http://club.jd.com/CommunityGroupDetail/13058096_1.html
http://club.jd.com/CommunityGroupDetail/13058102_1.html
http://club.jd.com/CommunityGroupDetail/13058104_1.html
http://club.jd.com/CommunityGroupDetail/13058105_1.html
http://club.jd.com/CommunityGroupDetail/13058130_1.html
http://club.jd.com/CommunityGroupDetail/13058132_1.html
http://club.jd.com/CommunityGroupDetail/13058133_1.html
http://club.jd.com/CommunityGroupDetail/13058138_1.html
http://club.jd.com/CommunityGroupDetail/13058139_1.html
http://club.jd.com/CommunityGroupDetail/13058140_1.html
http://club.jd.com/CommunityGroupDetail/13058163_1.html
http://club.jd.com/CommunityGroupDetail/13058165_1.html
http://club.jd.com/CommunityGroupDetail/13058170_1.html
http://club.jd.com/CommunityGroupDetail/13058188_1.html
http://club.jd.com/CommunityGroupDetail/13058210_1.html
http://club.jd.com/CommunityGroupDetail/13058220_1.html
http://club.jd.com/CommunityGroupDetail/13058229_1.html
http://club.jd.com/CommunityGroupDetail/13058230_1.html
http://club.jd.com/CommunityGroupDetail/13058241_1.html
http://club.jd.com/CommunityGroupDetail/13058243_1.html
http://club.jd.com/CommunityGroupDetail/13058244_1.html
http://club.jd.com/CommunityGroupDetail/13058249_1.html
http://club.jd.com/CommunityGroupDetail/13058260_1.html
http://club.jd.com/CommunityGroupDetail/13058266_1.html
http://club.jd.com/CommunityGroupDetail/13058274_1.html
http://club.jd.com/CommunityGroupDetail/13058276_1.html
http://club.jd.com/CommunityGroupDetail/13058278_1.html
http://club.jd.com/CommunityGroupDetail/13058280_1.html
http://club.jd.com/CommunityGroupDetail/13058288_1.html
http://club.jd.com/CommunityGroupDetail/13058295_1.html
http://club.jd.com/CommunityGroupDetail/13058299_1.html
http://club.jd.com/CommunityGroupDetail/13058300_1.html
http://club.jd.com/CommunityGroupDetail/13058301_1.html
http://club.jd.com/CommunityGroupDetail/13058305_1.html
http://club.jd.com/CommunityGroupDetail/13058316_1.html
http://club.jd.com/CommunityGroupDetail/13058320_1.html
http://club.jd.com/CommunityGroupDetail/13058322_1.html
http://club.jd.com/CommunityGroupDetail/13058323_1.html
http://club.jd.com/CommunityGroupDetail/13058330_1.html
http://club.jd.com/CommunityGroupDetail/13058338_1.html
http://club.jd.com/CommunityGroupDetail/13058339_1.html
http://club.jd.com/CommunityGroupDetail/13058344_1.html
http://club.jd.com/CommunityGroupDetail/13058351_1.html
http://club.jd.com/CommunityGroupDetail/13058361_1.html
http://club.jd.com/CommunityGroupDetail/13058363_1.html
http://club.jd.com/CommunityGroupDetail/13058365_1.html
http://club.jd.com/CommunityGroupDetail/13058368_1.html
http://club.jd.com/CommunityGroupDetail/13058376_1.html
http://club.jd.com/CommunityGroupDetail/13058378_1.html
http://club.jd.com/CommunityGroupDetail/13058379_1.html
http://club.jd.com/CommunityGroupDetail/13058381_1.html
http://club.jd.com/CommunityGroupDetail/13058392_1.html
http://club.jd.com/CommunityGroupDetail/13058393_1.html
http://club.jd.com/CommunityGroupDetail/13058395_1.html
http://club.jd.com/CommunityGroupDetail/13058400_1.html
http://club.jd.com/CommunityGroupDetail/13058402_1.html
http://club.jd.com/CommunityGroupDetail/13058408_1.html
http://club.jd.com/CommunityGroupDetail/13058416_1.html
http://club.jd.com/CommunityGroupDetail/13058417_1.html
http://club.jd.com/CommunityGroupDetail/13058429_1.html
http://club.jd.com/CommunityGroupDetail/13058435_1.html
http://club.jd.com/CommunityGroupDetail/13058436_1.html
http://club.jd.com/CommunityGroupDetail/13058441_1.html
http://club.jd.com/CommunityGroupDetail/13058442_1.html
http://club.jd.com/CommunityGroupDetail/13058443_1.html
http://club.jd.com/CommunityGroupDetail/13058450_1.html
http://club.jd.com/CommunityGroupDetail/13058460_1.html
http://club.jd.com/CommunityGroupDetail/13058465_1.html
http://club.jd.com/CommunityGroupDetail/13058466_1.html
http://club.jd.com/CommunityGroupDetail/13058469_1.html
http://club.jd.com/CommunityGroupDetail/13058474_1.html
http://club.jd.com/CommunityGroupDetail/13058475_1.html
http://club.jd.com/CommunityGroupDetail/13058481_1.html
http://club.jd.com/CommunityGroupDetail/13058487_1.html
http://club.jd.com/CommunityGroupDetail/13058491_1.html
http://club.jd.com/CommunityGroupDetail/13058493_1.html
http://club.jd.com/CommunityGroupDetail/13058499_1.html
http://club.jd.com/CommunityGroupDetail/13058500_1.html
http://club.jd.com/CommunityGroupDetail/13058502_1.html
http://club.jd.com/CommunityGroupDetail/13058504_1.html
http://club.jd.com/CommunityGroupDetail/13058506_1.html
http://club.jd.com/CommunityGroupDetail/13058508_1.html
http://club.jd.com/CommunityGroupDetail/13058513_1.html
http://club.jd.com/CommunityGroupDetail/13058524_1.html
http://club.jd.com/CommunityGroupDetail/13058526_1.html
http://club.jd.com/CommunityGroupDetail/13058529_1.html
http://club.jd.com/CommunityGroupDetail/13058536_1.html
http://club.jd.com/CommunityGroupDetail/13058537_1.html
http://club.jd.com/CommunityGroupDetail/13058538_1.html
http://club.jd.com/CommunityGroupDetail/13058539_1.html
http://club.jd.com/CommunityGroupDetail/13058542_1.html
http://club.jd.com/CommunityGroupDetail/13058543_1.html
http://club.jd.com/CommunityGroupDetail/13058544_1.html
http://club.jd.com/CommunityGroupDetail/13058549_1.html
http://club.jd.com/CommunityGroupDetail/13058550_1.html
http://club.jd.com/CommunityGroupDetail/13058555_1.html
http://club.jd.com/CommunityGroupDetail/13058559_1.html
http://club.jd.com/CommunityGroupDetail/13058562_1.html
http://club.jd.com/CommunityGroupDetail/13058565_1.html
http://club.jd.com/CommunityGroupDetail/13058567_1.html
http://club.jd.com/CommunityGroupDetail/13058572_1.html
http://club.jd.com/CommunityGroupDetail/13058573_1.html
http://club.jd.com/CommunityGroupDetail/13058576_1.html
http://club.jd.com/CommunityGroupDetail/13058579_1.html
http://club.jd.com/CommunityGroupDetail/13058580_1.html
http://club.jd.com/CommunityGroupDetail/13058595_1.html
http://club.jd.com/CommunityGroupDetail/13058600_1.html
http://club.jd.com/CommunityGroupDetail/13058601_1.html
http://club.jd.com/CommunityGroupDetail/13058602_1.html
http://club.jd.com/CommunityGroupDetail/13058603_1.html
http://club.jd.com/CommunityGroupDetail/13058609_1.html
http://club.jd.com/CommunityGroupDetail/13058613_1.html
http://club.jd.com/CommunityGroupDetail/13058615_1.html
http://club.jd.com/CommunityGroupDetail/13058618_1.html
http://club.jd.com/CommunityGroupDetail/13058621_1.html
http://club.jd.com/CommunityGroupDetail/13058622_1.html
http://club.jd.com/CommunityGroupDetail/13058631_1.html
http://club.jd.com/CommunityGroupDetail/13058636_1.html
http://club.jd.com/CommunityGroupDetail/13058645_1.html
http://club.jd.com/CommunityGroupDetail/13058651_1.html
http://club.jd.com/CommunityGroupDetail/13058658_1.html
http://club.jd.com/CommunityGroupDetail/13058662_1.html
http://club.jd.com/CommunityGroupDetail/13058670_1.html
http://club.jd.com/CommunityGroupDetail/13058671_1.html
http://club.jd.com/CommunityGroupDetail/13058672_1.html
http://club.jd.com/CommunityGroupDetail/13058691_1.html
http://club.jd.com/CommunityGroupDetail/13058692_1.html
http://club.jd.com/CommunityGroupDetail/13058693_1.html
http://club.jd.com/CommunityGroupDetail/13058694_1.html
http://club.jd.com/CommunityGroupDetail/13058695_1.html
http://club.jd.com/CommunityGroupDetail/13058696_1.html
http://club.jd.com/CommunityGroupDetail/13058708_1.html
http://club.jd.com/CommunityGroupDetail/13058719_1.html
http://club.jd.com/CommunityGroupDetail/13058727_1.html
http://club.jd.com/CommunityGroupDetail/13058728_1.html
http://club.jd.com/CommunityGroupDetail/13058732_1.html
http://club.jd.com/CommunityGroupDetail/13058733_1.html
http://club.jd.com/CommunityGroupDetail/13058734_1.html
http://club.jd.com/CommunityGroupDetail/13058736_1.html
http://club.jd.com/CommunityGroupDetail/13058738_1.html
http://club.jd.com/CommunityGroupDetail/13058740_1.html
http://club.jd.com/CommunityGroupDetail/13058743_1.html
http://club.jd.com/CommunityGroupDetail/13058746_1.html
http://club.jd.com/CommunityGroupDetail/13058748_1.html
http://club.jd.com/CommunityGroupDetail/13058751_1.html
http://club.jd.com/CommunityGroupDetail/13058755_1.html
http://club.jd.com/CommunityGroupDetail/13058757_1.html
http://club.jd.com/CommunityGroupDetail/13058760_1.html
http://club.jd.com/CommunityGroupDetail/13058763_1.html
http://club.jd.com/CommunityGroupDetail/13058767_1.html
http://club.jd.com/CommunityGroupDetail/13058770_1.html
http://club.jd.com/CommunityGroupDetail/13058771_1.html
http://club.jd.com/CommunityGroupDetail/13058776_1.html
http://club.jd.com/CommunityGroupDetail/13058784_1.html
http://club.jd.com/CommunityGroupDetail/13058786_1.html
http://club.jd.com/CommunityGroupDetail/13058787_1.html
http://club.jd.com/CommunityGroupDetail/13058792_1.html
http://club.jd.com/CommunityGroupDetail/13058793_1.html
http://club.jd.com/CommunityGroupDetail/13058795_1.html
http://club.jd.com/CommunityGroupDetail/13058797_1.html
http://club.jd.com/CommunityGroupDetail/13058802_1.html
http://club.jd.com/CommunityGroupDetail/13058810_1.html
http://club.jd.com/CommunityGroupDetail/13058813_1.html
http://club.jd.com/CommunityGroupDetail/13058815_1.html
http://club.jd.com/CommunityGroupDetail/13058818_1.html
http://club.jd.com/CommunityGroupDetail/13058823_1.html
http://club.jd.com/CommunityGroupDetail/13058824_1.html
http://club.jd.com/CommunityGroupDetail/13058825_1.html
http://club.jd.com/CommunityGroupDetail/13058828_1.html
http://club.jd.com/CommunityGroupDetail/13058831_1.html
http://club.jd.com/CommunityGroupDetail/13058833_1.html
http://club.jd.com/CommunityGroupDetail/13058835_1.html
http://club.jd.com/CommunityGroupDetail/13058839_1.html
http://club.jd.com/CommunityGroupDetail/13058844_1.html
http://club.jd.com/CommunityGroupDetail/13058848_1.html
http://club.jd.com/CommunityGroupDetail/13058851_1.html
http://club.jd.com/CommunityGroupDetail/13058857_1.html
http://club.jd.com/CommunityGroupDetail/13058858_1.html
http://club.jd.com/CommunityGroupDetail/13058861_1.html
http://club.jd.com/CommunityGroupDetail/13058864_1.html
http://club.jd.com/CommunityGroupDetail/13058869_1.html
http://club.jd.com/CommunityGroupDetail/13058872_1.html
http://club.jd.com/CommunityGroupDetail/13058878_1.html
http://club.jd.com/CommunityGroupDetail/13058879_1.html
http://club.jd.com/CommunityGroupDetail/13058880_1.html
http://club.jd.com/CommunityGroupDetail/13058888_1.html
http://club.jd.com/CommunityGroupDetail/13058890_1.html
http://club.jd.com/CommunityGroupDetail/13058897_1.html
http://club.jd.com/CommunityGroupDetail/13058898_1.html
http://club.jd.com/CommunityGroupDetail/13058900_1.html
http://club.jd.com/CommunityGroupDetail/13058903_1.html
http://club.jd.com/CommunityGroupDetail/13058919_1.html
http://club.jd.com/CommunityGroupDetail/13058923_1.html
http://club.jd.com/CommunityGroupDetail/13058931_1.html
http://club.jd.com/CommunityGroupDetail/13058942_1.html
http://club.jd.com/CommunityGroupDetail/13058945_1.html
http://club.jd.com/CommunityGroupDetail/13058953_1.html
http://club.jd.com/CommunityGroupDetail/13058954_1.html
http://club.jd.com/CommunityGroupDetail/13058958_1.html
http://club.jd.com/CommunityGroupDetail/13058960_1.html
http://club.jd.com/CommunityGroupDetail/13058963_1.html
http://club.jd.com/CommunityGroupDetail/13058979_1.html
http://club.jd.com/CommunityGroupDetail/13058983_1.html
http://club.jd.com/CommunityGroupDetail/13058985_1.html
http://club.jd.com/CommunityGroupDetail/13058986_1.html
http://club.jd.com/CommunityGroupDetail/13058991_1.html
http://club.jd.com/CommunityGroupDetail/13058995_1.html
http://club.jd.com/CommunityGroupDetail/13058997_1.html
http://club.jd.com/CommunityGroupDetail/13058999_1.html
http://club.jd.com/CommunityGroupDetail/13059000_1.html
http://club.jd.com/CommunityGroupDetail/13059004_1.html
http://club.jd.com/CommunityGroupDetail/13059022_1.html
http://club.jd.com/CommunityGroupDetail/13059023_1.html
http://club.jd.com/CommunityGroupDetail/13059032_1.html
http://club.jd.com/CommunityGroupDetail/13059062_1.html
http://club.jd.com/CommunityGroupDetail/13059063_1.html
http://club.jd.com/CommunityGroupDetail/13059064_1.html
http://club.jd.com/CommunityGroupDetail/13059069_1.html
http://club.jd.com/CommunityGroupDetail/13059078_1.html
http://club.jd.com/CommunityGroupDetail/13059080_1.html
http://club.jd.com/CommunityGroupDetail/13059092_1.html
http://club.jd.com/CommunityGroupDetail/13059094_1.html
http://club.jd.com/CommunityGroupDetail/13059095_1.html
http://club.jd.com/CommunityGroupDetail/13059100_1.html
http://club.jd.com/CommunityGroupDetail/13059101_1.html
http://club.jd.com/CommunityGroupDetail/13059108_1.html
http://club.jd.com/CommunityGroupDetail/13059112_1.html
http://club.jd.com/CommunityGroupDetail/13059118_1.html
http://club.jd.com/CommunityGroupDetail/13059119_1.html
http://club.jd.com/CommunityGroupDetail/13059124_1.html
http://club.jd.com/CommunityGroupDetail/13059131_1.html
http://club.jd.com/CommunityGroupDetail/13059141_1.html
http://club.jd.com/CommunityGroupDetail/13059143_1.html
http://club.jd.com/CommunityGroupDetail/13059151_1.html
http://club.jd.com/CommunityGroupDetail/13059158_1.html
http://club.jd.com/CommunityGroupDetail/13059186_1.html
http://club.jd.com/CommunityGroupDetail/13059189_1.html
http://club.jd.com/CommunityGroupDetail/13059195_1.html
http://club.jd.com/CommunityGroupDetail/13059205_1.html
http://club.jd.com/CommunityGroupDetail/13059209_1.html
http://club.jd.com/CommunityGroupDetail/13059210_1.html
http://club.jd.com/CommunityGroupDetail/13059211_1.html
http://club.jd.com/CommunityGroupDetail/13059222_1.html
http://club.jd.com/CommunityGroupDetail/13059230_1.html
http://club.jd.com/CommunityGroupDetail/13059232_1.html
http://club.jd.com/CommunityGroupDetail/13059233_1.html
http://club.jd.com/CommunityGroupDetail/13059240_1.html
http://club.jd.com/CommunityGroupDetail/13059242_1.html
http://club.jd.com/CommunityGroupDetail/13059252_1.html
http://club.jd.com/CommunityGroupDetail/13059267_1.html
http://club.jd.com/CommunityGroupDetail/13059311_1.html
http://club.jd.com/CommunityGroupDetail/13059319_1.html
http://club.jd.com/CommunityGroupDetail/13059320_1.html
http://club.jd.com/CommunityGroupDetail/13059324_1.html
http://club.jd.com/CommunityGroupDetail/13059329_1.html
http://club.jd.com/CommunityGroupDetail/13059342_1.html
http://club.jd.com/CommunityGroupDetail/13059343_1.html
http://club.jd.com/CommunityGroupDetail/13059349_1.html
http://club.jd.com/CommunityGroupDetail/13059356_1.html
http://club.jd.com/CommunityGroupDetail/13059374_1.html
http://club.jd.com/CommunityGroupDetail/13059377_1.html
http://club.jd.com/CommunityGroupDetail/13059381_1.html
http://club.jd.com/CommunityGroupDetail/13059382_1.html
http://club.jd.com/CommunityGroupDetail/13059383_1.html
http://club.jd.com/CommunityGroupDetail/13059408_1.html
http://club.jd.com/CommunityGroupDetail/13059431_1.html
http://club.jd.com/CommunityGroupDetail/13059440_1.html
http://club.jd.com/CommunityGroupDetail/13059496_1.html
http://club.jd.com/CommunityGroupDetail/13059497_1.html
http://club.jd.com/CommunityGroupDetail/13059498_1.html
http://club.jd.com/CommunityGroupDetail/13059522_1.html
http://club.jd.com/CommunityGroupDetail/13059525_1.html
http://club.jd.com/CommunityGroupDetail/13059552_1.html
http://club.jd.com/CommunityGroupDetail/13059611_1.html
倒计时的CountDownTimer