在android布局中使用kotlin的Unit(或任何其他对象)类型

时间:2017-09-01 10:10:07

标签: android kotlin rx-java2 android-databinding

调查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类型的方法?

1 个答案:

答案 0 :(得分:1)

您应该能够使用Unit.INSTANCE,就像在Java代码中一样。