gpt4 book ai didi

android - actionSearch Edittex 上的数据绑定(bind)

转载 作者:行者123 更新时间:2023-11-29 18:31:16 25 4
gpt4 key购买 nike

kotlin中如何通过@BindingAdapter绑定(bind)点击软键盘actionSearch按钮时的数据?

我的编辑文本:

<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:padding="10dp"
android:text="@{viewModel.text}"
android:hint="@string/edit_text_hint"
android:imeOptions="actionSearch"/>

我的 View 模型:

class RelationListViewModel: BaseViewModel(){
//..
val text: MutableLiveData<String> = MutableLiveData()

最佳答案

setOnEditorActionListener添加一个BindingAdapter


class ViewModel {
private val editorActionListener: TextView.OnEditorActionListener

init {
this.editorActionListener = TextView.OnEditorActionListener { v, actionId, event ->
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
// do your search logic
true
} else false
}
}

companion object {

@BindingAdapter("onEditorActionListener")
fun bindOnEditorActionListener(editText: EditText, editorActionListener: TextView.OnEditorActionListener) {
editText.setOnEditorActionListener(editorActionListener)
}
}
}

并在您的 xml 中使用它

<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:padding="10dp"
android:text="@{viewModel.text}"
android:hint="@string/edit_text_hint"
android:imeOptions="actionSearch"
app:onEditorActionListener="@{viewModel.editorActionListener}"/>

关于android - actionSearch Edittex 上的数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56176758/

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