溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android 簡單封裝獲取驗證碼倒計時功能

發(fā)布時間:2020-10-22 08:27:35 來源:腳本之家 閱讀:130 作者:萌動小彩筆 關(guān)注 欄目:移動開發(fā)

效果如下圖所示:

Android 簡單封裝獲取驗證碼倒計時功能 

如圖所示的效果相信大家都不陌生,我們可以使用很多種方法去實現(xiàn)此效果,這里自己采用 CountDownTimer 定時器簡單封裝下此效果,方便我們隨時調(diào)用。

首頁先在 attrs.xml 中定義下所需的幾個屬性:

<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>

下面是實現(xiàn)的具體代碼,很簡單的一種方式,通俗易懂:

/**
 * Created by xiaolong on 2018/1/12.
 */
@SuppressLint("AppCompatCustomView")
public class CountDownButton extends Button{
 //總時長
 private long millisinfuture;
 //間隔時長
 private long countdowninterva;
 //默認背景顏色
 private int normalColor;
 //倒計時 背景顏色
 private int countDownColor;
 //是否結(jié)束
 private boolean isFinish;
 //定時器
 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è)置默認時長
  millisinfuture = (long) typedArray.getInt(R.styleable.CountDownButton_millisinfuture,60000);
  //設(shè)置默認間隔時長
  countdowninterva = (long)typedArray.getInt(R.styleable.CountDownButton_countdowninterva,1000);
  //設(shè)置默認背景色
  normalColor = typedArray.getColor(R.styleable.CountDownButton_normalColor,android.R.color.holo_blue_light);
  //設(shè)置默認倒計時 背景色
  countDownColor = typedArray.getColor(R.styleable.CountDownButton_countDownColor,android.R.color.darker_gray);
  typedArray.recycle();
  //默認為已結(jié)束狀態(tài)
  isFinish = true;
  //字體居中
  setGravity(Gravity.CENTER);
  //默認文字和背景色
  normalBackground();
  //設(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("獲取驗證碼");
  setBackgroundResource(normalColor);
 }
 public boolean isFinish() {
  return isFinish;
 }
 public void cancel(){
  countDownTimer.cancel();
 }
 public void start(){
  countDownTimer.start();
 }
}

一個簡單的調(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) {
    //這里判斷是否倒計時結(jié)束,避免在倒計時時多次點擊導(dǎo)致重復(fù)請求接口
    if (countDownButton.isFinish()) {
     //發(fā)送驗證碼請求成功后調(diào)用
     countDownButton.start();
    }
   }
  });
 }
 @Override
 protected void onDestroy() {
  super.onDestroy();
  if (!countDownButton.isFinish()) {
   countDownButton.cancel();
  }
 }
}

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

總結(jié)

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

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI