RxJava/RxAndroid - handle multiple EditText changes

此生再无相见时 提交于 2019-12-05 03:31:10
Akash Sherpa

Try this, this will definately gonna work. use combineLatest.

//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);
    }

  }
});
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!