gpt4 book ai didi

android - 使用数据绑定(bind),如何将函数(或 lambda)变量传递给包含的布局

转载 作者:行者123 更新时间:2023-12-02 13:40:37 24 4
gpt4 key购买 nike

我将编辑器操作设置为edittext。
我的第一个代码是

@BindingAdapter("onEditorAction")
fun bindOnEditorAction(view: TextView, event: () -> Unit) {
view.setOnEditorActionListener { _, _, _ ->
event()
true
}
}
...
<EditText
...
app:onEditorAction="@{vm::searchAction}"
... />
这运作良好
但是当我使用包含标签时,我不知道如何通过 @{vm::searchAction}作为这样的变量:
Activity .xml:
<?xml version="1.0" encoding="utf-8"?>
<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="vm"
type="com.example.MyViewModel" />
</data>

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:fillViewport="true"
android:overScrollMode="never">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">


<include
android:id="@+id/input"
layout="@layout/view_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:action="@{vm::searchAction}"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:text="@={vm.input}" />

</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</layout>
view_input_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<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="text"
type="String" />

<variable
name="action"
type="???" /> // **What type for event??**

</data>

<com.google.android.material.card.MaterialCardView
android:id="@+id/input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<EditText
android:id="@+id/input_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:text="@{text}
app:editorAction=@{action} /> // Passed action will be setted here!

<TextView
android:id="@+id/timer_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|end"
android:layout_marginEnd="20dp"
tools:text="00:00" />

</com.google.android.material.card.MaterialCardView>

</layout>
我的 View 模型:
class MyViewModel(): ViewModel() {

val input = ObservableField<String>()

fun searchAction() {
Log.i("searchAction", "$input")
}
}
有没有办法解决这个问题?

最佳答案

这是一个老问题,但我为其他需要帮助的人回答。
试试这个代码。

<import type="kotlin.jvm.functions.Function0" />

<import type="kotlin.Unit" />

<variable
name="action"
type="Function0&lt;Unit>" />

...
android:onClick="@{() -> action.invoke()}"
...
如果你的函数有很多参数,你可以这样尝试。
<import type="kotlin.jvm.functions.Function2" />

<import type="kotlin.Unit" />

<variable
name="action"
type="Function2&lt;Integer, String, Unit>"/>

关于android - 使用数据绑定(bind),如何将函数(或 lambda)变量传递给包含的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63786530/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com