Android自定義View獲取注冊(cè)驗(yàn)證碼倒計(jì)時(shí)按鈕
在Android開(kāi)發(fā)中,我們不可避免的會(huì)做到注冊(cè)功能,而現(xiàn)在的注冊(cè)大多數(shù)都是用手機(jī)去注冊(cè)的,那么注冊(cè)的時(shí)候都會(huì)要求用獲取驗(yàn)證碼的方式去驗(yàn)證,我們接下來(lái)就來(lái)實(shí)戰(zhàn)一下自定義獲取驗(yàn)證碼倒計(jì)時(shí)按鈕:
1.先看效果圖

2.我們涉及到的變量
//倒計(jì)時(shí)時(shí)長(zhǎng),可設(shè)置 /** * 倒計(jì)時(shí)時(shí)長(zhǎng),默認(rèn)倒計(jì)時(shí)時(shí)間60秒; */ private long length = 60 * 1000; //在點(diǎn)擊按鈕之前按鈕所顯示的文字 /** * 在點(diǎn)擊按鈕之前按鈕所顯示的文字,默認(rèn)是獲取驗(yàn)證碼 */ private String beforeText = "獲取驗(yàn)證碼"; //在開(kāi)始倒計(jì)時(shí)之后那個(gè)秒數(shù)數(shù)字之后所要顯示的字 /** * 在開(kāi)始倒計(jì)時(shí)之后那個(gè)秒數(shù)數(shù)字之后所要顯示的字,默認(rèn)是秒 */ private String afterText = "秒";
3.利用什么倒計(jì)時(shí)Timer
在Java中定時(shí)器任務(wù)的執(zhí)行需要兩個(gè)基本的類:
java.util.Timer;
java.util.TimerTask;
要運(yùn)行一個(gè)定時(shí)任務(wù),最基本的步驟如下:
1、建立一個(gè)要執(zhí)行的任務(wù)TimerTask。
2、創(chuàng)建一個(gè)Timer實(shí)例,通過(guò)Timer提供的schedule()方法,將 TimerTask加入到定時(shí)器Timer中,同時(shí)設(shè)置執(zhí)行的規(guī)則即可。
當(dāng)程序執(zhí)行了Timer初始化代碼后,Timer定時(shí)任務(wù)就會(huì)按照設(shè)置去執(zhí)行。
Timer中的schedule()方法是有多種重載格式的,以適應(yīng)不同的情況。該方法的格式如下:
void schedule(TimerTask task, Date time)
安排在指定的時(shí)間執(zhí)行指定的任務(wù)。
void schedule(TimerTask task, Date firstTime, long period)
安排指定的任務(wù)在指定的時(shí)間開(kāi)始進(jìn)行重復(fù)的固定延遲執(zhí)行。
void schedule(TimerTask task, long delay)
安排在指定延遲后執(zhí)行指定的任務(wù)。
void schedule(TimerTask task, long delay, long period)
安排指定的任務(wù)從指定的延遲后開(kāi)始進(jìn)行重復(fù)的固定延遲執(zhí)行。
Timer是線程安全的,此類可擴(kuò)展到大量同時(shí)安排的任務(wù)(存在數(shù)千個(gè)都沒(méi)有問(wèn)題)。其所有構(gòu)造方法都啟動(dòng)計(jì)時(shí)器線程??梢哉{(diào)用cancel() 終止此計(jì)時(shí)器,丟棄所有當(dāng)前已安排的任務(wù)。purge()從此計(jì)時(shí)器的任務(wù)隊(duì)列中移除所有已取消的任務(wù)。此類不提供實(shí)時(shí)保證:它使用 Object.wait(long) 方法來(lái)安排任務(wù)。
TimerTask是一個(gè)抽象類,由 Timer 安排為一次執(zhí)行或重復(fù)執(zhí)行的任務(wù)。它有一個(gè)抽象方法run()—-計(jì)時(shí)器任務(wù)要執(zhí)行的操作。因此,每個(gè)具體的任務(wù)類都必須繼承TimerTask類,并且重寫run()方法。另外它還有兩個(gè)非抽象的方法:
boolean cancel()
取消此計(jì)時(shí)器任務(wù)。
long scheduledExecutionTime()
返回此任務(wù)最近實(shí)際 執(zhí)行的安排 執(zhí)行時(shí)間。
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;
/**
* 自定義倒計(jì)時(shí)按鈕
* <p/>
*
* @author Dylan
* [佛祖保佑 永無(wú)BUG]
* Created by Dylan on 2016/10/5 0005.
*/
public class CountdownButton extends Button implements View.OnClickListener {
/**
* 倒計(jì)時(shí)時(shí)長(zhǎng),默認(rèn)倒計(jì)時(shí)時(shí)間60秒;
*/
private long length = 60 * 1000;
/**
* 開(kāi)始執(zhí)行計(jì)時(shí)的類,可以在每秒實(shí)行間隔任務(wù)
*/
private Timer timer;
/**
* 每秒時(shí)間到了之后所執(zhí)行的任務(wù)
*/
private TimerTask timerTask;
/**
* 在點(diǎn)擊按鈕之前按鈕所顯示的文字,默認(rèn)是獲取驗(yàn)證碼
*/
private String beforeText = "獲取驗(yàn)證碼";
/**
* 在開(kāi)始倒計(jì)時(shí)之后那個(gè)秒數(shù)數(shù)字之后所要顯示的字,默認(rèn)是秒
*/
private String afterText = "秒";
/**
* 按鈕點(diǎn)擊事件
*/
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);
}
/**
* 初始化時(shí)間
*/
private void initTimer() {
timer = new Timer();
timerTask = new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(1);
}
};
}
/**
* 設(shè)置倒計(jì)時(shí)時(shí)長(zhǎng)
*
* @param length 默認(rèn)毫秒
*/
public void setLength(long length) {
this.length = length;
}
/**
* 設(shè)置未點(diǎn)擊時(shí)顯示的文字
*
* @param beforeText
*/
public void setBeforeText(String beforeText) {
this.beforeText = beforeText;
}
/**
* 設(shè)置未點(diǎn)擊后顯示的文字
*
* @param beforeText
*/
public void setAfterText(String beforeText) {
this.afterText = afterText;
}
/**
* 設(shè)置監(jiān)聽(tīng)按鈕點(diǎn)擊事件
*
* @param onclickListener
*/
@Override
public void setOnClickListener(OnClickListener onclickListener) {
if (onclickListener instanceof CountdownButton) {
super.setOnClickListener(onclickListener);
} else {
this.onClickListener = onclickListener;
}
}
/**
* 點(diǎn)擊按鈕后的操作
*
* @param v
*/
@Override
public void onClick(View v) {
start();
if (onClickListener != null) {
onClickListener.onClick(v);
}
}
/**
* 開(kāi)始倒計(jì)時(shí)
*/
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;
}
}
};
/**
* 清除倒計(jì)時(shí)
*/
private void clearTimer() {
if (timerTask != null) {
timerTask.cancel();
timerTask = null;
}
if (timer != null) {
timer.cancel();
timer = null;
}
}
/**
* 記得一定要在activity或者fragment消亡的時(shí)候清除倒計(jì)時(shí),
* 因?yàn)槿绻褂?jì)時(shí)沒(méi)有完的話子線程還在跑,
* 這樣的話就會(huì)引起內(nèi)存溢出
*/
@Override
protected void onDetachedFromWindow() {
clearTimer();
super.onDetachedFromWindow();
}
}
5.用法,超級(jí)簡(jiǎn)單
<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="獲取驗(yàn)證碼"
android:textColor="#1179c4"
android:textSize="@dimen/txt14sp" />
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android sdcard實(shí)現(xiàn)圖片存儲(chǔ) 、聯(lián)網(wǎng)下載
這篇文章主要介紹了Android sdcard實(shí)現(xiàn)圖片存儲(chǔ) 、聯(lián)網(wǎng)下載功能,感興趣的小伙伴們可以參考一下2016-02-02
Android通過(guò)XListView實(shí)現(xiàn)上拉加載下拉刷新功能
這篇文章主要為大家詳細(xì)介紹了Android通過(guò)XListView實(shí)現(xiàn)上拉加載下拉刷新功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android實(shí)現(xiàn)頂部導(dǎo)航欄可點(diǎn)擊可滑動(dòng)效果(仿微信仿豆瓣網(wǎng))
這篇文章主要介紹了 Android實(shí)現(xiàn)頂部導(dǎo)航欄可點(diǎn)擊可滑動(dòng)效果(仿微信仿豆瓣網(wǎng)),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
Android實(shí)現(xiàn) EditText輸入手機(jī)號(hào)空格功能
這篇文章主要介紹了Android實(shí)現(xiàn) EditText輸入手機(jī)號(hào)空格功能,實(shí)現(xiàn)思路是要重寫TextWatcher,每次EditText內(nèi)容變化,都判斷內(nèi)容是否符合要求,具體實(shí)例代碼大家參考下本文2018-02-02
三種Android單擊事件onclick的實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了三種Android單擊事件onclick的實(shí)現(xiàn)方法,感興趣的小伙伴們可以參考一下2016-05-05
Android之沉浸式狀態(tài)欄的實(shí)現(xiàn)方法、狀態(tài)欄透明
本篇文章主要介紹了Android之沉浸式狀態(tài)欄的實(shí)現(xiàn)方法、狀態(tài)欄透明,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-02-02

