actionSearch Edittex 上的数据绑定
Data binding on actionSearch Edittex
kotlin中@BindingAdapter
软键盘点击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"/>
我的视图模型:
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}"/>
onEditoractionListener的数据绑定可以实现为
fun onEditorAction(view: TextView?, actionId: Int, event: KeyEvent?): Boolean {
if (actionId == EditorInfo.IME_ACTION_DONE) {
//todo action you want on this
return true
}
return false
}
并在布局文件中使用 as
android:onEditorAction="@{(view,actionId,event) -> viewmodel.onEditorAction(view,actionId,event)}"
kotlin中@BindingAdapter
软键盘点击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"/>
我的视图模型:
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}"/>
onEditoractionListener的数据绑定可以实现为
fun onEditorAction(view: TextView?, actionId: Int, event: KeyEvent?): Boolean {
if (actionId == EditorInfo.IME_ACTION_DONE) {
//todo action you want on this
return true
}
return false
}
并在布局文件中使用 as
android:onEditorAction="@{(view,actionId,event) -> viewmodel.onEditorAction(view,actionId,event)}"