Learning Android RxJava operators -- combining and merging operators -- combining and judging multiple events

Posted by coffeeguy on Wed, 11 Dec 2019 07:17:54 +0100

1. Demand scenario

Multiple events need to be judged jointly at the same time

For example, when filling in the form, all information (name, age, occupation, etc.) in the form must be filled in before clicking the "submit" button

2. Function description

  • Fill in the form as the joint judgment function display here
  • That is, only after all information (name, age, occupation, etc.) in the form has been filled in can you click the "submit" button

3. Specific implementation

  • principle
    Implementation of combineLatest() in RxJava

For the use of combineLatest() in composite operators, see the article: Android RxJava: Combination / merge operator tutorial

  • Specific code implementation

activity_main.xml

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

    <EditText
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Please fill in your name"
        />

    <EditText
        android:id="@+id/age"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Please fill in the age"
        />

    <EditText
        android:id="@+id/job"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Please fill in occupation"
        />

    <Button
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submission"
        android:enabled="false"
        />


</LinearLayout>

MainActivity.java

        /*
         * Step 1: set control variable & binding
         **/
        EditText name,age,job;
        Button list;

        name = (EditText) findViewById(R.id.name);
        age = (EditText) findViewById(R.id.age);
        job = (EditText) findViewById(R.id.job);
        list = (Button) findViewById(R.id.list);

        /*
         * Step 2: set the observed for each EditText to send listening events
         * Explain:
         * 1. Here, RxBinding: RxTextView.textChanges(name) = is used to monitor control data changes (function similar to TextWatcher). Dependency: compile 'com. Jakewarton. RxBinding2: RxBinding: 2.0.0' needs to be introduced
         * 2. Pass in the EditText control, and when clicking any EditText composition, the return value of data event = Function3() will be sent (to be explained in detail below)
         * 3. skip(1) reason: skip the null value when EditText has no input at the beginning
         **/
        Observable<CharSequence> nameObservable = RxTextView.textChanges(name).skip(1);
        Observable<CharSequence> ageObservable = RxTextView.textChanges(age).skip(1);
        Observable<CharSequence> jobObservable = RxTextView.textChanges(job).skip(1);

        /*
         * Step 3: merge events & Joint judgment through combineLatest()
         **/
        Observable.combineLatest(nameObservable,ageObservable,jobObservable,new Function3<CharSequence, CharSequence, CharSequence,Boolean>() {
            @Override
            public Boolean apply(@NonNull CharSequence charSequence, @NonNull CharSequence charSequence2, @NonNull CharSequence charSequence3) throws Exception {

                /*
                 * Step 4: specify that the form information input cannot be empty
                 **/
                // 1. Name information
                boolean isUserNameValid = !TextUtils.isEmpty(name.getText()) ;
                // In addition to setting to null, you can also set the length limit
                // boolean isUserNameValid = !TextUtils.isEmpty(name.getText()) && (name.getText().toString().length() > 2 && name.getText().toString().length() < 9);

                // 2. Age information
                boolean isUserAgeValid = !TextUtils.isEmpty(age.getText());
                // 3. Career information
                boolean isUserJobValid = !TextUtils.isEmpty(job.getText()) ;

                /*
                 * Step 5: return information = joint judgment, that is, three information have been filled in at the same time, and the "submit" button can be clicked
                 **/
                return isUserNameValid && isUserAgeValid && isUserJobValid;
            }

                }).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean s) throws Exception {
                /*
                 * Step 6: return to result & set button to click style
                 **/
                Log.e(TAG, "Can the submit button be clicked "+s);
                list.setEnabled(s);
            }
        });
  • test result

Topics: Mobile Android xml encoding Java