How to modify SearchView close/clear button to always close the searchbox no matter whether it has content?

℡╲_俬逩灬. 提交于 2021-01-01 13:49:27

问题


mSearchView.setOnCloseListener {
      // do something
}

This method won't be called if the searchview contains some text. It is only called when the searchbox is empty.

UPDATE:

menu xml:

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_scrolltotop"
        android:icon="@drawable/ic_baseline_arrow_upward_24_white"
        android:title="@string/action_scrolltotop"
        app:showAsAction="ifRoom" />

    <item
        android:id="@+id/action_search"
        android:icon="@android:drawable/ic_menu_search"
        android:title="@string/action_search"
        app:actionViewClass="android.widget.SearchView"
        app:showAsAction="always" />
</menu>

I use navigation component so I inflate the menu from a fragment.

onCreateOptionsMenu (from a fragment):

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
    super.onCreateOptionsMenu(menu, inflater)

    inflater.inflate(R.menu.menu_memories_tools, menu)

    val mSearch = menu.findItem(R.id.action_search)
    mSearchView = mSearch.actionView as SearchView

    mSearchView.setOnSearchClickListener {
        memoriesScrollView.smoothScrollTo(0, 0)
    }

    val closeBtn = mSearchView.findViewById<ImageView>(R.id.search_close_btn)
    closeBtn?.setOnClickListener {
        mSearchView.setQuery("", false)
        mSearchView.isIconified = true
    }

    mSearchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
        ...
    }
}

回答1:


Solution

val closeBtnId = mSearchView.context.resources
    .getIdentifier("android:id/search_close_btn", null, null)
val closeBtn = mSearchView.findViewById<ImageView>(closeBtnId)
closeBtn?.setOnClickListener {
    mSearchView.onActionViewCollapsed()
}



回答2:


You can do it by inflating the close button, and in its onClick callback:

  • Submit an empty text by searchView.setQuery

  • Replace the x icon with the search icon by searchView.setIconified(true)

Java:

View closeBtn = searchView.findViewById(androidx.appcompat.R.id.search_close_btn);
closeBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        searchView.setQuery("", false); // reset Query text to be empty without submition 
        searchView.setIconified(true); // Replace the x icon with the search icon
    }
});

Kotlin

val closeBtn: View = searchView.findViewById(androidx.appcompat.R.id.search_close_btn)
closeBtn.setOnClickListener {
    searchView.setQuery("", false) // reset Query text to be empty without submition 
    searchView.isIconified = true // Replace the x icon with the search icon
}



回答3:


You can modify the functionality of close/clear by using this:

Declare a variable ImageView in your class

private var closeButton: ImageView? = null

closeButton = mSearchView.findViewById(androidx.appcompat.R.id.search_close_btn)
        closeButton?.setOnClickListener {
          mSearchView.onActionViewCollapsed() 
        }

Make sure you have app:iconifiedByDefault="true" in your androidx.appcompat.widget.SearchView XML.



来源:https://stackoverflow.com/questions/65172875/how-to-modify-searchview-close-clear-button-to-always-close-the-searchbox-no-mat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!