Android RxJava Combat series: joint judgment

Source: Internet
Author: User

Objective
    • Rxjava, because of its chain call based on event flow, Logic concise & easy to use characteristics, by the major Android developers welcome.

If you do not know Rxjava, see the article: Android: This is a clear & understandable Rxjava Getting Started tutorial


    • RxJavaThe reason for this popularity is that it provides a rich & powerful operator that can do almost all of the functional requirements
    • Today, I'm going to bring you a Rxjava common development scenario for creating operators: Co-judging needs , hopefully everyone will like it.

  1. This series of articles is based mainly on Rxjava 2.0
  2. The next time, I will continue to Android launch Rxjava 2.0 a series of articles, including principles, operators, application scenarios, back pressure , and so on, interested in continuing to focus on Carson_ho's Android development notes!!

Directory

1. Demand Scenarios

Multiple events need to be jointly judged at the same time

For example, when filling out a form, all information in the form (name, age, occupation, etc.) is required to be filled in to allow clicking on the "Submit" button

2. Function description
    • Fill out the form as a joint judgment function
    • That is, all information in the form (name, age, occupation, etc.) is filled in to allow clicking on the "Submit" button
3. Concrete implementation
    • Principle
      RxJavaimplementation using combined operators combineLatest()

      See the article on using the combined operator: combineLatest() : Android RxJava: Combination/merge operator detailed 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  =< Span class= "Hljs-value" > "wrap_content"  android:layout_height  = "wrap_content"  android:hint  =" Please fill in the name "/>     <edittext  android:id  = "@+id/age"  android:layout_width  =< Span class= "Hljs-value" > "wrap_content"  android:layout_height  = "wrap_content"  android:hint  =" Please fill in the age "/>     <edittext  android:id  = "@+id/job"  android:layout_width  =< Span class= "Hljs-value" > "wrap_content"  android:layout_height  = "wrap_content"  android:hint  =" Please fill in the occupation "/>     <buttonandroid:id="@+id/list"android:layout_width="wrap_content "android:layout_height="wrap_content "android:text=" Submit "Android: Enabled="false" />                                        </linearlayout>

Mainactivity.java

        /* * Step 1: Set control variables & bindings **/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 observer for each edittext to send the Listener event * Description: * 1. RxBinding:RxTextView.textChanges (name) is used here = Monitoring of control data changes (functionally similar to textwatcher) requires the introduction of dependencies: Compile ' com.jakewharton.rxbinding2:rxbinding:2.0.0 ' * 2. When you pass in the EditText control, when you click any of the 1 edittext compose, the return value of the data event = Function3 () is sent (detailed below) * 3. Use Skip (1) Reason: Skips the first edittext null value without any input **/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 judgments through Combinelatest () **/Observable. Combinelatest(Nameobservable,ageobservable,jobobservable,new function3<charsequence, Charsequence, CharSequence,Boolean> () {@Override public Boolean apply (@NonNull charsequence charsequence, @NonNull charsequence charse Quence2, @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 being set 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 Info Boolean isuserjobvalid =! Textutils. IsEmpty(Job. GetText());                /* * Step 5: return information = Joint judgment, that is, 3 information has been filled in, "submit button" can be clicked **/return isusernamevalid && isuseragevalid && isuserjobvalid;}                }). Subscribe(New consumer<boolean> () {@Override public void accept (Boolean s) throws Exception {/* * Step 6: Return results & Settings button to click on style **/Log. E(TAG,"submit button can be clicked:"+s);List. setenabled(s);}        });
    • Test results

4. Demo Address

Carson_ho's GitHub address = RXJAVA2 Combat series: joint judgment

5. Summary
    • This article mainly explains Rxjava the actual development requirement scenario: joint judgment
    • I will follow the actual scenario application & Rxjava related use framework (such as Retrofit , Eventbus ) , continue to Android the Rxjava actual development requirements of the scene in-depth explanation, interested can continue to focus on Carson_ Ho's Android Development notes

Please help the top/comment to praise! Because your encouragement is my biggest motive of writing!

Android RxJava Combat series: joint judgment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.