当代码和名称不为空时,我需要enable/disable
按钮。
我的代码:
btnAddItem.isEnabled = false
val codeIsValid = RxTextView.textChanges(txvCode)
.debounce(350, TimeUnit.MILLISECONDS)
.map { code ->
code.isNotEmpty()
}
val nameIsValid = RxTextView.textChanges(edtName)
.debounce(350, TimeUnit.MILLISECONDS)
.map { name ->
name.isNotEmpty()
}
disposableEnableButtonSave = Observables.combineLatest(codeIsValid, nameIsValid)
{ b1, b2 -> b1 && b2 }
.subscribe {
if (btnAddItem.isEnabled != it){
btnAddItem.isEnabled = it //crash here.
}
}
但是运行时出错:
Logcat:
io.reactivex.exceptions.OnErrorNotImplementedException:动画师只能在Looper线程上运行
android.util.AndroidRuntimeException:动画师只能在Looper
个线程上运行
代码崩溃是enable/disable
按钮。
btnAddItem.isEnabled = it
答案 0 :(得分:0)
尝试
runOnUiThread(new Runnable() {
@Override
public void run() {
if (btnAddItem.isEnabled != it){
btnAddItem.isEnabled = it
}
}
});