我在我的项目中使用 RxJava2、Kotlin-1.1 和 RxBindings。
我有一个简单的登录屏幕,默认情况下禁用“登录”按钮,我只想在用户名和密码编辑文本字段不为空时启用该按钮。
登录活动.java
Observable<Boolean> isFormEnabled =
Observable.combineLatest(mUserNameObservable, mPasswordObservable,
(userName, password) -> userName.length() > 0 && password.length() > 0)
.distinctUntilChanged();
我无法将上述代码从 Java 转换为 Kotlin:
登录活动.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() })
}
}