final int time = 10;
Observable.interval(0, 1, TimeUnit.SECONDS)//延迟时间是0,间隔时间是1,秒
.take(time + 1)//10秒
.map(new Function<Long, Long>() {
@Override
public Long apply(Long aLong) throws Exception {
//使用map操作符,从10开始倒数
return time - aLong;
}
})
.observeOn(AndroidSchedulers.mainThread())//在主线程中更新ui
.subscribe(new Observer<Long>() {
@Override
public void onSubscribe(Disposable d) {
//当开始倒计时的时候按钮不可以点击,并改变按钮的颜色和字体
button.setEnabled(false);
button.setTextColor(Color.parseColor("#be3131"));
button.setBackgroundColor(Color.parseColor("#f0f0f0"));
}
@Override
public void onNext(Long time) {
//实时的改变按钮的内容
button.setText("剩余" + time + "秒");
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
//倒计时结束的时候按钮不可以点击,并改变按钮的颜色和字体
button.setEnabled(true);
button.setText("获取验证码");
button.setTextColor(Color.parseColor("#000000"));
button.setBackgroundColor(Color.parseColor("#ffffff"));
}
});
使用Rxjava实现按钮的倒计时
猜你喜欢
转载自blog.csdn.net/a_sid/article/details/90299469
今日推荐
周排行