【Android自定義View實戰】之獲取驗證碼倒計時按鈕

轉載請注明出處:http://blog.csdn.net/linglongxin24/article/details/52876225 【DylanAndroid的csdn博客】


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

1.先看效果圖

這里寫圖片描述

2.我們涉及到的變量

  • 倒計時時長,可設置

/**
* 倒計時時長,默認倒計時時間60秒;
*/
private long length = 60 * 1000;


*  在點擊按鈕之前按鈕所顯示的文字
```java
 /**
    * 在點擊按鈕之前按鈕所顯示的文字,默認是獲取驗證碼
    */
   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" />
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容