Android 簡(jiǎn)單封裝獲取驗(yàn)證碼倒計(jì)時(shí)功能
效果如下圖所示:
如圖所示的效果相信大家都不陌生,我們可以使用很多種方法去實(shí)現(xiàn)此效果,這里自己采用 CountDownTimer 定時(shí)器簡(jiǎn)單封裝下此效果,方便我們隨時(shí)調(diào)用。
首頁先在 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é)束了,過程很簡(jiǎn)單。這里主要是對(duì) CountDownTimer 的使用練習(xí),之前工作中一直沒有接觸過這個(gè)類。順便貼上源碼吧!
總結(jié)
以上所述是小編給大家介紹的Android 簡(jiǎn)單封裝獲取驗(yàn)證碼倒計(jì)時(shí)功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Flutter進(jìn)階之實(shí)現(xiàn)動(dòng)畫效果(二)
這篇文章主要為大家詳細(xì)介紹了Flutter進(jìn)階之實(shí)現(xiàn)動(dòng)畫效果的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08Android利用滑動(dòng)菜單框架實(shí)現(xiàn)滑動(dòng)菜單效果
這篇文章主要介紹了Android實(shí)現(xiàn)滑動(dòng)菜單特效之滑動(dòng)菜單框架完全解析,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05EditText監(jiān)聽方法,實(shí)時(shí)的判斷輸入多少字符
在EditText提供了一個(gè)方法addTextChangedListener實(shí)現(xiàn)對(duì)輸入文本的監(jiān)控。本文分享了EditText監(jiān)聽方法案例,需要的朋友一起來看下吧2016-12-12Android編程中File文件常見存儲(chǔ)與讀取操作demo示例
這篇文章主要介紹了Android編程中File文件常見存儲(chǔ)與讀取操作,結(jié)合實(shí)例形式分析了Android針對(duì)文件的打開、讀寫及布局等相關(guān)操作技巧,需要的朋友可以參考下2017-09-09使用ViewPager實(shí)現(xiàn)左右循環(huán)滑動(dòng)及滑動(dòng)跳轉(zhuǎn)
今天實(shí)現(xiàn)了左右滑動(dòng),至于在最后一頁滑動(dòng)跳轉(zhuǎn),這個(gè)也做了但是效果不是太好,也希望有實(shí)現(xiàn)的朋友能夠分享下2013-01-01RecyclerView優(yōu)雅實(shí)現(xiàn)復(fù)雜列表布局
這篇文章主要為大家詳細(xì)介紹了RecyclerView優(yōu)雅實(shí)現(xiàn)復(fù)雜列表布局,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11Android OpenGLES如何給相機(jī)添加濾鏡詳解
這篇文章主要給大家介紹了關(guān)于Android OpenGLES如何給相機(jī)添加濾鏡的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Android 軟鍵盤自動(dòng)彈出與關(guān)閉實(shí)例詳解
這篇文章主要介紹了Android 軟鍵盤自動(dòng)彈出與關(guān)閉實(shí)例詳解的相關(guān)資料,為了用戶體驗(yàn)應(yīng)該自動(dòng)彈出軟鍵盤而不是讓用戶主動(dòng)點(diǎn)擊輸入框才彈出,這里舉例說明該如何實(shí)現(xiàn),需要的朋友可以參考下2016-12-12