Android自定義倒計(jì)時(shí)按鈕
本文實(shí)例為大家分享了Android自定義倒計(jì)時(shí)按鈕的具體代碼,供大家參考,具體內(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; /** * 自定義倒計(jì)時(shí)按鈕 * <p/> * * @author Dylan * <p/> * [佛祖保佑 永無(wú)BUG] * Created by Dylan on 2015/11/5 0005. */ public class CountdownButton extends Button implements View.OnClickListener { private long lenght = 60 * 1000;//默認(rèn)倒計(jì)時(shí)時(shí)間; private long time;//倒計(jì)時(shí)時(shí)長(zhǎng) private Timer timer;//開(kāi)始執(zhí)行倒計(jì)時(shí) private TimerTask timerTask;//每次倒計(jì)時(shí)執(zhí)行的任務(wù) private String beforeText = "點(diǎn)擊獲取驗(yàn)證碼"; private String afterText = "秒后重新獲取"; private OnClickListener onClickListener;//按鈕點(diǎn)擊事件 /** * 更新顯示的文本 */ 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); } /** * 清除倒計(jì)時(shí) */ private void clearTimer() { if (timerTask != null) { timerTask.cancel(); timerTask = null; } if (timer != null) { timer.cancel(); timer = null; } } /** * 設(shè)置倒計(jì)時(shí)時(shí)長(zhǎng) * * @param lenght 默認(rèn)毫秒 */ public void setLenght(long lenght) { this.lenght = lenght; } /** * 設(shè)置未點(diǎn)擊時(shí)顯示的文字 * * @param beforeText */ public void setBeforeText(String beforeText) { this.beforeText = beforeText; } /** * 設(shè)置未點(diǎn)擊后顯示的文字 * * @param beforeText */ public void setAfterText(String afterText) { this.afterText = afterText; } /** * 點(diǎn)擊按鈕后的操作 * * @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); } /** * 初始化時(shí)間 */ private void initTimer() { time = lenght; timer = new Timer(); timerTask = new TimerTask() { @Override public void run() { handler.sendEmptyMessage(1); } }; } /** * 設(shè)置監(jiān)聽(tīng)按鈕點(diǎn)擊事件 * * @param onclickListener */ @Override public void setOnClickListener(OnClickListener onclickListener) { if (onclickListener instanceof CountdownButton) { super.setOnClickListener(onclickListener); } else { this.onClickListener = onclickListener; } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android xUtils更新到3.0后的基本使用規(guī)則詳解
xUtils是基于android的開(kāi)發(fā)框架,簡(jiǎn)化了很多的開(kāi)發(fā)步驟,可以說(shuō)是非常好的開(kāi)發(fā)工具。下面小編給大家?guī)?lái)了Android xUtils更新到3.0后的基本使用規(guī)則詳解,感興趣的朋友一起學(xué)習(xí)吧2016-08-08關(guān)于Android?Webview?設(shè)置Cookie問(wèn)題詳解
大家好,本篇文章是關(guān)于Android?Webview?設(shè)置Cookie問(wèn)題詳解,感興趣的同學(xué)可以看看,希望對(duì)你起到幫助,有用的話(huà)記得收藏,方便下次瀏覽2021-11-11Android入門(mén)之使用RecyclerView完美實(shí)現(xiàn)瀑布流界面詳解
網(wǎng)上充滿(mǎn)著不完善的基于RecyclerView的瀑布流實(shí)現(xiàn),要么根本是錯(cuò)的、要么就是只知其一不知其二。本文就來(lái)用RecyclerView完美實(shí)現(xiàn)瀑布流界面,希望大家有所幫助2023-02-02Android開(kāi)發(fā)軟鍵盤(pán)遮擋登陸按鈕的完美解決方案
在應(yīng)用登陸頁(yè)面我們需要填寫(xiě)用戶(hù)名和密碼。當(dāng)填寫(xiě)這些信息的時(shí)候,軟鍵盤(pán)會(huì)遮擋登陸按鈕,這使得用戶(hù)體驗(yàn)較差。今天小編給大家分享本教程給大家介紹解決android軟鍵盤(pán)遮擋登陸按鈕的方法,感興趣的朋友一起學(xué)習(xí)吧2016-10-10Android Listview notifyDataSetChanged() 不起作用的
這篇文章主要介紹了Android Listview notifyDataSetChanged()不起作用的解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-08-08android實(shí)現(xiàn)圖片驗(yàn)證碼方法解析(自繪控件)
本文主要介紹了android自繪控件的應(yīng)用--實(shí)現(xiàn)圖片驗(yàn)證碼方法案例,具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01Android開(kāi)發(fā)模仿qq視頻通話(huà)懸浮按鈕(實(shí)例代碼)
這篇文章主要介紹了Android開(kāi)發(fā)模仿qq視頻通話(huà)懸浮按鈕功能的實(shí)例代碼,需要的的朋友參考下2017-02-02android實(shí)現(xiàn)App活動(dòng)定時(shí)自動(dòng)跳轉(zhuǎn)效果
本篇文章主要介紹了android實(shí)現(xiàn)App活動(dòng)定時(shí)自動(dòng)跳轉(zhuǎn)效果,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02