验证码
Posted 蹭饭熊
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了验证码相关的知识,希望对你有一定的参考价值。
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>
以上是关于验证码的主要内容,如果未能解决你的问题,请参考以下文章