java rxJava结合(表单验证)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java rxJava结合(表单验证)相关的知识,希望对你有一定的参考价值。

Observable<CharSequence> observable2 = Observable.create(new Observable.OnSubscribe<CharSequence>() {
      @Override
      public void call(final Subscriber<? super CharSequence> subscriber) {
          txt2.addTextChangedListener(new TextWatcher() {
              @Override
              public void beforeTextChanged(CharSequence s, int start, int count, int after) {
              }

              @Override
              public void onTextChanged(CharSequence s, int start, int before, int count) {
                  subscriber.onNext(s);
              }

              @Override
              public void afterTextChanged(Editable s) {
              }
          });
      }
  });
  Observable<CharSequence> observable3 = Observable.create(new Observable.OnSubscribe<CharSequence>() {
      @Override
      public void call(final Subscriber<? super CharSequence> subscriber) {
          txt3.addTextChangedListener(new TextWatcher() {
              @Override
              public void beforeTextChanged(CharSequence s, int start, int count, int after) {
              }

              @Override
              public void onTextChanged(CharSequence s, int start, int before, int count) {
                  subscriber.onNext(s);
              }

              @Override
              public void afterTextChanged(Editable s) {
              }
          });
      }
  });
      

  Observable.combineLatest(observable2, observable3, new Func2<CharSequence, CharSequence, Boolean>() {
      @Override
      public Boolean call(CharSequence charSequence, CharSequence charSequence2) {
          boolean isGreater10 = Integer.parseInt(txt2.getText().toString()) > 10;
          if (!isGreater10) txt2.setError("must be greater 10");

          boolean isLengthGreater5 = txt3.getText().toString().length() > 5;
          if (!isLengthGreater5) txt3.setError("length must be greater than 5");

          return isGreater10 && isLengthGreater5;
      }
  }).subscribe(new Subscriber<Boolean>() {
      @Override
      public void onCompleted() {
          Log.d(TAG, "onCompleted: ");
      }

      @Override
      public void onError(Throwable e) {
          Log.d(TAG, "onError: ");
      }

      @Override
      public void onNext(Boolean aBoolean) {
          Log.d(TAG, "return: " + aBoolean);
      }
  });

以上是关于java rxJava结合(表单验证)的主要内容,如果未能解决你的问题,请参考以下文章

RxJava开发精要8 – 与REST无缝结合-RxJava和Retrofit

Rxjava源码分析&实践RxJava基本原理分析之订阅流

RxJava——响应式和区域化的优秀框架(java&android)

django实现密码加密的注册(数据对象插入)-结合forms表单实现表单验证

网络库与Rxjava2结合常见使用场景介绍

RxJava 与 Retrofit 结合的最佳实践