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

首頁 > 系統 > Android > 正文

Android自定義倒計時按鈕

2019-10-22 18:11:22
字體:
來源:轉載
供稿:網友

本文實例為大家分享了Android自定義倒計時按鈕的具體代碼,供大家參考,具體內容如下

效果

Android,倒計時,按鈕

Android,倒計時,按鈕

代碼:

 

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;//開始執行倒計時  private TimerTask timerTask;//每次倒計時執行的任務  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;    }  }  /**   * 設置倒計時時長   *   * @param lenght 默認毫秒   */  public void setLenght(long lenght) {    this.lenght = lenght;  }  /**   * 設置未點擊時顯示的文字   *   * @param beforeText   */  public void setBeforeText(String beforeText) {    this.beforeText = beforeText;  }  /**   * 設置未點擊后顯示的文字   *   * @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);      }    };  }  /**   * 設置監聽按鈕點擊事件   *   * @param onclickListener   */  @Override  public void setOnClickListener(OnClickListener onclickListener) {    if (onclickListener instanceof CountdownButton) {      super.setOnClickListener(onclickListener);    } else {      this.onClickListener = onclickListener;    }  }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 康马县| 马鞍山市| 台东县| 蓬安县| 介休市| 洛南县| 广东省| 乐都县| 获嘉县| 镇沅| 深州市| 威信县| 和顺县| 祁门县| 蛟河市| 油尖旺区| 岳普湖县| 武胜县| 黔江区| 镇远县| 获嘉县| 林州市| 塔城市| 东乌珠穆沁旗| 济阳县| 拉萨市| 普兰县| 罗源县| 福鼎市| 宿松县| 唐海县| 商洛市| 灵丘县| 通渭县| 侯马市| 平谷区| 交城县| 墨竹工卡县| 交城县| 尼玛县| 上饶市|