Android中怎么自定義一個倒計時按鈕

本篇文章為大家展示了Android中怎么自定義一個倒計時按鈕,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

創(chuàng)新互聯(lián)公司是一家專業(yè)提供皮山企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站制作、成都做網(wǎng)站、HTML5建站、小程序制作等業(yè)務(wù)。10年已為皮山眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進行中。

代碼:

package com.dylan.frame.ui;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 自定義倒計時按鈕
 * <p/>
 *
 * @author Dylan
 *   <p/>
 *   [佛祖保佑 永無BUG]
 *   Created by Dylan on 2015/11/5 0005.
 */
public class CountdownButton extends Button implements View.OnClickListener {
 private long lenght = 60 * 1000;//默認倒計時時間;
 private long time;//倒計時時長
 private Timer timer;//開始執(zhí)行倒計時
 private TimerTask timerTask;//每次倒計時執(zhí)行的任務(wù)
 private String beforeText = "點擊獲取驗證碼";
 private String afterText = "秒后重新獲取";
 private OnClickListener onClickListener;//按鈕點擊事件
 /**
  * 更新顯示的文本
  */
 private Handler handler = new Handler() {
  @Override
  public void handleMessage(Message msg) {
   super.handleMessage(msg);
   CountdownButton.this.setText(time / 1000 + afterText);
   time -= 1000;
   if (time < 0) {
    CountdownButton.this.setEnabled(true);

    CountdownButton.this.setText(beforeText);

    clearTimer();
   }
  }
 };


 public CountdownButton(Context context) {
  super(context);
  this.setText(beforeText);
  setOnClickListener(this);
 }

 public CountdownButton(Context context, AttributeSet attrs) {
  super(context, attrs);
  setOnClickListener(this);
 }

 public CountdownButton(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  setOnClickListener(this);
 }

 /**
  * 清除倒計時
  */
 private void clearTimer() {
  if (timerTask != null) {
   timerTask.cancel();
   timerTask = null;
  }
  if (timer != null) {
   timer.cancel();
   timer = null;
  }
 }

 /**
  * 設(shè)置倒計時時長
  *
  * @param lenght 默認毫秒
  */
 public void setLenght(long lenght) {
  this.lenght = lenght;
 }

 /**
  * 設(shè)置未點擊時顯示的文字
  *
  * @param beforeText
  */
 public void setBeforeText(String beforeText) {
  this.beforeText = beforeText;
 }

 /**
  * 設(shè)置未點擊后顯示的文字
  *
  * @param beforeText
  */
 public void setAfterText(String afterText) {
  this.afterText = afterText;
 }

 /**
  * 點擊按鈕后的操作
  *
  * @param v
  */
 @Override
 public void onClick(View v) {
  if (onClickListener != null) {
   onClickListener.onClick(v);
  }
  initTimer();
  this.setText(time / 1000 + afterText);
  this.setEnabled(false);
  timer.schedule(timerTask, 0, 1000);
 }

 /**
  * 初始化時間
  */
 private void initTimer() {
  time = lenght;
  timer = new Timer();
  timerTask = new TimerTask() {
   @Override
   public void run() {
    handler.sendEmptyMessage(1);
   }
  };
 }

 /**
  * 設(shè)置監(jiān)聽按鈕點擊事件
  *
  * @param onclickListener
  */
 @Override
 public void setOnClickListener(OnClickListener onclickListener) {
  if (onclickListener instanceof CountdownButton) {
   super.setOnClickListener(onclickListener);

  } else {
   this.onClickListener = onclickListener;
  }

 }
}

上述內(nèi)容就是Android中怎么自定義一個倒計時按鈕,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

新聞標(biāo)題:Android中怎么自定義一個倒計時按鈕
轉(zhuǎn)載注明:http://muchs.cn/article44/jpgphe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、網(wǎng)站維護、電子商務(wù)、Google、網(wǎng)站設(shè)計公司、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護公司