Android自定義TimeButton實(shí)現(xiàn)倒計(jì)時(shí)按鈕
項(xiàng)目需要要實(shí)現(xiàn)一個(gè)帶有倒計(jì)時(shí)功能的按鈕,其效果類(lèi)似發(fā)送驗(yàn)證碼之后在按鈕上顯示倒計(jì)時(shí)并且將按鈕設(shè)置為不可用的功能。
為了項(xiàng)目中其他地方能夠調(diào)用到,便重寫(xiě)了一個(gè)繼承于Button的TimeButton來(lái)實(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;
}
然后編寫(xiě)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.
* 注意把該類(lèi)的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)// 這里表示沒(méi)有上次未完成的計(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)擊開(kāi)始排隊(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)擊開(kāi)始倒計(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,使用起來(lái)也很簡(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"/>
那么定義出來(lái)的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è)問(wèn)題,對(duì)于長(zhǎng)時(shí)間計(jì)時(shí)而言,用戶(hù)可能在計(jì)時(shí)后退出應(yīng)用程序,如果用戶(hù)把我們的APP置于后臺(tái),那么OK,我們的倒計(jì)時(shí)還是可以進(jìn)行,但是假如用戶(hù)在退出后把APP進(jìn)程滑掉,或者使用了其他軟件清理后臺(tái)等等,就會(huì)執(zhí)行OnDestory方法,再次進(jìn)去APP的時(shí)候只能重新建立一個(gè)Timer。所以打算的是使用輕量級(jí)存儲(chǔ)來(lái)儲(chǔ)存每次退出后的倒計(jì)時(shí)數(shù)據(jù),然后在重新OnCreate的時(shí)候?yàn)門(mén)imer賦值。當(dāng)然對(duì)于短時(shí)間的計(jì)時(shí),即在用戶(hù)可接受的等待范圍內(nèi)是完全可以接受的!有Bug也歡迎指出,對(duì)于應(yīng)用進(jìn)程被銷(xiāo)毀時(shí)Timer也銷(xiāo)毀這個(gè)問(wèn)題假如你有更好的解決方法,也請(qǐng)多指教!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android使用Kotlin API實(shí)踐WorkManager
- 使用Android studio3.6的java api方式調(diào)用opencv
- Android 通過(guò)API獲取數(shù)據(jù)庫(kù)中的圖片文件方式
- 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)文章
Kotlin 集合函數(shù)map 和 first 的使用場(chǎng)景分析
Kotlin 提供了許多強(qiáng)大的集合操作函數(shù),其中 map 適用于轉(zhuǎn)換集合,first 適用于獲取符合條件的第一個(gè)元素,這篇文章給大家介紹Kotlin 集合函數(shù):map 和 first 的使用場(chǎng)景,感興趣的朋友一起看看吧2025-04-04
Android Studio 報(bào)錯(cuò)failed to create jvm error code -4的解決方法
這篇文章主要介紹了Android Studio 在安裝后啟動(dòng)報(bào)錯(cuò)failed to create jvm error code -4的解決方法,需要的朋友可以參考下2014-05-05
Android 實(shí)現(xiàn)WebView點(diǎn)擊圖片查看大圖列表及圖片保存功能
這篇文章主要介紹了Android 實(shí)現(xiàn)WebView點(diǎn)擊圖片查看大圖列表及圖片保存功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02
解決Android studio模擬器啟動(dòng)失敗的問(wèn)題
這篇文章主要介紹了Android studio模擬器啟動(dòng)失敗的問(wèn)題及解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
詳解關(guān)于Android Studio中安裝和gradle的一些坑
本篇文章主要介紹了關(guān)于Android Studio中安裝和gradle的一些坑,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
Android實(shí)現(xiàn)在一個(gè)activity中添加多個(gè)listview的方法
這篇文章主要介紹了Android實(shí)現(xiàn)在一個(gè)activity中添加多個(gè)listview的方法,分析了Activity中添加listview的原理與具體實(shí)現(xiàn)方法,需要的朋友可以參考下2016-08-08

