研究 Kotlin 的 Unit
类型,这是一个 object
基本上代表Java的void
.
在某些情况下,我想使用 PublishSubject<Unit>
在onClick
数据绑定(bind)基本上只是发出点击信号。 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>
[...]
但是用这种方式调用对象类型当然是不可能的,有没有人找到使用Unit
的方法?输入 android xml?
最佳答案
您应该能够像在 Java 代码中一样使用 Unit.INSTANCE
。
关于android - 在 android 布局中使用 kotlin 的 Unit(或任何其他对象)类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45997922/