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

标签 android kotlin android-databinding

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/

相关文章:

firebase - 上传文件之前生成 Firebase 存储下载 url

android - Flutter - 找不到 Base64 类

android - 使用 FileProvider 拍照后无法保存在图库中

function - 为什么有两个函数执行完全相同的操作?为什么必须在 Kotlin 中以不同的方式调用?

gradle - 从 ktor 服务 kotlin 多平台 javascript

android - 通用@BindingConversion 不起作用

android - 使用 Kotlin 进行数据绑定(bind)会导致 Resources$NotFoundException

android - 错误 :Data Binding does not support Jack builds yet

android - 我无法在默认 'Welcome to React Native!"APP 中使用辅助功能

java - 在Android中隐藏UI元素后如何最小化 ScrollView 的大小?