国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android 簡單封裝獲取驗(yàn)證碼倒計(jì)時(shí)功能

2019-10-22 18:16:50
字體:
供稿:網(wǎng)友

效果如下圖所示:

android,驗(yàn)證碼,倒計(jì)時(shí),封裝,驗(yàn)證碼倒計(jì)時(shí)

如圖所示的效果相信大家都不陌生,我們可以使用很多種方法去實(shí)現(xiàn)此效果,這里自己采用 CountDownTimer 定時(shí)器簡單封裝下此效果,方便我們隨時(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)的具體代碼,很簡單的一種方式,通俗易懂:

/** * Created by xiaolong on 2018/1/12. */@SuppressLint("AppCompatCustomView")public class CountDownButton extends Button{ //總時(shí)長 private long millisinfuture; //間隔時(shí)長 private long countdowninterva; //默認(rèn)背景顏色 private int normalColor; //android/233299.html">倒計(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í)長  millisinfuture = (long) typedArray.getInt(R.styleable.CountDownButton_millisinfuture,60000);  //設(shè)置默認(rèn)間隔時(shí)長  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è)簡單的調(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ù)請求接口    if (countDownButton.isFinish()) {     //發(fā)送驗(yàn)證碼請求成功后調(diào)用     countDownButton.start();    }   }  }); } @Override protected void onDestroy() {  super.onDestroy();  if (!countDownButton.isFinish()) {   countDownButton.cancel();  } }}

這樣一個(gè)簡單的封裝就結(jié)束了,過程很簡單。這里主要是對 CountDownTimer 的使用練習(xí),之前工作中一直沒有接觸過這個(gè)類。順便貼上源碼吧!

總結(jié)

以上所述是小編給大家介紹的Android 簡單封裝獲取驗(yàn)證碼倒計(jì)時(shí)功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對VEVB武林網(wǎng)網(wǎng)站的支持!


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 洛浦县| 广昌县| 资阳市| 宕昌县| 南华县| 邮箱| 丹东市| 中西区| 黔江区| 乐昌市| 思南县| 金寨县| 东阿县| 恭城| 江永县| 廊坊市| 象州县| 蚌埠市| 沙雅县| 金湖县| 新野县| 中卫市| 响水县| 泸定县| 凤翔县| 安平县| 三河市| 成安县| 开鲁县| 广德县| 陈巴尔虎旗| 平塘县| 大方县| 新田县| 泰顺县| 乌苏市| 东兰县| 兰溪市| 玛曲县| 汤原县| 宁远县|