我試圖熟悉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些什麼呢?這是否是正確的地方這樣的檢查?對我來說,我看起來很愚蠢,我擁有完全相同的代碼,只需進行最少的更改即可複製和粘貼三次。
非常感謝您的時間。
當3個狀態= true時,您想要將提交按鈕的背景設置爲綠色嗎? –
Yeap,很簡單,只是想了解庫 –
使用combineLatest這種情況 –