javaandroid

android 倒计时 TimeButton控件

2017-09-19  本文已影响9人  揪个太阳

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) {
}
});
上一篇 下一篇

猜你喜欢

热点阅读