How to remove focus from SearchView?

前端 未结 10 1469
野趣味
野趣味 2021-02-06 22:20

I want to remove focus and text from SearchView in onResume().

I tried searchView.clearFocus() but it is not working.

Thi

相关标签:
10条回答
  • 2021-02-06 23:03

    For those who are looking to get rid of the focus when the Activity is started AND the SearchView is in the Menu, I did these 3 things, it might work with just a few:

    1.- Create a parent class of SearchView. Let's call it RafaSearchView:

    class RafaSearchView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0)
        : SearchView(context, attrs, defStyleAttr) {
    
        init {
            setIconifiedByDefault(false)
        }
    }
    

    2.- Get the xml where you have your Toolbar, and set touchable="false" and focusableInTouchMode="true":

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/searchToolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/white"
            android:focusable="false"
            android:focusableInTouchMode="true"
            android:theme="@style/ToolBarStyle"
            app:popupTheme="@style/AppTheme.PopupOverlay" />
    

    3.- On onCreateOptionsMenu(Menu) when I'm declaring my RafaSearchView, call:

        searchView?.isIconified = false
        searchView?.clearFocus()
    
    0 讨论(0)
  • 2021-02-06 23:04

    The simplest (and I think, the only 100% working) solution is, to create an invisible LinearLayout. This LinearLayout will grab the focus from the EditText.

    0 讨论(0)
  • 2021-02-06 23:08

    I had just request focus to some other viewgroup and it removed focus from th searchview. Like in the parent viewgroup eg(Linearlayout, RelativeLayout) in which searchview is present, add

    focusable = false; focusableInTouchMode=true
    

    to the parent viewgroup.

    0 讨论(0)
  • 2021-02-06 23:10

    Just searchView.clearFocus(); in onResume()helped my case

    0 讨论(0)
提交回复
热议问题