Android自定義TimeButton實(shí)現(xiàn)倒計(jì)時(shí)按鈕
項(xiàng)目需要要實(shí)現(xiàn)一個(gè)帶有倒計(jì)時(shí)功能的按鈕,其效果類似發(fā)送驗(yàn)證碼之后在按鈕上顯示倒計(jì)時(shí)并且將按鈕設(shè)置為不可用的功能。
為了項(xiàng)目中其他地方能夠調(diào)用到,便重寫了一個(gè)繼承于Button的TimeButton來實(shí)現(xiàn)倒計(jì)時(shí)功能,并方便調(diào)用。
老規(guī)矩,上效果圖:
邏輯也不復(fù)雜,直接上代碼:
首先新建一個(gè)App.class繼承于Application
package com.example.xuboyu.myapplication; /** * 用于存放倒計(jì)時(shí)時(shí)間 * @author bnuzlbs-xuboyu 2017/4/5. */ import java.util.Map; import android.app.Application; public class App extends Application { // 用于存放倒計(jì)時(shí)時(shí)間 public static Map<String, Long> map; }
然后編寫TimeButton.class繼承于Button
package com.example.xuboyu.myapplication; import java.util.HashMap; import java.util.Map; import java.util.Timer; import java.util.TimerTask; import android.annotation.SuppressLint; import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; /** * 倒計(jì)時(shí)按鈕 * @author bnuzlbs-xuboyu 2017/4/5. * 注意把該類的onCreate()onDestroy()和activity的onCreate()onDestroy()同步處理 */ public class TimeButton extends Button implements OnClickListener { private long lenght = 60 * 1000;// 倒計(jì)時(shí)長(zhǎng)度,這里給了默認(rèn)60秒 private String textafter = "秒后重新獲取~"; private String textbefore = "點(diǎn)擊獲取驗(yàn)證碼~"; private int colorafter; private int colorbefore; private final String TIME = "time"; private final String CTIME = "ctime"; private OnClickListener mOnclickListener; private Timer t; private TimerTask tt; private long time; 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); 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 = lenght; t = new Timer(); tt = new TimerTask() { @Override public void run() { Log.e("xuboyu", time / 1000 + ""); han.sendEmptyMessage(0x01);//十六進(jìn)制的數(shù)字1 } }; } private void clearTimer() { 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); // t.scheduleAtFixedRate(task, delay, period); } /** * 和activity的onDestroy()方法同步 */ public void onDestroy() { if (App.map == null) App.map = new HashMap<String, Long>(); App.map.put(TIME, time); App.map.put(CTIME, System.currentTimeMillis()); clearTimer(); Log.e("xuboyu", "onDestroy"); } /** * 和activity的onCreate()方法同步 */ public void onCreate(Bundle bundle) { Log.e("xuboyu:倒計(jì)時(shí)相關(guān)", App.map + ""); if (App.map == null) return; if (App.map.size() <= 0)// 這里表示沒有上次未完成的計(jì)時(shí) return; long time = System.currentTimeMillis() - App.map.get(CTIME) - App.map.get(TIME); App.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.lenght = lenght; return this; } }
最后在MainActivity.class中調(diào)用
package com.example.xuboyu.myapplication; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Toast; /** * 測(cè)試主界面 * @author bnuzlbs-xuboyu 2017/4/5. */ public class MainActivity extends Activity implements OnClickListener { private TimeButton v; private TimeButton v2; private TimeButton v3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); v = (TimeButton) findViewById(R.id.button1); v.onCreate(savedInstanceState); v.setTextAfter("秒后重新排隊(duì)").setTextBefore("點(diǎn)擊開始排隊(duì)").setLenght(15 * 1000); v.setOnClickListener(this); v2 = (TimeButton) findViewById(R.id.button2); v2.onCreate(savedInstanceState); v2.setTextAfter("秒后重新驗(yàn)證").setTextBefore("點(diǎn)擊發(fā)送驗(yàn)證碼").setLenght(10 * 1000); v2.setOnClickListener(this); v3 = (TimeButton) findViewById(R.id.button3); v3.onCreate(savedInstanceState); v3.setTextAfter("秒后重新倒計(jì)時(shí)").setTextBefore("點(diǎn)擊開始倒計(jì)時(shí)").setLenght(5 * 1000); v3.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "這是處理調(diào)用者onclicklistnenr", Toast.LENGTH_SHORT).show(); } @Override protected void onDestroy() { // TODO Auto-generated method stub v.onDestroy(); v2.onDestroy(); super.onDestroy(); } }
其中綠色按鈕是使用了自定義樣式的Button,使用起來也很簡(jiǎn)單
首先在drawable中新建一個(gè)樣式文件mybutton.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#5cbe6c" /> <!-- 設(shè)置按鈕的四個(gè)角為弧形 --> <!-- android:radius 弧形的半徑 --> <corners android:radius="15dip" /> <!-- padding:Button里面的文字與Button邊界的間隔 --> <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" /> </shape>
然后在定義TimeButton的時(shí)候如下:
android:background="@drawable/mybutton"
<com.example.xuboyu.myapplication.TimeButton android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="" android:background="@drawable/mybutton" android:layout_margin="20dp"/>
那么定義出來的Button樣式就為下圖:
記得在AndroidManifest.xml中的Application添加:
android:name=".App"
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme" android:name=".App"> <activity android:name=".MainActivity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
Ps.這個(gè)倒計(jì)時(shí)按鈕存在一個(gè)問題,對(duì)于長(zhǎng)時(shí)間計(jì)時(shí)而言,用戶可能在計(jì)時(shí)后退出應(yīng)用程序,如果用戶把我們的APP置于后臺(tái),那么OK,我們的倒計(jì)時(shí)還是可以進(jìn)行,但是假如用戶在退出后把APP進(jìn)程滑掉,或者使用了其他軟件清理后臺(tái)等等,就會(huì)執(zhí)行OnDestory方法,再次進(jìn)去APP的時(shí)候只能重新建立一個(gè)Timer。所以打算的是使用輕量級(jí)存儲(chǔ)來儲(chǔ)存每次退出后的倒計(jì)時(shí)數(shù)據(jù),然后在重新OnCreate的時(shí)候?yàn)門imer賦值。當(dāng)然對(duì)于短時(shí)間的計(jì)時(shí),即在用戶可接受的等待范圍內(nèi)是完全可以接受的!有Bug也歡迎指出,對(duì)于應(yīng)用進(jìn)程被銷毀時(shí)Timer也銷毀這個(gè)問題假如你有更好的解決方法,也請(qǐng)多指教!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android使用Kotlin API實(shí)踐WorkManager
- 使用Android studio3.6的java api方式調(diào)用opencv
- Android 通過API獲取數(shù)據(jù)庫中的圖片文件方式
- Android 中TeaPickerView數(shù)據(jù)級(jí)聯(lián)選擇器功能的實(shí)例代碼
- Android音視頻之視頻采集(系統(tǒng)API預(yù)覽)
- Android CountDownTimer案例總結(jié)
- Android日歷控件PickTime代碼實(shí)例
- Android倒計(jì)時(shí)神器(CountDownTimer)
- Android中各種Time API詳細(xì)
相關(guān)文章
Android使用HorizontalScrollView實(shí)現(xiàn)水平滾動(dòng)
這篇文章主要為大家詳細(xì)介紹了Android使用HorizontalScrollView實(shí)現(xiàn)水平滾動(dòng),并點(diǎn)擊有相應(yīng)的反應(yīng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11Android實(shí)現(xiàn)計(jì)時(shí)與倒計(jì)時(shí)的常用方法小結(jié)
這篇文章主要介紹了Android實(shí)現(xiàn)計(jì)時(shí)與倒計(jì)時(shí)的常用方法,總結(jié)并對(duì)比分析了幾種常用計(jì)時(shí)方法的特點(diǎn),具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10android實(shí)現(xiàn)多圖文分享朋友圈功能
本文主要介紹android調(diào)用系統(tǒng)分享實(shí)現(xiàn)朋友圈同時(shí)分享文字和圖片,圖片還可以多圖添加分享的功能。2017-11-11Android自定義View實(shí)現(xiàn)仿1號(hào)店垂直滾動(dòng)廣告條代碼
這篇文章主要介紹了Android自定義View實(shí)現(xiàn)仿1號(hào)店垂直滾動(dòng)廣告條代碼,實(shí)現(xiàn)步驟及實(shí)現(xiàn)原理本文給大家介紹的非常詳細(xì),需要的朋友參考下吧2017-01-01Eclipse打開時(shí)“發(fā)現(xiàn)了以元素''d:skin''”開頭的無效內(nèi)容。此處不應(yīng)含有子元素的解決方法
這篇文章主要介紹了Eclipse打開時(shí)“發(fā)現(xiàn)了以元素'd:skin'”開頭的無效內(nèi)容。此處不應(yīng)含有子元素的解決方法,涉及Android sdk中devices.xml文件的修改,需要的朋友可以參考下2016-01-01Android中判斷網(wǎng)絡(luò)是否可用的代碼分享
這篇文章主要介紹了Android中判斷網(wǎng)絡(luò)是否可用的代碼分享,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-03-03