定义一个类,调用即可,直接看代码。很简单
package com.apicloud.A6970406947389.utils; import android.os.CountDownTimer; import android.widget.Button; import com.apicloud.A6970406947389.R; /** * 时间倒计时 */ public class TimerCount extends CountDownTimer { private Button bnt; public TimerCount(long millisInFuture, long countDownInterval, Button bnt) { super(millisInFuture, countDownInterval); this.bnt = bnt; } public TimerCount(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); // TODO Auto-generated constructor stub } @Override public void onFinish() { // TODO Auto-generated method stub bnt.setClickable(true); bnt.setBackgroundResource(R.drawable.bt_bg);//设置为正常色 bnt.setText("获取验证码"); } @Override public void onTick(long arg0) { // TODO Auto-generated method stub bnt.setClickable(false); bnt.setBackgroundResource(R.drawable.shape_forgetpwd_bg);//设置为灰色 bnt.setText(arg0 / 1000 + "秒重新获取"); // bnt.setText("还剩"+arg0 / 1000 + "秒"); } }
在需要的地方直接调用
private TimerCount timer;
timer = new TimerCount(60000,1000,bnt); getVerifyCode();//执行发送验证码 timer.start();//启动