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/