无法使用 RxKotlin 更改 ActionMenuItemView 的文本
Can't Change Text of ActionMenuItemView with RxKotlin
我正在尝试使用 Kotlin 编写 Android 应用程序。现在,我想在 ActionBar 中显示一个计数器。我为此添加了一个名为 show_timer 的项目。每一秒,它应该加一:
override fun onWindowFocusChanged(hasFocus: Boolean) {
val item = findViewById(R.id.show_timer) as ActionMenuItemView
PublishSubject.interval(1, java.util.concurrent.TimeUnit.SECONDS, Schedulers.newThread())
.subscribeBy(onNext = {item.text = it.toString()})
super.onWindowFocusChanged(hasFocus)
}
但不知何故这不起作用。它将默认文本更新为 0,但之后什么都不做。有人知道为什么这不起作用吗?
提前谢谢你,
尼克拉斯
为了更新文本,需要在主线程(不是 Schedulers.newThread()
线程)上更新
添加:
.observeOn(AndroidSchedulers.mainThread())
应该修复问题,并更新标签
我正在尝试使用 Kotlin 编写 Android 应用程序。现在,我想在 ActionBar 中显示一个计数器。我为此添加了一个名为 show_timer 的项目。每一秒,它应该加一:
override fun onWindowFocusChanged(hasFocus: Boolean) {
val item = findViewById(R.id.show_timer) as ActionMenuItemView
PublishSubject.interval(1, java.util.concurrent.TimeUnit.SECONDS, Schedulers.newThread())
.subscribeBy(onNext = {item.text = it.toString()})
super.onWindowFocusChanged(hasFocus)
}
但不知何故这不起作用。它将默认文本更新为 0,但之后什么都不做。有人知道为什么这不起作用吗?
提前谢谢你,
尼克拉斯
为了更新文本,需要在主线程(不是 Schedulers.newThread()
线程)上更新
添加:
.observeOn(AndroidSchedulers.mainThread())
应该修复问题,并更新标签