忘了是看哪位大神代码写好的,代码如下,注释写的挺清楚了,就不说了什么了:
private TimeCount time;//验证码倒计时 private TextView register_get_verification;//点击获取验证码按钮 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register_layout); initView(); } private void initView() { HideIMEUtil.wrap(this); register_get_verification = (TextView) findViewById(R.id.item_tv_add_isrono); time = new TimeCount(60000, 1000);//构造CountDownTimer对象 } /** * 验证码倒计时 */ class TimeCount extends CountDownTimer { public TimeCount(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval);//参数依次为总时长,和计时的时间间隔 } @Override public void onTick(long l) {//计时过程显示 register_get_verification.setClickable(false); register_get_verification.setText("剩余" + l / 1000 + "秒"); } @Override public void onFinish() {//计时完毕时触发 register_get_verification.setText("重新获取"); register_get_verification.setClickable(true); } } /** * 获取验证码请求 */ private void getVerifyMessage() { //验证码获取成功后 register_get_verification.setEnabled(false); } /** * 开始计时 */ private void startRockOn() { time.start();//开始计时 }