当下很多app都会采用验证码登录,正好最近项目中用到了,就拿出来跟大家分享。
1.首先定义一个计时器
package com.shangshaban.zhaopin.utils;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.widget.Button;
import com.shangshaban.zhaopin.activity.R;
/**
* 作者: peter on 2016/6/18.
* 发送验证码后的倒计时
*/
public class ShangshabanTimeCount extends CountDownTimer {
private Button button;
public ShangshabanTimeCount(long millisInFuture, long countDownInterval, final Button button) {
super(millisInFuture, countDownInterval);//参数依次为总时长,和计时的时间间隔,要显示的按钮
this.button = button;
}
@Override
public void onTick(long millisUntilFinished) {//计时过程显示
button.setTextColor(Color.parseColor("#c6c6c6"));
button.setBackgroundResource(R.drawable.send_code_wait);
button.setClickable(false);
//c6c6c6
button.setText(millisUntilFinished / 1000 + "秒后重试");
}
@Override
public void onFinish() {//计时完毕时触发
button.setText("再次发送");
button.setTextColor(Color.parseColor("#FB6749"));
button.setBackgroundResource(R.drawable.send_code);
button.setClickable(true);
}
}
2.在你的业务代码中(点击事件),添加如下字段
time = new ShangshabanTimeCount(60000, 1000, btn_post_code);
time.start();
OK ,搞定!