【问题标题】:Use kotlin's Unit (or any other object) type in android layout在 android 布局中使用 kotlin 的 Unit(或任何其他对象)类型
【发布时间】:2017-09-01 10:10:07
【问题描述】:

查看 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>
    [...]

但是这样调用对象类型当然是不可能的,有没有人找到在android xml中使用Unit类型的方法?

【问题讨论】:

    标签: android kotlin rx-java2 android-databinding


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2012-05-18
      • 1970-01-01
      • 1970-01-01
      • 2020-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-02
      • 1970-01-01
      相关资源
      最近更新 更多