Android 如何在 MainActivity 的 ActionBar 中关闭/关闭/折叠 SearchView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23928253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to dismiss/close/collapse SearchView in ActionBar in MainActivity?
提问by Kamil
I'm trying to clear and close SearchView after entering a value. I found a solution but it closes the search and won't perform any action if I try to search again.
我试图在输入一个值后清除并关闭 SearchView。我找到了一个解决方案,但它会关闭搜索,如果我再次尝试搜索,则不会执行任何操作。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main_actions, menu);
searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setOnQueryTextListener(searchListener);
return super.onCreateOptionsMenu(menu);
}
SearchView.OnQueryTextListener searchListener = new SearchView.OnQueryTextListener(){
@Override
public boolean onQueryTextChange(String arg0) {
return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
new JsoupGetData("http://api.openweathermap.org/data/2.5/find?q="+ query + "&lang=pl").execute();
try {
searchView.onActionViewCollapsed();
}
catch(Exception ex){
ex.printStackTrace();
System.out.println(ex);
}
return true;
}
};
I can search only for the first time. Every next time it only closes my input keyboard and does nothing. How can it be performed in a right way?
我只能搜索第一次。下次它只关闭我的输入键盘,什么也不做。如何以正确的方式执行?
Edit. Suggested change looks like this:
编辑。建议的更改如下所示:
try {
searchView.setIconified(true);
}
activity_main_actions.xml:
activity_main_actions.xml:
<!-- Search Widget -->
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView"/>
Edit2:
编辑2:
I changed showAsAction="always|collapseActionView"
but it closes my app when I click the search.
我改变了showAsAction="always|collapseActionView"
,但当我点击搜索时它会关闭我的应用程序。
I also put the listener inside OnCreateOptionsMenu to see if it changes anything:
我还将侦听器放在 OnCreateOptionsMenu 中以查看它是否有任何更改:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main_actions, menu);
searchMenuItem = menu.findItem(R.id.action_search);
searchView = (SearchView) searchMenuItem.getActionView();
searchView.setOnQueryTextListener(new OnQueryTextListener(){
@Override
public boolean onQueryTextChange(String arg0) {
return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
if (searchMenuItem != null) {
boolean closed = searchMenuItem.collapseActionView();
Toast.makeText(getApplicationContext(), "closing: " + closed, Toast.LENGTH_SHORT).show();
}
new JsoupGetData("http://api.openweathermap.org/data/2.5/find?q="+ query + "&lang=pl").execute();
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
And the closed
boolean is false
- I don't know why. I read the docbut it tells me nothing.
而closed
布尔是false
-我不知道为什么。我阅读了文档,但它什么也没告诉我。
Edit3:
编辑3:
I've read too much Internet on this subject and it is not clear for me yet. Search widget is cleared, keyboard is hidden and action is performed. The only issue is the search isn't collapsed so it covers the rest of action buttons on the ActionBar.
我在互联网上阅读了太多关于这个主题的信息,但我还不清楚。搜索小部件被清除,键盘被隐藏并执行操作。唯一的问题是搜索没有折叠,因此它覆盖了 ActionBar 上的其余操作按钮。
@Override
public boolean onQueryTextSubmit(String query) {
searchView.setIconified(true);
searchView.clearFocus();
new JsoupGetData("http://api.openweathermap.org/data/2.5/find?q="+ query + "&lang=pl").execute();
return false;
}
HereI read that collapseActionView() will not work, because my Search is not a view but a widget.
在这里,我读到了 collapseActionView() 不起作用,因为我的搜索不是视图而是小部件。
回答by Newinjava
Use:
用:
searchMenuItem.collapseActionView();
searchMenuItem.collapseActionView();
Instead of:
代替:
searchView.onActionViewCollapsed();
searchView.onActionViewCollapsed();
If you are using AppCompat library
, then use:
如果您正在使用AppCompat library
,则使用:
MenuItemCompat.collapseActionView(searchMenuItem);
MenuItemCompat.collapseActionView(searchMenuItem);
回答by Rafael
In my case calling invalidateOptionsMenu();
closes SearchView
在我的情况下,调用会invalidateOptionsMenu();
关闭 SearchView
回答by Darshan Dorai
回答by Khaled Jas
In menu_main.xml add :
在 menu_main.xml 添加:
<item android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
android:title="Search"
app:showAsAction="collapseActionView|ifRoom"
android:orderInCategory="1"
app:actionViewClass="android.support.v7.widget.SearchView"
android:menuCategory="secondary"
/>
and in onCreateOptionsMenu
并在 onCreateOptionsMenu
final MenuItem miSearch = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) miSearch.getActionView();
searchView.setQueryHint("Searh For");
searchView.setOnQueryTextListener(newSearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Toast.makeText(context,query, Toast.LENGTH_LONG).show();
// Here Put Your Code.
//searchView.onActionViewCollapsed();
miSearch.collapseActionView();
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
回答by strwils
It sounds like what you are looking for is a way to close the search bar programmatically. Unfortunately, there is not a method or an equivalent workaround for this. You may have already seen another postthat had some suggestions, but no real way to do this.
听起来您正在寻找的是一种以编程方式关闭搜索栏的方法。不幸的是,没有一种方法或等效的解决方法。您可能已经看到另一篇有一些建议的帖子,但没有真正的方法来做到这一点。
Your first edit above that calls setIconified(true)
is the best alternative. The docs suggest calling setIconified(true)
should collapse the search widget, clear it, and remove it from focus if the attribute iconifiedByDefault
is true (which it is by default so I don't see any problem with your activity_main_actions.xml).
您在上面调用的第一个编辑setIconified(true)
是最好的选择。文档建议调用setIconified(true)
应该折叠搜索小部件,清除它,如果属性iconifiedByDefault
为真(默认情况下是这样,所以我看不到您的activity_main_actions.xml 有任何问题),将其从焦点中移除。
回答by Max
Hi i faced a similar scenario and so i think this changed to code should do the trick.
Hope this helps...:)
嗨,我遇到了类似的情况,所以我认为将其更改为代码应该可以解决问题。
希望这可以帮助...:)
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main_actions, menu);
Menu mMenuItem = menu.findItem(R.id.action_search_loc); // take a reference with menue item
searchView = (SearchView) mMenuItem.getActionView(); //use that to find searchview
searchView.setOnQueryTextListener(searchListener);
return super.onCreateOptionsMenu(menu);
}
SearchView.OnQueryTextListener searchListener = new SearchView.OnQueryTextListener(){
@Override
public boolean onQueryTextChange(String arg0) {
return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
new JsoupGetData("http://api.openweathermap.org/data/2.5/find?q="+ query + "&lang=pl").execute();
try {
mMenuItem.collapseActionView(); //this will collapse your search view
}
catch(Exception ex){
ex.printStackTrace();
System.out.println(ex);
}
return true;
}
};