亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android 簡(jiǎn)單實(shí)現(xiàn)倒計(jì)時(shí)功能

 更新時(shí)間:2019年12月16日 09:28:48   作者:是我ChaoYoung  
在 Android 中倒計(jì)時(shí)功能是比較常用的一個(gè)功能,比如短信驗(yàn)證碼,付款倒計(jì)時(shí)等。今天小編給大家分享Android 簡(jiǎn)單實(shí)現(xiàn)倒計(jì)時(shí)功能,感興趣的朋友一起看看吧

在 Android 中倒計(jì)時(shí)功能是比較常用的一個(gè)功能,比如短信驗(yàn)證碼,付款倒計(jì)時(shí)等。實(shí)現(xiàn)方式有Handler、Thread 等,但是實(shí)現(xiàn)起來(lái)都有點(diǎn)麻煩,其實(shí)Android已經(jīng)為我們封裝好了一個(gè)抽象類 CountDownTimer,可以簡(jiǎn)單的實(shí)現(xiàn)倒計(jì)時(shí)功能,如下圖所示。

countDown.gif

CountDownTimer 實(shí)現(xiàn)倒計(jì)時(shí)功能的機(jī)制也是用Handler 消息控制,只是它幫我們已經(jīng)封裝好了,先看一下它的介紹。

Schedule a countdown until a time in the future, with regular notifications on intervals along the way. Example of showing a 30 second countdown in a text field:
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText(“done!”);
}
}.start();

大致意思是,設(shè)置一個(gè)倒計(jì)時(shí),直到完成這個(gè)時(shí)間段的計(jì)時(shí),并會(huì)實(shí)時(shí)更新時(shí)間的變化,最后舉了一個(gè)30秒倒計(jì)時(shí)的例子,如下:

 new CountDownTimer(30000, 1000) {
   public void onTick(long millisUntilFinished) {
     mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
   }
   public void onFinish() {
     mTextField.setText("done!");
   }
 }.start();

詳解

可以看到,上面示例中構(gòu)造方法需要傳入兩個(gè)參數(shù),如下:

 /**
 * @param millisInFuture The number of millis in the future from the call to start()
 *           until the countdown is done and onFinish() is called.
 * @param countDownInterval The interval along the way to receive onTick(long) callbacks.
 */
 public CountDownTimer(long millisInFuture, long countDownInterval) {
   mMillisInFuture = millisInFuture;
   mCountdownInterval = countDownInterval;
 }

第一個(gè)參數(shù)是倒計(jì)時(shí)的總時(shí)間,第二個(gè)參數(shù)是倒計(jì)時(shí)的時(shí)間間隔(每隔多久執(zhí)行一次),注意這里傳入的兩個(gè)時(shí)間參數(shù)的單位都是毫秒。

它提供的幾個(gè)方法也很簡(jiǎn)單,如下:

methods.png

  • start():開(kāi)始倒計(jì)時(shí)。
  • cancel():取消倒計(jì)時(shí)。
  • onFinish():倒計(jì)時(shí)完成后回調(diào)。
  • onTick(long millisUnitilFinished):當(dāng)前任務(wù)每完成一次倒計(jì)時(shí)間隔時(shí)間時(shí)回調(diào)。

驗(yàn)證碼示例

短信驗(yàn)證碼倒計(jì)時(shí)原理很簡(jiǎn)單,也就是點(diǎn)擊獲取驗(yàn)證碼開(kāi)啟倒計(jì)時(shí),在倒計(jì)時(shí)內(nèi)不可點(diǎn)擊,倒計(jì)時(shí)結(jié)束后方可重新獲取,如下所示:

new CountDownTimer(millisUntilFinished, 1000) {
  /**
   * 當(dāng)前任務(wù)每完成一次倒計(jì)時(shí)間隔時(shí)間時(shí)回調(diào)
   * @param millisUntilFinished
   */
  public void onTick(long millisUntilFinished) {
    if (btn_Code != null) {
      //按鈕不可用
      btn_Code.setClickable(false);
      btn_Code.setEnabled(false);
      btn_Code.setText(millisUntilFinished / 1000 + "s");
    }
  }

  /**
   * 倒計(jì)時(shí)完成后回調(diào)
   */
  public void onFinish() {
    if (btn_Code != null) {
      //按鈕可用
      btn_Code.setText("重新獲取");
      btn_Code.setClickable(true);
      btn_Code.setEnabled(true);
    }
    //取消倒計(jì)時(shí)
    cancel();
  }
}.start();

注:在Activity或Fragment銷(xiāo)毀的時(shí)候記得調(diào)用 cancle() 方法,否則它的 onTick() 方法還會(huì)繼續(xù)執(zhí)行,容易造成內(nèi)存泄漏。

總結(jié)

以上所述是小編給大家介紹的Android 簡(jiǎn)單實(shí)現(xiàn)倒計(jì)時(shí)功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

相關(guān)文章

最新評(píng)論