Android自訂View擷取註冊驗證碼倒計時按鈕_Android

來源:互聯網
上載者:User

在Android開發中,我們不可避免的會做到註冊功能,而現在的註冊大多數都是用手機去註冊的,那麼註冊的時候都會要求用擷取驗證碼的方式去驗證,我們接下來就來實戰一下自訂擷取驗證碼倒計時按鈕:

1.先看效果圖

2.我們涉及到的變數

//倒計時時間長度,可設定/** * 倒計時時間長度,預設倒計時時間60秒; */private long length = 60 * 1000;//在點擊按鈕之前按鈕所顯示的文字/** * 在點擊按鈕之前按鈕所顯示的文字,預設是擷取驗證碼 */private String beforeText = "擷取驗證碼";//在開始倒計時之後那個秒數數字之後所要顯示的字/** * 在開始倒計時之後那個秒數數字之後所要顯示的字,預設是秒 */private String afterText = "秒";

3.利用什麼倒計時Timer

在Java中定時器任務的執行需要兩個基本的類:
java.util.Timer;
java.util.TimerTask;

要運行一個定時任務,最基本的步驟如下:
1、建立一個要執行的任務TimerTask。
2、建立一個Timer執行個體,通過Timer提供的schedule()方法,將 TimerTask加入到定時器Timer中,同時設定執行的規則即可。

當程式執行了Timer初始化代碼後,Timer定時任務就會按照設定去執行。
Timer中的schedule()方法是有多種重載格式的,以適應不同的情況。該方法的格式如下:
void schedule(TimerTask task, Date time)
安排在指定的時間執行指定的任務。
void schedule(TimerTask task, Date firstTime, long period)
安排指定的任務在指定的時間開始進行重複的固定順延強制。
void schedule(TimerTask task, long delay)
安排在指定延遲後執行指定的任務。
void schedule(TimerTask task, long delay, long period)
安排指定的任務從指定的延遲後開始進行重複的固定順延強制。
Timer是安全執行緒的,此類可擴充到大量同時安排的任務(存在數千個都沒有問題)。其所有構造方法都啟動計時器線程。可以調用cancel() 終止此計時器,丟棄所有當前已安排的任務。purge()從此計時器的任務隊列中移除所有已取消的任務。此類不提供即時保證:它使用 Object.wait(long) 方法來安排任務。
TimerTask是一個抽象類別,由 Timer 安排為一次執行或重複執行的任務。它有一個抽象方法run()—-計時器任務要執行的操作。因此,每個具體的任務類都必須繼承TimerTask類,並且重寫run()方法。另外它還有兩個非抽象的方法:
boolean cancel()
取消此計時器任務。
long scheduledExecutionTime()
返回此任務最近實際 執行的安排 執行時間。

4.代碼

import android.content.Context;import android.os.Handler;import android.os.Message;import android.text.TextUtils;import android.util.AttributeSet;import android.view.View;import android.widget.Button;import java.util.Timer;import java.util.TimerTask;/** * 自訂倒計時按鈕 * <p/> * * @author Dylan *     [佛祖保佑 永無BUG] *     Created by Dylan on 2016/10/5 0005. */public class CountdownButton extends Button implements View.OnClickListener {  /**   * 倒計時時間長度,預設倒計時時間60秒;   */  private long length = 60 * 1000;  /**   * 開始執行計時的類,可以在每秒實行間隔任務   */  private Timer timer;  /**   * 每秒時間到了之後所執行的任務   */  private TimerTask timerTask;  /**   * 在點擊按鈕之前按鈕所顯示的文字,預設是擷取驗證碼   */  private String beforeText = "擷取驗證碼";  /**   * 在開始倒計時之後那個秒數數字之後所要顯示的字,預設是秒   */  private String afterText = "秒";  /**   * 按鈕點擊事件   */  private OnClickListener onClickListener;  public CountdownButton(Context context) {    super(context);    initView();  }  public CountdownButton(Context context, AttributeSet attrs) {    super(context, attrs);    initView();  }  public CountdownButton(Context context, AttributeSet attrs, int defStyleAttr) {    super(context, attrs, defStyleAttr);    initView();  }  /**   * 初始化操作   */  private void initView() {    if (!TextUtils.isEmpty(getText())) {      beforeText = getText().toString().trim();    }    this.setText(beforeText);    setOnClickListener(this);  }  /**   * 初始化時間   */  private void initTimer() {    timer = new Timer();    timerTask = new TimerTask() {      @Override      public void run() {        handler.sendEmptyMessage(1);      }    };  }  /**   * 設定倒計時時間長度   *   * @param length 預設毫秒   */  public void setLength(long length) {    this.length = length;  }  /**   * 設定未點擊時顯示的文字   *   * @param beforeText   */  public void setBeforeText(String beforeText) {    this.beforeText = beforeText;  }  /**   * 設定未點擊後顯示的文字   *   * @param beforeText   */  public void setAfterText(String beforeText) {    this.afterText = afterText;  }  /**   * 設定監聽按鈕點擊事件   *   * @param onclickListener   */  @Override  public void setOnClickListener(OnClickListener onclickListener) {    if (onclickListener instanceof CountdownButton) {      super.setOnClickListener(onclickListener);    } else {      this.onClickListener = onclickListener;    }  }  /**   * 點擊按鈕後的操作   *   * @param v   */  @Override  public void onClick(View v) {    start();    if (onClickListener != null) {      onClickListener.onClick(v);    }  }  /**   * 開始倒計時   */  public void start() {    initTimer();    this.setText(length / 1000 + afterText);    this.setEnabled(false);    timer.schedule(timerTask, 0, 1000);  }  /**   * 更新顯示的文本   */  private Handler handler = new Handler() {    @Override    public void handleMessage(Message msg) {      super.handleMessage(msg);      CountdownButton.this.setText(length / 1000 + afterText);      length -= 1000;      if (length < 0) {        CountdownButton.this.setEnabled(true);        CountdownButton.this.setText(beforeText);        clearTimer();        length = 60 * 1000;      }    }  };  /**   * 清除倒計時   */  private void clearTimer() {    if (timerTask != null) {      timerTask.cancel();      timerTask = null;    }    if (timer != null) {      timer.cancel();      timer = null;    }  }  /**   * 記得一定要在activity或者fragment消亡的時候清除倒計時,   * 因為如果倒計時沒有完的話子線程還在跑,   * 這樣的話就會引起記憶體溢出   */  @Override  protected void onDetachedFromWindow() {    clearTimer();    super.onDetachedFromWindow();  }}

5.用法,超級簡單

 <com.bm.ykzx.view.CountdownButton        android:id="@+id/cdb_register_timer"        android:layout_width="120dp"        android:textAllCaps="false"        android:layout_height="match_parent"        android:background="@android:color/transparent"        android:gravity="center"        android:paddingLeft="3dp"        android:paddingRight="3dp"        android:text="擷取驗證碼"        android:textColor="#1179c4"        android:textSize="@dimen/txt14sp" />

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.