gpt4 book ai didi

android - 如何使用数据绑定(bind)从 xml 调用 Edittext onEditorAction

转载 作者:行者123 更新时间:2023-11-29 01:00:22 25 4
gpt4 key购买 nike

喜欢下面的代码

<data>
<variable
name="listener"
type="android.view.View.OnClickListener" />
</data>


<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android: text="@ {user.firstName}"
android:onClick="@{listener::onClick}"/>

我想从 XML 调用 editorAction,是否可以通过 xml 执行它?

最佳答案

1.创建界面

interface CustomOnEditorActionListener {
fun onEditorAction()
}

2.绑定(bind)适配器

@BindingAdapter("app:customOnEditorActionListener")
fun setCustomOnEditorActionListener(view: TextView, listener: CustomOnEditorActionListener?) {
if (listener == null) {
view.setOnEditorActionListener(null)
} else {
view.setOnEditorActionListener(object : TextView.OnEditorActionListener {
override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
listener.onEditorAction()
return false
}
})
}
}

3. 在 XML 中。 viewModel 实现接口(interface) CustomOnEditorActionListener

    ...
<variable
name="viewModel"
type="nl.login.LoginViewModel" />

...
<EditText
android:id="@+id/password_edit"
app:customOnEditorActionListener="@{viewModel}"
...

关于android - 如何使用数据绑定(bind)从 xml 调用 Edittext onEditorAction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51648366/

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