2017-07-25 77 views
1

我試圖熟悉RxBinding以及它如何應用於簡單的表單。我想知道是否有辦法創建一個執行檢查和更新模型的Action1。如果我不夠清楚,我希望下面的代碼能幫助你理解我的意思。創建一個統一的Action1來對字段執行檢查

Observable<CharSequence> nameObservable = RxTextView.textChanges(name).share(); 
    Action1<CharSequence> validateAction; 
    Observable<CharSequence> surnameObservable = RxTextView.textChanges(surname).share(); 
    Observable<CharSequence> emailObservable = RxTextView.textChanges(mail).share(); 

    validateAction = new Action1<CharSequence>() { 
     @Override 
     public void call(CharSequence charSequence) { 
      if(state.nameState && state.surnameState && state.emailState) 
       submit.setBackgroundColor(getResources().getColor(R.color.green)); 
      else 
       submit.setBackgroundColor(getResources().getColor(R.color.blue)); 
     } 
    }; 



    Subscription nameState = nameObservable 
      .subscribe(new Action1<CharSequence>() { 
       @Override 
       public void call(CharSequence charSequence) { 
        Log.d("Length","Len : " + charSequence.length()); 
        if(charSequence.length() > 5) 
         state.nameState = true; 
        else 
         state.nameState = false; 
       } 
      }); 


    Subscription surnameState = surnameObservable 
      .subscribe(new Action1<CharSequence>() { 
       @Override 
       public void call(CharSequence charSequence) { 
        Log.d("Length","Len : " + charSequence.length()); 
        if(charSequence.length() > 5) 
         state.surnameState = true; 
        else 
         state.surnameState = false; 
       } 
      }); 

    Subscription mailState = emailObservable 
      .subscribe(new Action1<CharSequence>() { 
       @Override 
       public void call(CharSequence charSequence) { 
        Log.d("Length","Len : " + charSequence.length()); 
        if(charSequence.length() > 5) 
         state.emailState = true; 
        else 
         state.emailState = false; 
       } 
      }); 

    Subscription nameValidate = nameObservable.subscribe(validateAction); 
    Subscription surnameValidate = surnameObservable.subscribe(validateAction); 
    Subscription mailValidate = emailObservable.subscribe(validateAction); 

我已經創建了一個措施1,其職責是檢查模型的狀態並更改按鈕的顏色,創建的訂閱平等的可觀的數量,並通過它作爲一個參數。
像這樣的事情,例如

Action1<CharSequence> lengthCheck = new Action1<CharSequence>() { 
     @Override 
     public void call(CharSequence charSequence) { 
      if(field is name){ 
       if(charSequence.length() > 5) 
        ...      
      } 
       ... 
     } 
    }; 

所以我的問題是,有沒有辦法取得成功與此類似對於檢查長度措施1些什麼呢?這是否是正確的地方這樣的檢查?對我來說,我看起來很愚蠢,我擁有完全相同的代碼,只需進行最少的更改即可複製和粘貼三次。
非常感謝您的時間。

+0

當3個狀態= true時,您想要將提交按鈕的背景設置爲綠色嗎? –

+0

Yeap,很簡單,只是想了解庫 –

+0

使用combineLatest這種情況 –

回答

2

對於名稱驗證

private Observable<Boolean> getNameObservable() { 
    return RxTextView.textChanges(name) 
      .map(charSequence -> charSequence.toString().trim().length() > 0); 
} 

對於姓驗證

private Observable<Boolean> getSurNameObservable() { 
    return RxTextView.textChanges(email) 
      .map(charSequence -> charSequence.toString().trim().length() > 0); 
} 

對於電子郵件驗證

private Observable<Boolean> getEmailObservable() { 
    return RxTextView.textChanges(email) 
      .map(charSequence -> charSequence.toString().trim().length() > 0); 
} 

使用combineLatest〜3個觀測相結合爲1流

Observable.combineLatest(getNameObservable(),getSurNameObservable(), getEmailObservable, 
        (nameValid, surnameValid,emailValid) -> nameValid && surnameValid && emailValid) 
        .subscribeOn(Schedulers.io()) 
        .observeOn(AndroidSchedulers.mainThread()) 
        .subscribe(isAllFieldsValid -> 
          enableButton(yourButton, isAllFieldsValid)); 

啓用功能

private void enableButton(Button button, boolean isAvailable) { 
    button.setEnabled(isAvailable); 
    if (isAvailable) { 
     button.setBackgroundResource(R.drawable.green); 
    } else { 
     button.setBackgroundResource(R.drawable.blue); 
    } 
} 

==================================== ===================================== PS:你可以創建一個函數來創建一個Observable for 3 EditText

private Observable<Boolean> getNameObservable(EditText et) { 
    return RxTextView.textChanges(et) 
      .map(charSequence -> charSequence.toString().trim().length() > 0); 
} 
+0

正是我在找什麼,非常感謝 –

+0

呀。希望對你有所幫助 –

+0

順便說一句'.subscribeOn()'給了我一個'java.lang.IllegalStateException',當我移除它時,一切都恢復正常 –

相關問題