compile 'io.reactivex.rxjava2:rxandroid:2.0.1' compile 'io.reactivex.rxjava2:rxjava:2.0.1' compile 'com.jakewharton.rxbinding2:rxbinding:2.0.0' compile 'com.jakewharton.rxbinding2:rxbinding-support-v4:2.0.0' compile 'com.jakewharton.rxbinding2:rxbinding-appcompat-v7:2.0.0'
Observable<Long> mObservableCountTime = RxView.clicks(mBtnSendMsm)
//防止重复点击
.throttleFirst(MAX_COUNT_TIME, TimeUnit.SECONDS)
//将点击事件转换成倒计时事件
.flatMap(new Function<Object, ObservableSource<Long>>() {
@Override
public ObservableSource<Long> apply(Object o) throws Exception {
//更新发送按钮的状态并初始化显现倒计时文字
RxView.enabled(mBtnSendMsm).accept(false);
RxTextView.text(mBtnSendMsm).accept("剩余 " + MAX_COUNT_TIME + " 秒");
//在实际操作中可以在此发送获取网络的请求
//返回 N 秒内的倒计时观察者对象。
return Observable.interval(1, TimeUnit.SECONDS, Schedulers.io()).take(MAX_COUNT_TIME);
}
})
//将递增数字替换成递减的倒计时数字
.map(new Function<Long, Long>() {
@Override
public Long apply(Long aLong) throws Exception {
return MAX_COUNT_TIME - (aLong + 1);
}
})
.observeOn(AndroidSchedulers.mainThread());//切换到 Android 的主线程。
Consumer<Long> mConsumerCountTime = new Consumer<Long>() {
@Override
public void accept(Long aLong) throws Exception {
//显示剩余时长。当倒计时为 0 时,还原 btn 按钮.
if (aLong == 0) {
RxView.enabled(mBtnSendMsm).accept(true);
RxTextView.text(mBtnSendMsm).accept("发送验证码");
} else {
RxTextView.text(mBtnSendMsm).accept("剩余 " + aLong + " 秒");
}
}
};
//订阅点击事件 Disposable mDisposable = mObservableCountTime.subscribe(mConsumerCountTime);
//重置验证码按钮。
RxView.clicks(mBtnClean).subscribe(new Consumer<Object>() {
@Override
public void accept(Object o) throws Exception {
if (mDisposable != null && !mDisposable.isDisposed()) {
//停止倒计时
mDisposable.dispose();
//重新订阅
mDisposable = mObservableCountTime.subscribe(mConsumerCountTime);
//按钮可点击
RxView.enabled(mBtnSendMsm).accept(true);
RxTextView.text(mBtnSendMsm).accept("发送验证码");
}
}
});
@Override
protected void onDestroy() {
super.onDestroy();
if (mDisposable != null) {
mDisposable.dispose();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有