码迷,mamicode.com
首页 > 其他好文 > 详细

验证码

时间:2016-09-18 13:42:46      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

/**
* Description:验证码
* User: lxs
*/
public class VerificationCode {

private Context context;
private TextView code;
private TimeHandler timeHandler;
private long startTime;
private final String second = " S";

public VerificationCode(Context context, TextView code) {
this.context = context;
this.code = code;
timeHandler = new TimeHandler();
}

public void start() {
code.setEnabled(false);
startTime = System.currentTimeMillis();
timeHandler.sendEmptyMessage(1);
}

public void stop() {
startTime = 0;
timeHandler.sendEmptyMessage(0);
}

public void clear() {
code = null;
timeHandler = null;
}

private class TimeHandler extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what > 0) {
int time = 59 - (int) ((System.currentTimeMillis() - startTime) / 1000);
String show = time + second;
code.setText(show);
sendEmptyMessageDelayed(time, 100);
} else {
code.setText(context.getResources().getString(R.string.get_verification_code));
code.setEnabled(true);
timeHandler.removeCallbacksAndMessages(null);
}
}
}
}

<string name="get_verification_code">获取验证码</string>

验证码

标签:

原文地址:http://www.cnblogs.com/QQ80565970/p/5881189.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!