Android開發(fā)之獲取短信驗(yàn)證碼后按鈕背景變化并且出現(xiàn)倒計(jì)時(shí)
目前越來(lái)越多的app在注冊(cè)或是進(jìn)行對(duì)應(yīng)操作時(shí),要求獲取短信驗(yàn)證碼,在點(diǎn)擊了獲取短信驗(yàn)證碼的按鈕后,就是出現(xiàn)倒計(jì)時(shí),比如倒計(jì)時(shí)120S,在倒計(jì)時(shí)期間內(nèi),按鈕背景變化并且出現(xiàn)倒計(jì)時(shí),當(dāng)?shù)褂?jì)時(shí)結(jié)束后,如果你沒(méi)有獲取到驗(yàn)證碼,可以再次點(diǎn)擊。
代碼如下所示:
VerCodeTimer mVerCodeTimer=(Button) findViewById(R.id.login_get_ver_code); private class VerCodeTimer extends CountDownTimer { private int seconds; private int interval; //millisInFuture為你設(shè)置的此次倒計(jì)時(shí)的總時(shí)長(zhǎng),比如60秒就設(shè)置為60000 //countDownInterval為你設(shè)置的時(shí)間間隔,比如一般為1秒,根據(jù)需要自定義。 public VerCodeTimer(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); seconds = (int) (millisInFuture / 1000); interval= (int) (countDownInterval/1000); } //每過(guò)你規(guī)定的時(shí)間間隔做的操作 @Override public void onTick(long millisUntilFinished) { getVerCodeButton.setText((seconds-interval) + "秒后重新獲取"); } //倒計(jì)時(shí)結(jié)束時(shí)做的操作↓↓ @Override public void onFinish() { getVerCodeButton.setTextSize(10); getVerCodeButton.setText("重新獲取驗(yàn)證碼"); getVerCodeButton.setClickable(true); getVerCodeButton.setBackgroundResource(R.drawable.login_get_ver_code_before_bg); } } @Override public void onBackPressed() { if (mVerCodeTimer != null) mVerCodeTimer.cancel(); super.onBackPressed(); }
使用的時(shí)候:
getVerCodeButton.setTextSize(11); getVerCodeButton.setClickable(false); getVerCodeButton.setBackgroundResource(R.drawable.login_get_ver_code_ago_bg); mVerCodeTimer = new VerCodeTimer(60000, 1000); mVerCodeTimer.start();
login_edit_normal_bg.xml:
<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:useLevel="false"> <!-- 背景填充顏色值 --> <solid android:color="#6c948b" /> <!-- radius值越大,越趨于圓形 --> <corners android:radius="10dip" /> <!-- 圓角圖像內(nèi)部填充四周的大小 ,將會(huì)以此擠壓內(nèi)部布置的view --> <padding android:bottom="10dip" android:left="10dip" android:right="10dip" android:top="10dip" /> </shape>
login_edit_passed_bg.xml:
<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:useLevel="false"> <!-- 背景填充顏色值 --> <solid android:color="#509989" /> <!-- radius值越大,越趨于圓形 --> <corners android:radius="10dip" /> <!-- 圓角圖像內(nèi)部填充四周的大小 ,將會(huì)以此擠壓內(nèi)部布置的view --> <padding android:bottom="10dip" android:left="10dip" android:right="10dip" android:top="10dip" /> </shape>
以上所述是小編給大家介紹了Android開發(fā)之獲取短信驗(yàn)證碼后按鈕背景變化并且出現(xiàn)倒計(jì)時(shí) 的全部代碼,希望本段代碼能夠幫助大家。同時(shí)感謝大家一直以來(lái)對(duì)腳本之家網(wǎng)站的支持。
- OpenHarmony實(shí)現(xiàn)類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封裝實(shí)現(xiàn)短信驗(yàn)證碼的獲取倒計(jì)時(shí)
相關(guān)文章
View Controller Transition實(shí)現(xiàn)京東加購(gòu)物車效果
這篇文章主要介紹了View Controller Transition實(shí)現(xiàn)京東加購(gòu)物車效果,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02android中GridView實(shí)現(xiàn)點(diǎn)擊查看更多功能示例
本篇文章主要介紹了android中GridView實(shí)現(xiàn)點(diǎn)擊查看更多功能示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-02-02Android的VSYNC機(jī)制和UI刷新流程示例詳解
這篇文章主要為大家介紹了Android的VSYNC機(jī)制和UI刷新流程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12Android實(shí)現(xiàn)獲取SERIAL信息的方法
這篇文章主要介紹了Android實(shí)現(xiàn)獲取SERIAL信息的方法,涉及Android操作SERIAL的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10Android?使用flow實(shí)現(xiàn)倒計(jì)時(shí)的方式
這篇文章主要介紹了Android?使用flow實(shí)現(xiàn)倒計(jì)時(shí)的方式,借助Flow這個(gè)工具,更加優(yōu)雅地實(shí)現(xiàn)這個(gè)需求功能,文末給大家整理了Android?實(shí)現(xiàn)倒計(jì)時(shí)的幾種方式,需要的朋友可以參考下2022-04-04Android ContentProvider獲取手機(jī)聯(lián)系人實(shí)例
這篇文章主要介紹了Android ContentProvider獲取手機(jī)聯(lián)系人實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02