How to remove focus from SearchView?

前端 未结 10 1479
野趣味
野趣味 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":

        
    

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

        searchView?.isIconified = false
        searchView?.clearFocus()
    

提交回复
热议问题