Android 簡(jiǎ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)。順便貼上源碼吧!
總結(jié)
以上所述是小編給大家介紹的Android 簡(jiǎn)單封裝獲取驗(yàn)證碼倒計(jì)時(shí)功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Android WebView通過(guò)動(dòng)態(tài)的修改js去攔截post請(qǐng)求參數(shù)實(shí)例
這篇文章主要介紹了Android WebView通過(guò)動(dòng)態(tài)的修改js去攔截post請(qǐng)求參數(shù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
簡(jiǎn)述Android中實(shí)現(xiàn)APP文本內(nèi)容的分享發(fā)送與接收方法
本篇文章主要對(duì)Android中實(shí)現(xiàn)APP文本內(nèi)容的分享發(fā)送與接收方法進(jìn)行介紹,相信對(duì)大家學(xué)習(xí)會(huì)有很好的幫助,需要的朋友一起來(lái)看下吧2016-12-12
教你一步步實(shí)現(xiàn)Android微信自動(dòng)搶紅包
自從微信添加搶紅包的功能,微信的電商之旅算是正式開(kāi)始正式火爆起來(lái)。但是作為Android開(kāi)發(fā)者來(lái)說(shuō),我們首先考慮的是如何實(shí)現(xiàn)Android微信自動(dòng)搶紅包呢,下面我們來(lái)一起看看吧。2016-08-08
Android圖片上傳實(shí)現(xiàn)預(yù)覽效果
這篇文章主要介紹了Android圖片上傳實(shí)現(xiàn)預(yù)覽效果的相關(guān)資料,需要的朋友可以參考下2016-01-01
Flutter 用自定義轉(zhuǎn)場(chǎng)動(dòng)畫(huà)實(shí)現(xiàn)頁(yè)面切換
本篇介紹了 fluro 導(dǎo)航到其他頁(yè)面的自定義轉(zhuǎn)場(chǎng)動(dòng)畫(huà)實(shí)現(xiàn),F(xiàn)lutter本身提供了不少預(yù)定義的轉(zhuǎn)場(chǎng)動(dòng)畫(huà),可以通過(guò) transitionBuilder 參數(shù)設(shè)計(jì)多種多樣的轉(zhuǎn)場(chǎng)動(dòng)畫(huà),也可以通過(guò)自定義的 AnimatedWidget實(shí)現(xiàn)個(gè)性化的轉(zhuǎn)場(chǎng)動(dòng)畫(huà)效果。2021-06-06
Android自定義環(huán)形LoadingView效果
這篇文章主要為大家詳細(xì)介紹了Android自定義環(huán)形LoadingView效果的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
android基于ListView和CheckBox實(shí)現(xiàn)多選和全選記錄的功能
本篇文章主要介紹了android基于ListView和CheckBox實(shí)現(xiàn)多選和全選記錄的功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-11-11
Android MediaPlayer 音頻倍速播放 調(diào)整播放速度問(wèn)題
這篇文章主要介紹了Android MediaPlayer 音頻倍速播放,調(diào)整播放速度,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09

