【Android -- RxJava】RxJava2.0 实践(六),采用 combineLatest 操作符实现输入表单验证

前言

在平时的应用中,我们经常会让用户输入一些信息,最常见的莫过于注册或者登录界面中,让用户输入用户名或者密码,但是我们经常会对用户名或者密码有一定的要求,只有当它们同时满足要求时,才允许用户进行下一步的操作。

这个需求就涉及到一种模型,即在多个地方监听变化,但是在一个地方进行统一验证,如果验证成功,那么允许用户进行下一步的操作,否则提示用户输入不正确。

通过这个例子,大家将学习到 combineLatest 操作符的用法。

效果图

在这里插入图片描述

具体实现

1. 添加依赖

	//Butter Knife
    implementation 'com.jakewharton:butterknife:10.2.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.0'

    //TitleBar
    implementation 'com.github.getActivity:TitleBar:8.6'

    //RxJava
    implementation 'io.reactivex.rxjava2:rxjava:2.2.10'
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'

2. 布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <com.hjq.bar.TitleBar
        android:id="@+id/title_bar"
        android:layout_width="match_parent"
        android:background="@color/teal_200"
        android:layout_height="?android:attr/actionBarSize"
        app:title="登录"
        app:titleStyle="bold"
        app:titleSize="18sp"
        app:backButton="false"
        app:titleColor="@color/white"/>

    <EditText
        android:id="@+id/et_userName"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/common_edt_bg"
        android:cursorVisible="true"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="14dp"
        android:layout_marginRight="14dp"
        android:hint="请输入2-8位用户名"
        android:inputType="text"
        android:padding="10dp"
        android:drawableLeft="@mipmap/ic_search"
        android:drawablePadding="8dp"
        android:singleLine="true"
        android:textColor="@color/black"
        android:textColorHint="#959595"
        android:textSize="16sp" />

    <EditText
        android:id="@+id/et_password"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/common_edt_bg"
        android:cursorVisible="true"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="14dp"
        android:layout_marginRight="14dp"
        android:hint="请输入4-16位密码"
        android:inputType="text"
        android:padding="10dp"
        android:drawableLeft="@mipmap/ic_search"
        android:drawablePadding="8dp"
        android:singleLine="true"
        android:textColor="@color/black"
        android:textColorHint="#959595"
        android:textSize="16sp" />

    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_button_bg"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="14dp"
        android:layout_marginRight="14dp"
        android:textSize="18sp"
        android:textColor="@color/white"
        android:text="登录" />


</LinearLayout>

3. 逻辑代码

public class MainActivity extends BaseActivity {
    private static final String TAG = "MainActivity";
    @BindView(R.id.et_userName)
    EditText mUserName;

    @BindView(R.id.et_password)
    EditText mPassword;

    @BindView(R.id.btn_login)
    Button mLogin;

    private PublishSubject<String> mNameSubject;
    private PublishSubject<String> mPasswordSubject;
    private CompositeDisposable mCompositeDisposable;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        mNameSubject = PublishSubject.create();
        mPasswordSubject = PublishSubject.create();

        mUserName.addTextChangedListener(new EditTextMonitor(mNameSubject));
        mPassword.addTextChangedListener(new EditTextMonitor(mPasswordSubject));
        Observable<Boolean> observable = Observable.combineLatest(mNameSubject, mPasswordSubject, new BiFunction<String, String, Boolean>() {

            @Override
            public Boolean apply(String name, String password) throws Exception {
                int nameLen = name.length();
                int passwordLen = password.length();
                return nameLen >= 2 && nameLen <= 8 && passwordLen >= 4 && passwordLen <= 16;
            }

        });
        DisposableObserver<Boolean> disposable = new DisposableObserver<Boolean>() {

            @Override
            public void onNext(Boolean value) {
                mLogin.setText(value ? "登录" : "用户名或密码无效");
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }

        };
        observable.subscribe(disposable);
        mCompositeDisposable = new CompositeDisposable();
        mCompositeDisposable.add(disposable);
    }


    private class EditTextMonitor implements TextWatcher {

        private PublishSubject<String> mPublishSubject;

        EditTextMonitor(PublishSubject<String> publishSubject) {
            mPublishSubject = publishSubject;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            mPublishSubject.onNext(s.toString());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mCompositeDisposable.clear();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kevin-Dev

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值