RxJava / RxAndroid - handle multiple EditText changes

I have 3 EditText fields and I created 3 observables for these fields.

Observable<CharSequence> o1 = RxTextView.textChanges(field1); Observable<CharSequence> o2 = RxTextView.textChanges(field2); Observable<CharSequence> o3 = RxTextView.textChanges(field3); 

I want to turn on the button when all three of these fields have some meaning. the user can enter values ​​in any order in the fields. How can i do this?

EDIT

I used zip to achieve this.

 Observable<CharSequence> o1 = RxTextView.textChanges(field1); Observable<CharSequence> o2 = RxTextView.textChanges(field2); Observable<CharSequence> o3 = RxTextView.textChanges(field3); Observable.zip(o1, o2, o3, (c1, c2, c3) -> c1.length() > 0 && c2.length() > 0 && c3.length() > 0).subscribe(myButton::setEnabled) 

This case above works when I enter something into all three text fields. for example, I entered 1 character in all three text fields, then the button will be turned on. But when I delete a character in any of the three fields. zip will not be called since it will wait until the other 2 text fields pass some data before it calls onNext on the subscriber. so when I delete any character in any text field, I want my button to turn off again. How can i achieve this?

+4
java android reactive-programming rx-java rx-android
source share
2 answers
+6
source share

Try it, it will definitely work. use combLatest.

 //declare global variable private Subscription subscription = null; Observable<CharSequence> o1 = RxTextView.textChanges(field1); Observable<CharSequence> o2 = RxTextView.textChanges(field2); public void combineEvent(){ subscription = Observable.combineLatest(o1, o2, new Func2<CharSequence, CharSequence, Boolean>() { @Override public Boolean call(CharSequence newEmail, CharSequence newPassword) { //here you can validate the edit text boolean emailValid= !TextUtils.isEmpty(newEmail) && android.util.Patterns.EMAIL_ADDRESS.matcher(newEmail).matches(); if(!emailValid){ etEmailAddress.setError("Invalid Email"); } boolean passValid = !TextUtils.isEmpty(newPassword) && newPassword.length() >6; if(!passValid){ etPassword.setError("invalid password"); } return emailValid && passValid; } }).subscribe(new Observer<Boolean>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Boolean aBoolean) { if(aBoolean){ //here you can enable your button or what ever you want. loginBtn.setEnabled(true); }else { loginBtn.setEnabled(false); } } }); } 
+2
source share

All Articles