将 id 引用传递给 xml 文件上的 onClick 函数

发布于 2025-01-10 16:38:43 字数 1393 浏览 1 评论 0原文

这是 xml 代码:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@drawable/round_corner">

    <EditText
            android:id="@+id/name_resource"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/name"
            android:importantForAutofill="no"
            android:inputType="textPersonName"
            tools:ignore="TouchTargetSizeCheck" />

    <Button
            android:id="@+id/submit_create_resource_button"
            android:layout_width="160dp"
            android:layout_height="wrap_content"
            android:text="@string/create"
            android:textSize="17sp"
            android:layout_gravity="center_horizontal"
            android:onClick="@{() -> customDialogViewModel.onSetSharedResourceName()}"/>

</LinearLayout>

这是被调用的函数:

fun onSetSharedResourceName(name: String) {
        ....some code....
    }

我想将 EditText 内容作为 onSetSharedResourceName() 函数提供。 如何引用 name_resource?

感谢 :)

Here's the xml code:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@drawable/round_corner">

    <EditText
            android:id="@+id/name_resource"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/name"
            android:importantForAutofill="no"
            android:inputType="textPersonName"
            tools:ignore="TouchTargetSizeCheck" />

    <Button
            android:id="@+id/submit_create_resource_button"
            android:layout_width="160dp"
            android:layout_height="wrap_content"
            android:text="@string/create"
            android:textSize="17sp"
            android:layout_gravity="center_horizontal"
            android:onClick="@{() -> customDialogViewModel.onSetSharedResourceName()}"/>

</LinearLayout>

and here's the called function:

fun onSetSharedResourceName(name: String) {
        ....some code....
    }

I want to give the EditText content as onSetSharedResourceName() function.
How can I make a reference to name_resource?

Thank :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽容 2025-01-17 16:38:43

首先,您应该使用 Lateinit 声明类内的视图并添加 dataType

private lateinit var nameResource: EditText
private lateinit var submitCreateResourceButton: Button

,然后使用 findViewById 函数在 onCreate 内初始化并调用 id,

nameResource = findViewById(R.id.name_resource)
submitCreateResourceButton = findViewById(R.id.submit_create_resource_button)

然后您可以将其用作 onCreate 内的视图,例如:

nameResource.setOnClickListener {//write your code here}

first you should declare the view inside class with lateinit and add the dataType

private lateinit var nameResource: EditText
private lateinit var submitCreateResourceButton: Button

and then initialize inside onCreate with findViewById function and call the id

nameResource = findViewById(R.id.name_resource)
submitCreateResourceButton = findViewById(R.id.submit_create_resource_button)

then you can use it as view inside onCreate, for example:

nameResource.setOnClickListener {//write your code here}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文