将 id 引用传递给 xml 文件上的 onClick 函数
这是 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您应该使用 Lateinit 声明类内的视图并添加 dataType
,然后使用 findViewById 函数在 onCreate 内初始化并调用 id,
然后您可以将其用作 onCreate 内的视图,例如:
first you should declare the view inside class with lateinit and add the dataType
and then initialize inside onCreate with findViewById function and call the id
then you can use it as view inside onCreate, for example: