1.创建一个CountDownTimerUtils工具类
//倒计时函数
private class CountDownTimerUtilsextends CountDownTimer {
private Button timeButton;
public CountDownTimerUtils(Button button,long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
this.timebutton = button;
}
//计时过程
@Override
public void onTick(long l) {
//防止计时过程中重复点击
timeButton.setClickable(false);
timeButton.setText(l/1000+"秒后重新发送");
}
//计时完毕的方法
@Override
public void onFinish() {
//重新给Button设置文字
timeButton.setText("重新获取");
//设置可点击
timeButton.setClickable(true);
}
}
}
Activity的按钮里添加
if(phone.isEmpty){
//new倒计时对象,总共的时间,每隔多少秒更新一次时间
CountDownTimerUtilsextends countDownTimerUtilsextends
= new CountDownTimerUtilsextends (btn_getCord,60000,1000);
countDownTimerUtilsextends .start();
}else{
Toast.makeText(this,"请输入手机号码",Toast.LENFTG_SHORT).show();
}