Android 对多个EditText监听

@ Android 对多个EditText监听

RxJava的掌握

https://github.com/ReactiveX/RxJava/wiki/How-To-Use-RxJava
上面是官网连接,RXJava一直在升级,今天讲解以我项目中用的版本为准。

gradle版本

api ‘io.reactivex:rxjava:1.3.0’

项目代码

    telEt = contentView.findViewById(R.id.tel_et);
    Observable<String> telObservable = Observable.unsafeCreate(subscriber -> telEt.addTextChangedListener(new MyTextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            super.afterTextChanged(s);
            subscriber.onNext(s.toString());
        }
    }));
    imageCodeEt = contentView.findViewById(R.id.image_code_et);
    Observable<String> imageObservable = Observable.unsafeCreate(subscriber -> imageCodeEt.addTextChangedListener(new MyTextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            super.afterTextChanged(s);
            subscriber.onNext(s.toString());
        }
    }));
    imageCodeIv = contentView.findViewById(R.id.image_code_iv);
    imageCodeIv.setOnClickListener(this);
    vCodeEt = contentView.findViewById(R.id.v_code_et);
    Observable<String> vCodeObservable = Observable.unsafeCreate(subscriber -> vCodeEt.addTextChangedListener(new MyTextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            super.afterTextChanged(s);
            subscriber.onNext(s.toString());
        }
    }));
    Observable.combineLatest(telObservable, imageObservable, vCodeObservable, (s, s2, s3) -> !StringUtils.isEmpty(s) && !StringUtils.isEmpty(s2) && !StringUtils.isEmpty(s3)).subscribe(new Observer<Boolean>() {
        @Override
        public void onCompleted() {
        }

        @Override
        public void onError(Throwable e) {
        }
        @Override
        public void onNext(Boolean aBoolean) {
            if (aBoolean) {
                loginBtn.setClickable(true);
                loginBtn.setText(activity.getString(R.string.login_btn));
                loginBtn.setBackground(activity.getResources().getDrawable(R.drawable.common_green_bg));
            } else {
                loginBtn.setClickable(false);
                loginBtn.setBackground(activity.getResources().getDrawable(R.drawable.common_grey_bg));
            }
        }
    });

主要方法是使用Observable.combineLatest(),将多个Observable合并起来。

猜你喜欢

转载自blog.csdn.net/w517272812/article/details/88639310
0条评论
添加一条新回复