最近做项目开发时再次用到短信验证码接收的功能,不能忍了,就自己写了一个通用的自定义控件,再也不用重复造轮子了。话不多说,code呈上来
代码实现:
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import com.ktc.base.SczwApplication;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* @Date 2016-5-26 下午3:00:52
* @Author Arvin
* @Description 验证码倒计时控件
*/
public class MyCodeButton extends Button implements OnClickListener {
private long lenghTime = 60 * 1000;// 倒计时时长,默认60秒
private String textafter = "s后再次获取";
private String textbefore = "获取验证码";
private final String TIME = "time";
private final String CTIME = "ctime";
private OnClickListener mOnclickListener;
private Timer timer;
private TimerTask timerTask;
private long time;
Map<String, Long> map = SczwApplication.map;
public MyCodeButton(Context context) {
super(context);
setOnClickListener(this);
}
public MyCodeButton(Context context, AttributeSet attrs) {
super(context, attrs);
setOnClickListener(this);
}
@SuppressLint("HandlerLeak")
Handler han = new Handler() {
public void handleMessage(android.os.Message msg) {
MyCodeButton.this.setText(time / 1000 + textafter);
time -= 1000;
if (time < 0) {
MyCodeButton.this.setEnabled(true);
MyCodeButton.this.setText(textbefore);
clearTimer();
}
};
};
private void initTimer() {
time = lenghTime;
timer = new Timer();
timerTask = new TimerTask() {
@Override
public void run() {
han.sendEmptyMessage(0x01);
}
};
}
private void clearTimer() {
if (timerTask != null) {
timerTask.cancel();
timerTask = null;
}
if (timer != null)
timer.cancel();
timer = null;
}
@Override
public void setOnClickListener(OnClickListener l) {
if (l instanceof MyCodeButton) {
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);
timer.schedule(timerTask, 0, 1000);
// t.scheduleAtFixedRate(task, delay, period);
}
/**
* 和activity的onDestroy()方法同步
*/
public void onDestroy() {
if (map == null)
map = new HashMap<String, Long>();
map.put(TIME, time);
map.put(CTIME, System.currentTimeMillis());
clearTimer();
}
/**
* 和activity的onCreate()方法同步
*/
public void onCreate(Bundle bundle) {
if (map == null)
return;
if (map.size() <= 0)// 这里表示没有上次未完成的计时
return;
long time = System.currentTimeMillis() - map.get(CTIME)
- map.get(TIME);
map.clear();
if (time > 0)
return;
else {
initTimer();
this.time = Math.abs(time);
timer.schedule(timerTask, 0, 1000);
this.setText(time + textafter);
this.setEnabled(false);
}
}
/** * 设置计时时候显示的文本 */
public MyCodeButton setTextAfter(String text1) {
this.textafter = text1;
return this;
}
/** * 设置点击之前的文本 */
public MyCodeButton setTextBefore(String text0) {
this.textbefore = text0;
this.setText(textbefore);
return this;
}
/**
* 设置到计时长度
*
* @param lenght
* 时间 默认毫秒
* @return
*/
public MyCodeButton setLenghTime(long lenghTime) {
this.lenghTime = lenghTime;
return this;
}
}
**xml中像常用Button一样直接用 **
谨以作为开发记录,如果有帮到您,记得点赞哦