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

Use kotlin's Unit (or any other object) type in android layout

研究 Kotlin 的 Unit 类型,这是一个 object 基本上代表 Java 的 void.

在某些情况下,我想在 onClick 数据绑定中使用 PublishSubject<Unit>,基本上只是发出点击信号。 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>
    [...]

不过这样调用object类型当然是不行的,有没有人找到在androidxml中使用Unit类型的方法?

您应该可以使用 Unit.INSTANCE,如 Java 代码。