我试图熟悉RxBinding以及它如何应用于简单表单。我想知道是否有办法创建一个执行检查并更新模型的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);
我创建了一个Action1,它负责检查模型的状态并更改按钮的颜色,创建的订阅等于Observable的数量并将其作为参数传递。
像这样的东西,例如
Action1<CharSequence> lengthCheck = new Action1<CharSequence>() {
@Override
public void call(CharSequence charSequence) {
if(field is name){
if(charSequence.length() > 5)
...
}
...
}
};
所以我的问题是,有没有办法为Action1检查长度成功?甚至是这样检查的正确位置吗?对我来说,我有完全相同的代码,复制和粘贴三次,只需要很少的更改,这似乎很愚蠢。
非常感谢你的时间。
答案 0 :(得分: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:您可以创建一个为3 EditText
private Observable<Boolean> getNameObservable(EditText et) {
return RxTextView.textChanges(et)
.map(charSequence -> charSequence.toString().trim().length() > 0);
}