如何检测 EditText 何时失去焦点

How to detect when EditText lossed focus

我尝试检测 editText 何时失去焦点。我使用 DataBinding,但我的代码不起作用。我使用自定义侦听器 EditTextFocusListener 因为我的 EditText 有另一个侦听适配器。 setOnFocusChangeListener

没有回调
  @JvmStatic
        @BindingAdapter("onFocusAction")
        fun onFocusAction(view: EditText, editTextFocusListener: EditTextFocusListener) {
            view.setOnFocusChangeListener { view, hasFocus ->
                if(!hasFocus){
                    editTextFocusListener.lossFocus("loss focus")
                } 
            }
        }



 <EditText
                android:id="@+id/input"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:resolveKeyboardInputType="@{model.inputTypeBind}"
                app:afterTextChanged="@{(text) -> model.inputBind.onTextChanged()}"
                app:requestKeyboard="@{model.inputBind.requestKeyboardBind}"
                app:requestKeyboardModel="@{model.inputBind}"
                app:onKeyboardDoneAction="@{() -> model.onDoneAction.invoke()}"
                app:onFocusAction="@{(loss) -> model.onFocusAction.invoke(loss)}"
                app:text="@={model.inputBind.textBind}"/>

您可以使用 OnFocusChangeListener 例如:

    override fun onFocusChange(view: View?, hasFocus: Boolean) {
        if (!hasFocus)
            //do something (lossed focus)
    }