Android實(shí)現(xiàn)倒計(jì)時(shí)的按鈕效果
最近有人問我如何實(shí)現(xiàn)倒計(jì)時(shí)的按鈕功能,例如發(fā)送驗(yàn)證碼,我記得有個(gè)CountDownTimer,因?yàn)楹镁脹]用過了,自己就寫了一個(gè),代碼如下
new CountDownTimer(10000, 1000) { @Override public void onTick(long millisUntilFinished) { btn2.setEnabled(false); btn2.setText(String.format("%ds后重新發(fā)送驗(yàn)證碼",millisUntilFinished/1000)); } @Override public void onFinish() { btn2.setEnabled(true); btn2.setText("發(fā)送驗(yàn)證碼"); } }.start();
點(diǎn)擊按鈕后開始倒計(jì)時(shí),貌似很簡(jiǎn)單啊,但是運(yùn)行起來發(fā)現(xiàn)有一些問題,先給大家看效果圖
我們打印一下時(shí)間
這里我們可以看到8這個(gè)秒數(shù)沒有出現(xiàn),并且最后1秒的時(shí)間有些長(zhǎng),每次點(diǎn)擊開始倒計(jì)時(shí)的時(shí)候偶爾就會(huì)出現(xiàn)少一個(gè)數(shù)字的問題,所以說這個(gè)東西是不精確的,網(wǎng)上也有很多人再說,那么有沒有其他的實(shí)現(xiàn)思路呢?這里我們來自定義一個(gè)倒計(jì)時(shí)的按鈕
public class TimeButton extends Button implements View.OnClickListener { private long length = 60 * 1000;// 倒計(jì)時(shí)長(zhǎng)度,這里給了默認(rèn)60秒 private String textafter = "秒后重新獲取"; private String textbefore = "點(diǎn)擊獲取驗(yàn)證碼"; private final String TIME = "time"; private final String CTIME = "ctime"; private OnClickListener mOnclickListener; private Timer t; private TimerTask tt; private long time; private Context mContext; Map<String, Long> map = new HashMap<String, Long>(); public TimeButton(Context context) { super(context); setOnClickListener(this); } public TimeButton(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; setOnClickListener(this); } @SuppressLint("HandlerLeak") Handler han = new Handler() { public void handleMessage(android.os.Message msg) { TimeButton.this.setText(time / 1000 + textafter); time -= 1000; if (time < 0) { TimeButton.this.setEnabled(true); TimeButton.this.setText(textbefore); clearTimer(); } } }; private void initTimer() { time = length; t = new Timer(); tt = new TimerTask() { @Override public void run() { Log.e("yung", time / 1000 + ""); han.sendEmptyMessage(0x01); } }; } private void clearTimer() { Toast.makeText(mContext, "計(jì)時(shí)結(jié)束", Toast.LENGTH_SHORT).show(); if (tt != null) { tt.cancel(); tt = null; } if (t != null) t.cancel(); t = null; } @Override public void setOnClickListener(OnClickListener l) { if (l instanceof TimeButton) { super.setOnClickListener(l); } else this.mOnclickListener = l; } @Override public void onClick(View v) { if (mOnclickListener != null) mOnclickListener.onClick(v); initTimer(); this.setText(time / 1000 + textafter); this.setEnabled(false); t.schedule(tt, 0, 1000); } /** * 和activity的onDestroy()方法同步 */ public void onDestroy() { if (MainActivity.map == null) MainActivity.map = new HashMap<String, Long>(); MainActivity.map.put(TIME, time); MainActivity.map.put(CTIME, System.currentTimeMillis()); clearTimer(); } /** * 和activity的onCreate()方法同步 */ public void onCreate(Bundle bundle) { Log.e("yung", MainActivity.map + ""); if (MainActivity.map == null) return; if (MainActivity.map.size() <= 0)// 這里表示沒有上次未完成的計(jì)時(shí) return; long time = System.currentTimeMillis() - MainActivity.map.get(CTIME) - MainActivity.map.get(TIME); MainActivity.map.clear(); if (time > 0) return; else { initTimer(); this.time = Math.abs(time); t.schedule(tt, 0, 1000); this.setText(time + textafter); this.setEnabled(false); } } /** * 設(shè)置計(jì)時(shí)時(shí)候顯示的文本 */ public TimeButton setTextAfter(String text1) { this.textafter = text1; return this; } /** * 設(shè)置點(diǎn)擊之前的文本 */ public TimeButton setTextBefore(String text0) { this.textbefore = text0; this.setText(textbefore); return this; } /** * 設(shè)置到計(jì)時(shí)長(zhǎng)度 * * @param lenght 時(shí)間 默認(rèn)毫秒 * @return */ public TimeButton setLenght(long lenght) { this.length = lenght; return this; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)啟動(dòng)頁倒計(jì)時(shí)效果
- Android 實(shí)現(xiàn)搶購倒計(jì)時(shí)功能的示例
- android實(shí)現(xiàn)倒計(jì)時(shí)動(dòng)態(tài)圈
- android實(shí)現(xiàn)圓環(huán)倒計(jì)時(shí)控件
- android利用handler實(shí)現(xiàn)倒計(jì)時(shí)功能
- Android自定義view實(shí)現(xiàn)倒計(jì)時(shí)控件
- 解決Android-RecyclerView列表倒計(jì)時(shí)錯(cuò)亂問題
- Android實(shí)現(xiàn)自定義倒計(jì)時(shí)
- Android 倒計(jì)時(shí)控件 CountDownView的實(shí)例代碼詳解
- Android倒計(jì)時(shí)神器(CountDownTimer)
- Android倒計(jì)時(shí)功能的實(shí)現(xiàn)代碼
- Android 簡(jiǎn)單實(shí)現(xiàn)倒計(jì)時(shí)功能
- Android自定義TimeButton實(shí)現(xiàn)倒計(jì)時(shí)按鈕
- 利用Android設(shè)計(jì)一個(gè)倒計(jì)時(shí)組件
相關(guān)文章
Android 中 ActivityLifecycleCallbacks的實(shí)例詳解
這篇文章主要介紹了Android 中 ActivityLifecycleCallbacks的實(shí)例詳解的相關(guān)資料,希望通過本文大家能掌握這部分內(nèi)容,需要的朋友可以參考下2017-09-09Android自定義LocationMarker的實(shí)現(xiàn)詳解
這篇文章主要為大家詳細(xì)介紹一個(gè)比較簡(jiǎn)單的東西:自定義繪制Marker 其實(shí)就是自定義view, 跟軌跡沒太多關(guān)聯(lián),感興趣的小伙伴可以跟隨小編一起了解一下2023-02-02android studio實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了android studio實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05Android使用百度地圖出現(xiàn)閃退及定位時(shí)顯示藍(lán)屏問題的解決方法
這篇文章主要介紹了Android使用百度地圖出現(xiàn)閃退及定位時(shí)顯示藍(lán)屏問題的解決方法,需要的朋友可以參考下2018-01-01Android自定義下拉刷新控件RefreshableView
這篇文章主要介紹了Android自定義下拉刷新控件RefreshableView,支持所有控件的下拉刷新,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Android SharedPreferences存儲(chǔ)的正確寫法
這篇文章主要介紹了Android SharedPreferences存儲(chǔ)的正確寫法的相關(guān)資料,需要的朋友可以參考下2017-06-06Android自定義超級(jí)炫酷的ViewPage指示器
由于應(yīng)公司開發(fā)要求,有一個(gè)顏色漸變帶縮放的指示器,雖然網(wǎng)上很多大佬開源的指示器開源庫,但如果一直都是使用別人造的輪子,那么對(duì)于自身的能力是毫無提升作用的,即使是參考別人的,然后自己動(dòng)手寫一遍那對(duì)于自身來說也是一種升華2022-07-07android語音即時(shí)通訊之錄音、播放功能實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了android語音即時(shí)通訊之錄音、播放功能的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07android viewpager實(shí)現(xiàn)豎直滑動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了android viewpager實(shí)現(xiàn)豎直滑動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07Android ListView數(shù)據(jù)的分批顯示功能
本文通過實(shí)例代碼給大家分享了Android ListView數(shù)據(jù)的分批顯示功能,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友參考下吧2017-04-04