Android 用 RxBinding 与 RxJava2 实现短信倒计时功能

场景:注册账号页面时,我们点击按钮发送验证码,在等待验证码时,界面会有倒计时提示,这此期间按钮不可点击。当倒计时结束时,按钮恢复。
实现与功能都不难,这次用 RxBinding,RxJava2 的方法去实现。并实现了手动、自动停止倒计时,防止多次点击。

《Android 用 RxBinding 与 RxJava2 实现短信倒计时功能》 功能动态图

要使用 RxBinding、RxJava2 先添加 Gradle 配置:

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'

首先通过 RxView.clicks() 绑定并转换成一个倒计时的 Observable 观察者对象。

   mObservableCountTime = RxView.clicks(mBtnSendMsm)
                //防止重复点击
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(AndroidSchedulers.mainThread())
                //判断手机号否为空
                .flatMap(new Function<Object, ObservableSource<Boolean>>() {
                    @Override
                    public ObservableSource<Boolean> apply(Object o) throws Exception {
                        String phone = mEtPhone.getText().toString();
                        if (TextUtils.isEmpty(phone)) {
                            Toast.makeText(mContext, "phone can not be empty", Toast.LENGTH_SHORT).show();
                            return Observable.empty();
                        }
                        return Observable.just(true);
                    }
                })
                //将点击事件转换成倒计时事件
                .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 + " 秒");

                        //在实际操作中可以在此发送获取网络的请求,,续1s

                        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);
                                    }
                                });
                    }
                })
                //切换到 Android 的主线程。
                .observeOn(AndroidSchedulers.mainThread());Phone verification

设置作为倒计时提示的 Consumer 被观察者对象。

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();
    }
}

源码:

  1. SmsRxbindingActivity
  2. 倒计时的各种花式实现
    原文作者:DoubleThunder
    原文地址: https://www.jianshu.com/p/29d4a705c447
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞