Button的点击倒计时

来源:互联网 发布:程序员工作体会 编辑:程序博客网 时间:2024/05/17 06:06

鉴于经常在注册获取验证码时候需要一个倒计时按钮觉得很麻烦,抽点时间写了一个TimeButton

开始一直为Button既要接受用户的onclick事件纠结,因为我封装的里面也是用onclick事件来实现点击效果的

这样势必有两个onclick事件而产生冲突,随后还是解决了,在封装的类里面定义了一个onlick变量储存用户的onclick

事件.而刚好本身的onclick事件和用户的是同步的只要到时候调用下就OK..

简述下特性,

TimeButton使用的时候跟普通Button一样没有冲突,

TimeButton在倒计时的时候返回了如果没有超过剩下的时间再次进入会继续跑时哦,

TimeButton里面完成了按钮的点击显示和倒计时逻辑,

TimeButton在activity销毁后也会销毁不会像线程一样还在后台跑,

TimeButton的显示文字和倒计时都可以自行设置当然也有默认的,
原创地址 http://blog.csdn.net/yung7086/article/details/43563033

贴出代码来,首先需要在Application定义一个Map 

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. public class App extends Application {  
  2.   
  3.     // 用于存放倒计时时间  
  4.     public static Map<String, Long> map;  
  5. }  
然后在activity如此使用
[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package com.yung.timebutton;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Toast;  
  8.   
  9. /** 
  10.  * 测试主界面 
  11.  *  
  12.  * @author yung 
  13.  *         <P> 
  14.  *         2015年1月14日 13:00:26 
  15.  */  
  16. public class MainActivity extends Activity implements OnClickListener {  
  17.   
  18.     private TimeButton v;  
  19.   
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.activity_main);  
  24.         v = (TimeButton) findViewById(R.id.button1);  
  25.         v.onCreate(savedInstanceState);  
  26.         v.setTextAfter("秒后重新获取").setTextBefore("点击获取验证码").setLenght(15 * 1000);  
  27.         v.setOnClickListener(this);  
  28.     }  
  29.   
  30.     @Override  
  31.     public void onClick(View v) {  
  32.         // TODO Auto-generated method stub  
  33.         Toast.makeText(MainActivity.this"这是处理调用者onclicklistnenr",  
  34.                 Toast.LENGTH_SHORT).show();  
  35.     }  
  36.   
  37.     @Override  
  38.     protected void onDestroy() {  
  39.         // TODO Auto-generated method stub  
  40.         v.onDestroy();  
  41.         super.onDestroy();  
  42.     }  
  43. }  

接下来就是TimeButton的代码
[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package com.yung.timebutton;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5. import java.util.Timer;  
  6. import java.util.TimerTask;  
  7.   
  8. import android.annotation.SuppressLint;  
  9. import android.content.Context;  
  10. import android.os.Bundle;  
  11. import android.os.Handler;  
  12. import android.util.AttributeSet;  
  13. import android.util.Log;  
  14. import android.view.View;  
  15. import android.view.View.OnClickListener;  
  16. import android.widget.Button;  
  17.   
  18. /** 
  19.  * 鉴于经常用到获取验证码倒计时按钮 在网上也没有找到理想的 自己写一个 
  20.  *  
  21.  *  
  22.  * @author yung 
  23.  *         <P> 
  24.  *         2015年1月14日[佛祖保佑 永无BUG] 
  25.  *         <p> 
  26.  *         PS: 由于发现timer每次cancle()之后不能重新schedule方法,所以计时完毕只恐timer. 
  27.  *         每次开始计时的时候重新设置timer, 没想到好办法初次下策 
  28.  *         注意把该类的onCreate()onDestroy()和activity的onCreate()onDestroy()同步处理 
  29.  *  
  30.  */  
  31. public class TimeButton extends Button implements OnClickListener {  
  32.     private long lenght = 60 * 1000;// 倒计时长度,这里给了默认60秒  
  33.     private String textafter = "秒后重新获取~";  
  34.     private String textbefore = "点击获取验证码~";  
  35.     private final String TIME = "time";  
  36.     private final String CTIME = "ctime";  
  37.     private OnClickListener mOnclickListener;  
  38.     private Timer t;  
  39.     private TimerTask tt;  
  40.     private long time;  
  41.     Map<String, Long> map = new HashMap<String, Long>();  
  42.   
  43.     public TimeButton(Context context) {  
  44.         super(context);  
  45.         setOnClickListener(this);  
  46.   
  47.     }  
  48.   
  49.     public TimeButton(Context context, AttributeSet attrs) {  
  50.         super(context, attrs);  
  51.         setOnClickListener(this);  
  52.     }  
  53.   
  54.     @SuppressLint("HandlerLeak")  
  55.     Handler han = new Handler() {  
  56.         public void handleMessage(android.os.Message msg) {  
  57.             TimeButton.this.setText(time / 1000 + textafter);  
  58.             time -= 1000;  
  59.             if (time < 0) {  
  60.                 TimeButton.this.setEnabled(true);  
  61.                 TimeButton.this.setText(textbefore);  
  62.                 clearTimer();  
  63.             }  
  64.         };  
  65.     };  
  66.   
  67.     private void initTimer() {  
  68.         time = lenght;  
  69.         t = new Timer();  
  70.         tt = new TimerTask() {  
  71.   
  72.             @Override  
  73.             public void run() {  
  74.                 Log.e("yung", time / 1000 + "");  
  75.                 han.sendEmptyMessage(0x01);  
  76.             }  
  77.         };  
  78.     }  
  79.   
  80.     private void clearTimer() {  
  81.         if (tt != null) {  
  82.             tt.cancel();  
  83.             tt = null;  
  84.         }  
  85.         if (t != null)  
  86.             t.cancel();  
  87.         t = null;  
  88.     }  
  89.   
  90.     @Override  
  91.     public void setOnClickListener(OnClickListener l) {  
  92.         if (l instanceof TimeButton) {  
  93.             super.setOnClickListener(l);  
  94.         } else  
  95.             this.mOnclickListener = l;  
  96.     }  
  97.   
  98.     @Override  
  99.     public void onClick(View v) {  
  100.         if (mOnclickListener != null)  
  101.             mOnclickListener.onClick(v);  
  102.         initTimer();  
  103.         this.setText(time / 1000 + textafter);  
  104.         this.setEnabled(false);  
  105.         t.schedule(tt, 01000);  
  106.         // t.scheduleAtFixedRate(task, delay, period);  
  107.     }  
  108.   
  109.     /** 
  110.      * 和activity的onDestroy()方法同步 
  111.      */  
  112.     public void onDestroy() {  
  113.         if (App.map == null)  
  114.             App.map = new HashMap<String, Long>();  
  115.         App.map.put(TIME, time);  
  116.         App.map.put(CTIME, System.currentTimeMillis());  
  117.         clearTimer();  
  118.         Log.e("yung""onDestroy");  
  119.     }  
  120.   
  121.     /** 
  122.      * 和activity的onCreate()方法同步 
  123.      */  
  124.     public void onCreate(Bundle bundle) {  
  125.         Log.e("yung", App.map + "");  
  126.         if (App.map == null)  
  127.             return;  
  128.         if (App.map.size() <= 0)// 这里表示没有上次未完成的计时  
  129.             return;  
  130.         long time = System.currentTimeMillis() - App.map.get(CTIME)  
  131.                 - App.map.get(TIME);  
  132.         App.map.clear();  
  133.         if (time > 0)  
  134.             return;  
  135.         else {  
  136.             initTimer();  
  137.             this.time = Math.abs(time);  
  138.             t.schedule(tt, 01000);  
  139.             this.setText(time + textafter);  
  140.             this.setEnabled(false);  
  141.         }  
  142.     }  
  143.   
  144.     /** * 设置计时时候显示的文本 */  
  145.     public TimeButton setTextAfter(String text1) {  
  146.         this.textafter = text1;  
  147.         return this;  
  148.     }  
  149.   
  150.     /** * 设置点击之前的文本 */  
  151.     public TimeButton setTextBefore(String text0) {  
  152.         this.textbefore = text0;  
  153.         this.setText(textbefore);  
  154.         return this;  
  155.     }  
  156.   
  157.     /** 
  158.      * 设置到计时长度 
  159.      *  
  160.      * @param lenght 
  161.      *            时间 默认毫秒 
  162.      * @return 
  163.      */  
  164.     public TimeButton setLenght(long lenght) {  
  165.         this.lenght = lenght;  
  166.         return this;  
  167.     }  
  168.     /* 
  169.  
  170. * 
  171. */  
  172. }  

代码有点烂了哈,,完成工程待上传
工程地址

http://download.csdn.net/detail/yung7086/8428517 

原创地址
http://write.blog.csdn.net/postedit/43563033

0 0
原创粉丝点击