RxJava 操作符处理表单验证 或者登录问题

论坛 期权论坛 脚本     
匿名技术用户   2021-1-5 23:16   56   0

Observable.combineLatest 方法使用

Observable.combineLatest<Observable1,Observable2,BiFunction<String,String,Bollean>>
    @SuppressWarnings("unchecked")
    @CheckReturnValue
    @SchedulerSupport(SchedulerSupport.NONE)
    public static <T1, T2, R> Observable<R> combineLatest(
            ObservableSource<? extends T1> source1, ObservableSource<? extends T2> source2,
            BiFunction<? super T1, ? super T2, ? extends R> combiner) {
        ObjectHelper.requireNonNull(source1, "source1 is null");
        ObjectHelper.requireNonNull(source2, "source2 is null");
        return combineLatest(Functions.toFunction(combiner), bufferSize(), source1, source2);
    }
例子方法1
Observable.combineLatest(RxTextView.textChanges(mRectEtMobile.mEditText),
        RxTextView.textChanges(mRectEtPwd.mEditText),
        BiFunction<CharSequence, CharSequence, Boolean> { mobile, password ->
            TextCheckUtil.checkMobilePhone(mobile.toString()) && password.length >= 6 && password.length <= 18
        })
        .compose(bindUntilEvent(ActivityEvent.DESTROY))
        .subscribe { aBoolean -> mBtnLogin.isEnabled = aBoolean }
例子方法2
Observable.combineLatest<String, String, Boolean>(
        RxTextView.textChanges(mRectEtMobile.mEditText).map { mobile -> mobile.toString() }
        , RxTextView.textChanges(mRectEtPwd.mEditText).map { password -> password.toString() }
        , BiFunction<String, String, Boolean> { mobile, password ->
    TextCheckUtil.checkMobilePhone(mobile) && password.length >= 6 && password.length <= 18
})
        .compose(bindUntilEvent(ActivityEvent.DESTROY))
        .subscribe { aBoolean -> mBtnLogin.isEnabled = aBoolean }
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP