Android_驗證碼按鈕倒計時

在注冊獲取驗證碼時候需要一個倒計時按鈕。

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);
            }
        }
    }
};
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,068評論 25 709
  • 相信大家在項目里面不少會用到倒計時操作吧,倒計時功能在我們業(yè)務(wù)開發(fā)中使用概率非常高,例如用戶操作姿勢錯誤,我們給...
    Scus閱讀 10,196評論 0 3
  • 內(nèi)容抽屜菜單ListViewWebViewSwitchButton按鈕點贊按鈕進度條TabLayout圖標(biāo)下拉刷新...
    皇小弟閱讀 47,162評論 22 665
  • 當(dāng)初沒有搬進小院之前還曾幻想著,每天干了什么事寫下來,也好,當(dāng)做紀(jì)念。結(jié)果開工一個多月,每天都是累得筋疲力盡。別說...
    八_方閱讀 746評論 0 51
  • 知道我存在的那刻起,爸比就高興地合不攏嘴;從那天開始,爸比就擔(dān)任起我家的大廚,說實話,爸比的手藝超級贊,要不怎么能...
    孫昱淑閱讀 1,010評論 0 2

友情鏈接更多精彩內(nèi)容