Android自定義倒計時按鈕
本文實例為大家分享了Android自定義倒計時按鈕的具體代碼,供大家參考,具體內(nèi)容如下
效果
代碼:
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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android xUtils更新到3.0后的基本使用規(guī)則詳解
xUtils是基于android的開發(fā)框架,簡化了很多的開發(fā)步驟,可以說是非常好的開發(fā)工具。下面小編給大家?guī)砹薃ndroid xUtils更新到3.0后的基本使用規(guī)則詳解,感興趣的朋友一起學(xué)習(xí)吧2016-08-08關(guān)于Android?Webview?設(shè)置Cookie問題詳解
大家好,本篇文章是關(guān)于Android?Webview?設(shè)置Cookie問題詳解,感興趣的同學(xué)可以看看,希望對你起到幫助,有用的話記得收藏,方便下次瀏覽2021-11-11Android入門之使用RecyclerView完美實現(xiàn)瀑布流界面詳解
網(wǎng)上充滿著不完善的基于RecyclerView的瀑布流實現(xiàn),要么根本是錯的、要么就是只知其一不知其二。本文就來用RecyclerView完美實現(xiàn)瀑布流界面,希望大家有所幫助2023-02-02Android Listview notifyDataSetChanged() 不起作用的
這篇文章主要介紹了Android Listview notifyDataSetChanged()不起作用的解決方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-08-08android實現(xiàn)圖片驗證碼方法解析(自繪控件)
本文主要介紹了android自繪控件的應(yīng)用--實現(xiàn)圖片驗證碼方法案例,具有一定的參考價值,下面跟著小編一起來看下吧2017-01-01Android開發(fā)模仿qq視頻通話懸浮按鈕(實例代碼)
這篇文章主要介紹了Android開發(fā)模仿qq視頻通話懸浮按鈕功能的實例代碼,需要的的朋友參考下2017-02-02android實現(xiàn)App活動定時自動跳轉(zhuǎn)效果
本篇文章主要介紹了android實現(xiàn)App活動定時自動跳轉(zhuǎn)效果,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02