我在我的项目中使用RxJava2,Kotlin-1.1以及RxBindings。
我有一个简单的登录界面,默认情况下'登录'按钮是禁用的,我希望只有当用户名和密码的编辑文本字段不为空时才启用该按钮。
LoginActivity.java
Observable<Boolean> isFormEnabled =
Observable.combineLatest(mUserNameObservable, mPasswordObservable,
(userName, password) -> userName.length() > 0 && password.length() > 0)
.distinctUntilChanged();
我无法将上述Java代码翻译成Kotlin:
LoginActivity.kt
class LoginActivity : AppCompatActivity() {
val disposable = CompositeDisposable()
private var userNameObservable: Observable<CharSequence>? = null
private var passwordObservable: Observable<CharSequence>? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
initialize()
}
fun initialize() {
userNameObservable = RxTextView.textChanges(username).skip(1)
.debounce(500, TimeUnit.MILLISECONDS)
passwordObservable = RxTextView.textChanges(password).skip(1)
.debounce(500, TimeUnit.MILLISECONDS)
}
private fun setSignInButtonEnableListener() {
val isSignInEnabled: Observable<Boolean> = Observable.combineLatest(userNameObservable,
passwordObservable,
{ u: CharSequence, p: CharSequence -> u.isNotEmpty() && p.isNotEmpty() })
}
}
我觉得这可能与combinelatest
的第三个参数的类型推断有关,但是通过阅读错误消息,我没有完全理解这个问题: ![Type Inference issue](https://istack.dev59.com/obrG5.webp)