如何修改SearchView close/clear 按钮无论是否有内容总是关闭搜索框?
How to modify SearchView close/clear button to always close the searchbox no matter whether it has content?
mSearchView.setOnCloseListener {
// do something
}
如果搜索视图包含一些文本,则不会调用此方法。仅在搜索框为空时调用。
更新:
菜单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>
我使用导航组件,所以我从片段中扩充菜单。
onCreateOptionsMenu(来自片段):
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 {
...
}
}
您可以使用以下方法修改 close/clear 的功能:
在您的 class
中声明一个变量 ImageView
private var closeButton: ImageView? = null
closeButton = mSearchView.findViewById(androidx.appcompat.R.id.search_close_btn)
closeButton?.setOnClickListener {
mSearchView.onActionViewCollapsed()
}
确保您的 androidx.appcompat.widget.SearchView XML.
中包含 app:iconifiedByDefault="true"
解决方案
val closeBtnId = mSearchView.context.resources
.getIdentifier("android:id/search_close_btn", null, null)
val closeBtn = mSearchView.findViewById<ImageView>(closeBtnId)
closeBtn?.setOnClickListener {
mSearchView.onActionViewCollapsed()
}
您可以通过给关闭按钮充气,并在其 onClick
回调中执行此操作:
通过 searchView.setQuery
提交空白文本
将 x 图标替换为 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
}
});
科特林
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
}
mSearchView.setOnCloseListener {
// do something
}
如果搜索视图包含一些文本,则不会调用此方法。仅在搜索框为空时调用。
更新:
菜单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>
我使用导航组件,所以我从片段中扩充菜单。
onCreateOptionsMenu(来自片段):
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 {
...
}
}
您可以使用以下方法修改 close/clear 的功能:
在您的 class
中声明一个变量 ImageViewprivate var closeButton: ImageView? = null
closeButton = mSearchView.findViewById(androidx.appcompat.R.id.search_close_btn)
closeButton?.setOnClickListener {
mSearchView.onActionViewCollapsed()
}
确保您的 androidx.appcompat.widget.SearchView XML.
中包含app:iconifiedByDefault="true"
解决方案
val closeBtnId = mSearchView.context.resources
.getIdentifier("android:id/search_close_btn", null, null)
val closeBtn = mSearchView.findViewById<ImageView>(closeBtnId)
closeBtn?.setOnClickListener {
mSearchView.onActionViewCollapsed()
}
您可以通过给关闭按钮充气,并在其 onClick
回调中执行此操作:
通过
提交空白文本searchView.setQuery
将 x 图标替换为
中的搜索图标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
}
});
科特林
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
}