Home > other >  How to detect when EditText lossed focus
How to detect when EditText lossed focus

Time:04-05

I try to detect when editText loss focus. I use DataBinding and my code doesn't work. I use custom listener EditTextFocusListener because my EditText has another listen adapter. There is no callback from 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}"/>

CodePudding user response:

You can use OnFocusChangeListener For example:

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

  • Related