调查Kotlin的Unit
类型,这是object
,基本上代表Java的void
。
在某些情况下,我想在PublishSubject<Unit>
数据绑定中使用onClick
基本上只是发出点击信号。 android xml看起来像这样:
<android.support.v7.widget.AppCompatButton
android:layout_width="148dp"
android:layout_height="60dp"
android:onClick="@{() -> viewModel.navigateSubject.onNext(Unit)}" />
在layout
我引入类型:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="viewModel"
type="com.example.TestViewModel"/>
<import type="kotlin.Unit"/>
</data>
[...]
然而,以这种方式调用对象类型当然是不可能的,有没有人找到在android xml中使用Unit
类型的方法?
答案 0 :(得分:1)
您应该能够使用Unit.INSTANCE
,就像在Java代码中一样。