Android中怎么通過自定義TimeButton實(shí)現(xiàn)倒計(jì)時(shí)按鈕

Android中怎么通過自定義TimeButton實(shí)現(xiàn)倒計(jì)時(shí)按鈕,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:主機(jī)域名雅安服務(wù)器托管、營銷軟件、網(wǎng)站建設(shè)、孟州網(wǎng)站維護(hù)、網(wǎng)站推廣。

首先新建一個(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í)長度,這里給了默認(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í)長度 * @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,使用起來也很簡單

首先在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>

關(guān)于Android中怎么通過自定義TimeButton實(shí)現(xiàn)倒計(jì)時(shí)按鈕問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

標(biāo)題名稱:Android中怎么通過自定義TimeButton實(shí)現(xiàn)倒計(jì)時(shí)按鈕
當(dāng)前地址:http://muchs.cn/article32/isjpsc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、網(wǎng)站設(shè)計(jì)、品牌網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站、網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)