在切换页面或者从新打开页面的时候,按钮自动倒计时,直到倒计时结束
Util_SMS.getInstance().setSMSView(fragmentBinding.tvVerificationCode, (EditText) fragmentBinding.tvPhone, mobile -> {
//点击以后的逻辑实现
});
/**
-
记忆功能的倒计时
*/
public class Util_SMS{
protected TextView tv_sendCode;
protected EditText et_mobile;
private static int remainingTime = -1;
static Util_SMS_One util_sms = new Util_SMS_One();private Runnable runnable = new Runnable() {
public void run() { if (tv_sendCode != null) { tv_sendCode.setText(remainingTime > 0 ? new StringBuilder("剩余").append(remainingTime).append("s") : "获取验证码"); tv_sendCode.setEnabled(remainingTime > 0 ? false : true); } if (sendCodeAttributes != null) { sendCodeAttributes.setText(remainingTime > 0 ? new StringBuilder("剩余").append(remainingTime).append("s") : "获取验证码"); sendCodeAttributes.setEnabled(remainingTime > 0 ? false : true); } }
};
{
io.reactivex.Observable.interval(1, TimeUnit.SECONDS).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(aLong -> {
if (remainingTime >= 0)
remainingTime–;
runnable.run();
}, Throwable::printStackTrace);
}public static Util_SMS_One getInstance() {
return util_sms;
}
public static Util_SMS getInstance() {
return util_sms;
}
public void setSMSView(TextView tv_sendCode, final EditText et_mobile, final ISMSCallback.OnCustomSendSMSListener onCustomSendSMSListener) {
util_sms.tv_sendCode = tv_sendCode;
util_sms.et_mobile = et_mobile;
runnable.run();
tv_sendCode.setOnClickListener(v -> {
String mobile = et_mobile.getText().toString();
//判断输入的不是手机号
if (Util_Empty.isEmptyAndMatchToToast(mobile, Constants_International.error_mobile_not_null, RegexConstants.REGEX_MOBILE_SIMPLE, error_expression_mobile))
return;
if (!NetworkUtils.isConnected()) {
Util_Toast.toast(Constants_International.error_net_disconnect);
return;
}
if (remainingTime < 0) {
if (onCustomSendSMSListener != null)
onCustomSendSMSListener.customSendSMS(mobile);
remainingTime = 60;
}
});
}
//用来获取验证码的回调
public interface ISMSCallback {
interface OnCustomSendSMSListener {
void customSendSMS(String mobile);
}
interface OnMobCommitSMSListener {
void success();
void fail();
}
}
}