RxBinding与RadioGroup和CheckBoxes

时间:2017-12-14 08:51:51

标签: android android-spinner rx-java2 radio-group

我已经在我的项目中用这样的butterknife实现了rxjava:

@BindView(R.id.login_et)
EditText mLoginField;

@BindView(R.id.password_et)
EditText mPassField;

@BindView(R.id.login_bt)
Button mLoginButton;

@BindDrawable(R.drawable.error)
Drawable mInvalidField;

@BindDrawable(R.drawable.check_green)
Drawable mValidField;

并验证我的输入字段和按钮,如下所示:

rx.Observable<CharSequence> loginObservable = RxTextView.textChanges(mLoginField);
    loginObservable.map(this::isValidLogin)
            .subscribe(isValid -> mLoginField.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, (isValid? mValidField : mInvalidField), null));

    rx.Observable<CharSequence> passwordObservable = RxTextView.textChanges(mPassField);
    passwordObservable.map(this::isValidPassword)
            .subscribe(isValid -> mPassField.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, (isValid ? mValidField : mInvalidField), null));

    rx.Observable<Boolean> combinedObservables = rx.Observable.combineLatest(loginObservable, passwordObservable, (o1, o2) -> isValidLogin(o1) && isValidPassword(o2));
    combinedObservables.subscribe(isColorChanged -> mLoginButton.setBackgroundColor(isColorChanged ? getResources().getColor(R.color.red): getResources().getColor(R.color.green)));

我的活动布局中有一个radiogroup和三个微调器,我很困惑如何在radiogroup和spinner上进行RxBindings以进行表单验证。

1 个答案:

答案 0 :(得分:1)

  

我很困惑如何在无线电组和微调器上进行RxBindings

这是您侦听CheckBox更改的方式:

 RxCompoundButton.checkedChanges(checkbox)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean isChecked) throws Exception {

                    }
                });

这是您如何收听Spinner选择的方法: RxBindings For Spinner?