Android封裝實(shí)現(xiàn)短信驗(yàn)證碼的獲取倒計(jì)時(shí)
如圖所示的效果相信大家都不陌生,我們可以使用很多種方法去實(shí)現(xiàn)此效果,這里自己采用CountDownTimer
定時(shí)器簡(jiǎn)單封裝下此效果,方便我們隨時(shí)調(diào)用。
首頁(yè)先在attrs.xml
中定義下所需的幾個(gè)屬性:
<resources> <declare-styleable name="CountDownButton"> <attr name="millisinfuture" format="integer"/> <attr name="countdowninterva" format="integer"/> <attr name="normalColor" format="color"/> <attr name="countDownColor" format="color"/> </declare-styleable> </resources>
下面是實(shí)現(xiàn)的具體代碼,很簡(jiǎn)單的一種方式,通俗易懂:
/** * Created by xiaolong on 2018/1/12. */ @SuppressLint("AppCompatCustomView") public class CountDownButton extends Button{ //總時(shí)長(zhǎng) private long millisinfuture; //間隔時(shí)長(zhǎng) private long countdowninterva; //默認(rèn)背景顏色 private int normalColor; //倒計(jì)時(shí) 背景顏色 private int countDownColor; //是否結(jié)束 private boolean isFinish; //定時(shí)器 private CountDownTimer countDownTimer; public CountDownButton(Context context) { this(context,null); } public CountDownButton(Context context, AttributeSet attrs) { this(context, attrs,0); } public CountDownButton(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CountDownButton,defStyleAttr,0); //設(shè)置默認(rèn)時(shí)長(zhǎng) millisinfuture = (long) typedArray.getInt(R.styleable.CountDownButton_millisinfuture,60000); //設(shè)置默認(rèn)間隔時(shí)長(zhǎng) countdowninterva = (long)typedArray.getInt(R.styleable.CountDownButton_countdowninterva,1000); //設(shè)置默認(rèn)背景色 normalColor = typedArray.getColor(R.styleable.CountDownButton_normalColor,android.R.color.holo_blue_light); //設(shè)置默認(rèn)倒計(jì)時(shí) 背景色 countDownColor = typedArray.getColor(R.styleable.CountDownButton_countDownColor,android.R.color.darker_gray); typedArray.recycle(); //默認(rèn)為已結(jié)束狀態(tài) isFinish = true; //字體居中 setGravity(Gravity.CENTER); //默認(rèn)文字和背景色 normalBackground(); //設(shè)置定時(shí)器 countDownTimer = new CountDownTimer(millisinfuture, countdowninterva) { @Override public void onTick(long millisUntilFinished) { //未結(jié)束 isFinish = false; setText((Math.round((double) millisUntilFinished / 1000) - 1) + "秒"); setBackgroundResource(countDownColor); } @Override public void onFinish() { //結(jié)束 isFinish = true; normalBackground(); } }; } private void normalBackground(){ setText("獲取驗(yàn)證碼"); setBackgroundResource(normalColor); } public boolean isFinish() { return isFinish; } public void cancel(){ countDownTimer.cancel(); } public void start(){ countDownTimer.start(); } }
一個(gè)簡(jiǎn)單的調(diào)用方式:
public class MainActivity extends AppCompatActivity { private CountDownButton countDownButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); countDownButton = ((CountDownButton) findViewById(R.id.countDownButton)); countDownButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //這里判斷是否倒計(jì)時(shí)結(jié)束,避免在倒計(jì)時(shí)時(shí)多次點(diǎn)擊導(dǎo)致重復(fù)請(qǐng)求接口 if (countDownButton.isFinish()) { //發(fā)送驗(yàn)證碼請(qǐng)求成功后調(diào)用 countDownButton.start(); } } }); } @Override protected void onDestroy() { super.onDestroy(); if (!countDownButton.isFinish()) { countDownButton.cancel(); } } }
這樣一個(gè)簡(jiǎn)單的封裝就結(jié)束了,過(guò)程很簡(jiǎn)單。這里主要是對(duì)CountDownTimer
的使用練習(xí),之前工作中一直沒(méi)有接觸過(guò)這個(gè)類(lèi)。傳送門(mén)
到此這篇關(guān)于Android封裝實(shí)現(xiàn)短信驗(yàn)證碼的獲取倒計(jì)時(shí)的文章就介紹到這了,更多相關(guān)Android封裝倒計(jì)時(shí)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- OpenHarmony實(shí)現(xiàn)類(lèi)Android短信驗(yàn)證碼及倒計(jì)時(shí)流程詳解
- Android自定義短信倒計(jì)時(shí)view流程分析
- Android使用CountDownTimer模擬短信驗(yàn)證倒計(jì)時(shí)
- Android使用Kotlin和RxJava 2.×實(shí)現(xiàn)短信驗(yàn)證碼倒計(jì)時(shí)效果
- Android 用RxBinding與RxJava2實(shí)現(xiàn)短信驗(yàn)證碼倒計(jì)時(shí)功能
- Android實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼倒計(jì)時(shí)功能示例
- Android短信驗(yàn)證碼倒計(jì)時(shí)驗(yàn)證的2種常用方式
- Android自定義Chronometer實(shí)現(xiàn)短信驗(yàn)證碼秒表倒計(jì)時(shí)功能
- Android開(kāi)發(fā)之獲取短信驗(yàn)證碼后按鈕背景變化并且出現(xiàn)倒計(jì)時(shí)
相關(guān)文章
Android使用Rotate3dAnimation實(shí)現(xiàn)3D旋轉(zhuǎn)動(dòng)畫(huà)效果的實(shí)例代碼
利用Android的ApiDemos的Rotate3dAnimation實(shí)現(xiàn)了個(gè)圖片3D旋轉(zhuǎn)的動(dòng)畫(huà),圍繞Y軸進(jìn)行旋轉(zhuǎn),還可以實(shí)現(xiàn)Z軸的縮放。點(diǎn)擊開(kāi)始按鈕開(kāi)始旋轉(zhuǎn),點(diǎn)擊結(jié)束按鈕停止旋轉(zhuǎn)。2018-05-05Android 下載網(wǎng)絡(luò)圖片并顯示到本地
本文主要介紹了Android實(shí)現(xiàn)下載網(wǎng)絡(luò)圖片并顯示到本地功能的示例代碼。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-03-03Android實(shí)現(xiàn)圖片瀏覽并改變透明度
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圖片瀏覽并改變透明度,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08Flutter實(shí)現(xiàn)Android滾動(dòng)懸浮效果過(guò)程
這篇文章主要介紹了Flutter實(shí)現(xiàn)Android滾動(dòng)懸浮效果,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-01-01Android通知欄增加快捷開(kāi)關(guān)的功能實(shí)現(xiàn)教程
對(duì)于Android來(lái)說(shuō)其中一項(xiàng)很方便的操作便是下拉菜單,下拉菜單欄可以快捷打開(kāi)某項(xiàng)設(shè)置,這篇文章主要給大家介紹了關(guān)于Android通知欄增加快捷開(kāi)關(guān)的功能實(shí)現(xiàn),需要的朋友可以參考下2023-01-01Android編程中的5種數(shù)據(jù)存儲(chǔ)方式
這篇文章主要介紹了Android編程中的5種數(shù)據(jù)存儲(chǔ)方式,結(jié)合實(shí)例形式詳細(xì)分析了Android實(shí)現(xiàn)數(shù)據(jù)存儲(chǔ)的5中實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-12-12Android序列化接口Parcelable與Serializable接口對(duì)比
我們使用 Intent 傳遞數(shù)據(jù)的時(shí)候,putExtra() 所支持的數(shù)據(jù)類(lèi)型事有限的,當(dāng)需要傳遞自定義對(duì)象的時(shí)候就需要序列化。Serializable更簡(jiǎn)單但是會(huì)把整個(gè)對(duì)象進(jìn)行序列化因此效率比Parcelable低一些2023-02-02Android自定義ViewPager實(shí)現(xiàn)縱向滑動(dòng)翻頁(yè)效果
這篇文章主要為大家詳細(xì)介紹了Android自定義ViewPager實(shí)現(xiàn)縱向滑動(dòng)翻頁(yè)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07