RxJava / RxAndroid - 处理多个EditText更改 [英] RxJava/RxAndroid - handle multiple EditText changes

查看:1398
本文介绍了RxJava / RxAndroid - 处理多个EditText更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个EditText字段,我为这些字段创建了3个observable。

I have 3 EditText fields and I have created 3 observables for these fields.

    Observable<CharSequence> o1 = RxTextView.textChanges(field1);
    Observable<CharSequence> o2 = RxTextView.textChanges(field2);
    Observable<CharSequence> o3 = RxTextView.textChanges(field3);

当所有这三个字段都有一些值时,我想启用一个按钮。用户可以在字段中以任何顺序输入值。我该怎么做?

I want to enable a button when all these three fields has some value in it. user can enter values in any order in the fields. How can i do that?

我用zip实现了这个目标。

I used zip to achieve that.

Observable<CharSequence> o1 = RxTextView.textChanges(field1);
Observable<CharSequence> o2 = RxTextView.textChanges(field2);
Observable<CharSequence> o3 = RxTextView.textChanges(field3);
Observable.zip(o1, o2, o3, (c1, c2, c3) -> c1.length() > 0 && c2.length() > 0 && c3.length() > 0).subscribe(myButton::setEnabled)

以上情况适用于我在所有三个文本字段中输入内容。例如我在所有三个文本字段中输入了1个字符,然后按钮将被启用。但是当我删除三个字段中的任何一个字符时。 zip不会被调用,因为它会在订阅者调用onNext之前等待其他2个文本字段流式传输某些数据。所以当我删除任何文本字段中的任何字符时,我希望我的按钮再次被禁用。我怎样才能做到这一点?

this above case works when i enter something in all three text fields. e.g. i entered 1 character in all three text fields then the button will be enabled. But when I delete a character in any of the three fields. zip will not get called as it will be waiting for the other 2 textfields to stream some data before it calls onNext on the subscriber. so when I delete any character in any textfield I want my button to get disabled again. How can I achieve that?

推荐答案

使用 CombineLatest 代替 zip http://reactivex.io/documentation/operators /combinelatest.html

这篇关于RxJava / RxAndroid - 处理多个EditText更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆