android 倒计时 TimeButton控件

1、TimeButton 类,其中Const中放一个static的map

importjava.util.HashMap;
 
importjava.util.Map;

importjava.util.Timer;

importjava.util.TimerTask;

importandroid.annotation.SuppressLint;

importandroid.content.Context;

importandroid.os.Bundle;

importandroid.os.Handler;

importandroid.support.v7.widget.AppCompatButton;

importandroid.util.AttributeSet;

importandroid.util.Log;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

importcom.jari.ext.utl.Const;

public classTimeButtonextendsAppCompatButtonimplementsView.OnClickListener {

private longlength=60*1000;//倒计时长度,这里给了默认60秒

privateStringtextafter="秒后重新获取";

privateStringtextbefore="点击获取验证码";

private finalStringTIME="time";

private finalStringCTIME="ctime";

privateOnClickListenermOnclickListener;

privateTimert;

privateTimerTasktt;

private longtime;

privateContextmContext;

Mapmap=newHashMap();

publicTimeButton(Context context) {

super(context);

setOnClickListener(this);

}

publicTimeButton(Context context,AttributeSet attrs) {

super(context,attrs);

mContext= context;

setOnClickListener(this);

}

@SuppressLint("HandlerLeak")

Handlerhan=newHandler() {

public voidhandleMessage(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 voidinitTimer() {

time=length;

t=newTimer();

tt=newTimerTask() {

@Override

public voidrun() {

Log.e("yung",time/1000+"");

han.sendEmptyMessage(0x01);

}

};

}

private voidclearTimer() {

if(tt!=null) {

tt.cancel();

tt=null;

}

if(t!=null)

t.cancel();

t=null;

}

@Override

public voidsetOnClickListener(OnClickListener l) {

if(linstanceofTimeButton) {

super.setOnClickListener(l);

}else

this.mOnclickListener= l;

}

@Override

public voidonClick(View v) {

if(mOnclickListener!=null)

mOnclickListener.onClick(v);

initTimer();

this.setText(time/1000+textafter);

this.setEnabled(false);

t.schedule(tt,0,1000);

}

/**

*和activity的onDestroy()方法同步

*/

public voidonDestroy() {

if(Const.map==null)

Const.map=newHashMap();

Const.map.put(TIME,time);

Const.map.put(CTIME,System.currentTimeMillis());

clearTimer();

}

/**

*和activity的onCreate()方法同步

*/

public voidonCreate(Bundle bundle) {

Log.e("yung",Const.map+"");

if(Const.map==null)

return;

if(Const.map.size() <=0)//这里表示没有上次未完成的计时

return;

longtime = System.currentTimeMillis() - Const.map.get(CTIME)

- Const.map.get(TIME);

Const.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);

}

}

/**

*设置计时时候显示的文本

*/

publicTimeButtonsetTextAfter(String text1) {

this.textafter= text1;

return this;

}

/**

*设置点击之前的文本

*/

publicTimeButtonsetTextBefore(String text0) {

this.textbefore= text0;

this.setText(textbefore);

return this;

}

/**

*设置到计时长度

*

*@paramlenght时间 默认毫秒

*@return

*/

publicTimeButtonsetLenght(longlenght) {

this.length= lenght;

return this;
}
}

2、使用

btnTime.setTextAfter("秒后可以重新发送").setTextBefore("发送验证码").setLenght(60*1000);
btnTime.setOnClickListener(newView.OnClickListener() {
@Override
public voidonClick(View v) {
}
});
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,014评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,780评论 18 399
  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,814评论 0 9
  • 莲花生的传说 给他带来了无尚荣光 却也把悄然而至的悲伤交杂在一起 使他成了孤独的活佛 佛法禅宗无法清净他世俗的心 ...
    乌托邦1990阅读 635评论 0 5
  • 1、姓名:秀秀 2、职业:数学启蒙老师 3、爱好:书法、喝茶、心灵成长 4、主题:行动引领着我走向创业之路 大家好...
    数学启蒙秀秀阅读 541评论 0 50