Android RxJava 實戰系列:聯合判斷

來源:互聯網
上載者:User

標籤:pre   encoding   監聽事件   new   change   事件   list   操作   har   

前言
  • Rxjava,由於其基於事件流的鏈式調用、邏輯簡潔 & 使用簡單的特點,深受各大 Android開發人員的歡迎。

如果還不瞭解RxJava,請看文章:Android:這是一篇 清晰 & 易懂的Rxjava 入門教程


  • RxJava如此受歡迎的原因,在於其提供了豐富 & 功能強大的操作符,幾乎能完成所有的功能需求
  • 今天,我將為大家帶來 Rxjava建立操作符的常見開發應用情境:聯合判斷需求 ,希望大家會喜歡。

  1. 本系列文章主要基於 Rxjava 2.0
  2. 接下來的時間,我將持續推出 AndroidRxjava 2.0 的一系列文章,包括原理、操作符、應用情境、背壓等等 ,有興趣可以繼續關注Carson_Ho的安卓開發筆記!!

目錄

1. 需求情境

需要同時對多個事件進行聯合判斷

如,填寫表單時,需要表單裡所有資訊(姓名、年齡、職業等)都被填寫後,才允許點擊 “提交” 按鈕

2. 功能說明
  • 此處採用 填寫表單 作為聯合判斷功能展示
  • 即,表單裡所有資訊(姓名、年齡、職業等)都被填寫後,才允許點擊 “提交” 按鈕
3. 具體實現
  • 原理
    採用 RxJava 組合操作符中的combineLatest() 實現

    關於組合操作符中的combineLatest() 的使用請看文章::Android RxJava:組合 / 合併作業符 詳細教程

  • 具體代碼實現

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="請填寫姓名"        />    <EditText        android:id="@+id/age"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:hint="請填寫年齡"        />    <EditText        android:id="@+id/job"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:hint="請填寫職業"        />    <Button        android:id="@+id/list"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="提交"        android:enabled="false"        /></LinearLayout>

MainActivity.java

        /*         * 步驟1:設定控制項變數 & 綁定         **/        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);        /*         * 步驟2:為每個EditText設定被觀察者,用於發送監聽事件         * 說明:         * 1. 此處採用了RxBinding:RxTextView.textChanges(name) = 對對控制項資料變更進行監聽(功能類似TextWatcher),需要引入依賴:compile ‘com.jakewharton.rxbinding2:rxbinding:2.0.0‘         * 2. 傳入EditText控制項,點擊任1個EditText撰寫時,都會發送資料事件 = Function3()的傳回值(下面會詳細說明)         * 3. 採用skip(1)原因:跳過 一開始EditText無任何輸入時的空值         **/        Observable<CharSequence> nameObservable = RxTextView.textChanges(name).skip(1);        Observable<CharSequence> ageObservable = RxTextView.textChanges(age).skip(1);        Observable<CharSequence> jobObservable = RxTextView.textChanges(job).skip(1);        /*         * 步驟3:通過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 {                /*                 * 步驟4:規定表單資訊輸入不可為空                 **/                // 1. 姓名資訊                boolean isUserNameValid = !TextUtils.isEmpty(name.getText()) ;                // 除了設定為空白,也可設定長度限制                // boolean isUserNameValid = !TextUtils.isEmpty(name.getText()) && (name.getText().toString().length() > 2 && name.getText().toString().length() < 9);                // 2. 年齡資訊                boolean isUserAgeValid = !TextUtils.isEmpty(age.getText());                // 3. 職業資訊                boolean isUserJobValid = !TextUtils.isEmpty(job.getText()) ;                /*                 * 步驟5:返回資訊 = 聯合判斷,即3個資訊同時已填寫,"提交按鈕"才可點擊                 **/                return isUserNameValid && isUserAgeValid && isUserJobValid;            }                }).subscribe(new Consumer<Boolean>() {            @Override            public void accept(Boolean s) throws Exception {                /*                 * 步驟6:返回結果 & 設定按鈕可點擊樣式                 **/                Log.e(TAG, "提交按鈕是否可點擊: "+s);                list.setEnabled(s);            }        });
  • 測試結果

4. Demo地址

Carson_Ho的Github地址 = RxJava2實戰系列:聯合判斷

5. 總結
  • 本文主要講解了 Rxjava的實際開發需求情境:聯合判斷
  • 下面我將結合 實際情境應用 & Rxjava的相關使用架構(如RetrofitEventbus ,繼續對 AndroidRxjava 的實際開發需求情境進行深入講解 ,有興趣可以繼續關注Carson_Ho的安卓開發筆記

請幫頂 / 評論點贊!因為你的鼓勵是我寫作的最大動力!

Android RxJava 實戰系列:聯合判斷

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.