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)用到,便重寫(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.
* 注意把該類的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í)而言,用戶可能在計(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ǔ)來(lái)儲(chǔ)存每次退出后的倒計(jì)時(shí)數(shù)據(jù),然后在重新OnCreate的時(shí)候?yàn)門(mén)imer賦值。當(dāng)然對(duì)于短時(shí)間的計(jì)時(shí),即在用戶可接受的等待范圍內(nèi)是完全可以接受的!有Bug也歡迎指出,對(duì)于應(yīng)用進(jìn)程被銷毀時(shí)Timer也銷毀這個(gè)問(wèn)題假如你有更好的解決方法,也請(qǐng)多指教!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:Android 簡(jiǎn)單實(shí)現(xiàn)倒計(jì)時(shí)功能
欄 目:Android
下一篇:android實(shí)現(xiàn)手寫(xiě)簽名功能
本文標(biāo)題:Android自定義TimeButton實(shí)現(xiàn)倒計(jì)時(shí)按鈕
本文地址:http://www.jygsgssxh.com/a1/Android/9021.html
您可能感興趣的文章
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方法詳解
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)登錄
- 01-10android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代碼
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10Emoji表情在Android JNI中的兼容性問(wèn)題詳解
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10android開(kāi)發(fā)環(huán)境中SDK文件夾下的所需內(nèi)容詳解


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方
- 01-10android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10Emoji表情在Android JNI中的兼容性問(wèn)題詳
隨機(jī)閱讀
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置


