Search code examples
androidbuttonandroid-edittextandroid-softkeyboard

how can full clear focus in editText , just like press button DONE on softkeyboard


I specify different backgrounds when edit text focus changed , but clearFocus() seem like no effect , because the background do not change to the right state .


Solution

  • Firstly, we can use cleareFocus method to remove the focus.

    editTextView.clearFocus()
    

    But when this method is called, as source code comment says:

    When not in touch-mode, the framework will try to give focus to the first focusable View from the top after focus is cleared. Hence, if this View is the first from the top that can take focus, then all callbacks related to clearing focus will be invoked after which the framework will give focus to this view.

    so after you call it, the first element will stay get the focus. so we should set the touch-mode true in parent layout.

    <LinearLayout
        android:focusable="true" 
        android:focusableInTouchMode="true" 
        android:layout_width="0px" 
        android:layout_height="0px"/>
    <EditText android:id="@+id/et"
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        />