Android自定义Timer实现发送手机验证码倒计时

tech2024-01-10  70

Android自定义Timer实现发送手机验证码倒计时,继承自CountDownTimer来实现该功能。

public class TimeCount extends CountDownTimer { private TextView textView; //参数依次为总时长和计时的时间间隔 //写死倒计时结束是60s,即:millisInFuture = 60000 //间隔为1s,即:countDownInterval = 1000 public TimeCount(TextView textView) { super(60_000L, 1_000L); this.textView = textView; } //计时过程显示 @Override public void onTick(long millisUntilFinished) { String time = "(" + millisUntilFinished / 1_000L + "s)"; setButtonInfo(time, false); } //计时完毕时触发 @Override public void onFinish() { setButtonInfo("获取验证码", true); } /** * 验证按钮在点击前后相关设置 * * @param content 要显示的内容 * @param isClick 是否可点击 */ private void setButtonInfo(String content, boolean isClick) { if (textView != null) { textView.setText(content); textView.setClickable(isClick); } } }

使用 timeCount.start();来调用开始倒计时

最新回复(0)