美文网首页RxJava
RxJava<第二十六篇>:联合判断

RxJava<第二十六篇>:联合判断

作者: NoBugException | 来源:发表于2019-04-01 17:24 被阅读1次
图片.png

如图所示,图片上有三个编辑框,当三个编辑框的内容满足条件时,登录按钮可被点击,否则不可被点击。

要求:

  • 手机号必须等于11位;
  • 密码必须大于等于6位;
  • 授权码不能为空;

同时满足以上条件即可让登录按钮使能。

如果使用RxJava来实现,代码可以更加优雅。

这里采用RxBindingcombineLatest相结合的方式实现。

    final EditText telphone = findViewById(R.id.telphone);
    final EditText password = findViewById(R.id.password);
    final EditText shouquan = findViewById(R.id.shouquan);
    final Button bt_login = findViewById(R.id.bt_login);

    //手机号文本监听被观察者
    Observable<CharSequence> observable1 = RxTextView.textChanges(telphone);
    //密码文本监听被观察者
    Observable<CharSequence> observable2 = RxTextView.textChanges(password);
    //授权文本监听被观察者
    Observable<CharSequence> observable3 = RxTextView.textChanges(shouquan);

    Observable.combineLatest(observable1, observable2, observable3, new Function3<CharSequence, CharSequence, CharSequence, Boolean>() {

        @Override
        public Boolean apply(CharSequence charSequence, CharSequence charSequence2, CharSequence charSequence3) throws Exception {
            boolean phone_boolean = (!TextUtils.isEmpty(telphone.getText()) && telphone.getText().length() == 11) ? true : false;
            boolean password_boolean = (!TextUtils.isEmpty(password.getText()) && password.getText().length() >= 6) ? true : false;
            boolean shouquan_boolean = !TextUtils.isEmpty(shouquan.getText()) ? true : false;
            return phone_boolean && password_boolean && shouquan_boolean;
        }
    }).subscribe(new Consumer<Boolean>() {
        @Override
        public void accept(Boolean aBoolean) throws Exception {
            if(aBoolean){
                bt_login.setEnabled(true);
            }else{
                bt_login.setEnabled(false);
            }
        }
    });

效果如下:

43.gif

相关文章

网友评论

    本文标题:RxJava<第二十六篇>:联合判断

    本文链接:https://www.haomeiwen.com/subject/dhxbbqtx.html