Android 自动展开并聚焦于 SearchView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11710042/
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
Expand and give focus to SearchView automatically
提问by SweSnow
I'm developing an application where the user presses the "Search" icon in the ActionBar
and a SearchView
is made visible at the top of the screen.
我正在开发一个应用程序,用户在其中按下“搜索”图标,ActionBar
并且 aSearchView
在屏幕顶部可见。
My problem is that the SearchView
is not in focus nor expanded so the user has to press the search button on the Searchview
to make it expand and bring out the keyboard.
我的问题是SearchView
既未聚焦也未展开,因此用户必须按下 上的搜索按钮Searchview
以使其展开并显示键盘。
How should this be solved?
这应该如何解决?
回答by JavierCane
You can also call to expandActionView() method in order to force it:
您还可以调用 expandActionView() 方法以强制执行它:
@Override
public boolean onCreateOptionsMenu( Menu menu )
{
super.onCreateOptionsMenu( menu );
MenuItem searchMenuItem = menu.findItem( R.id.mi_search ); // get my MenuItem with placeholder submenu
searchMenuItem.expandActionView(); // Expand the search menu item in order to show by default the query
return true;
}
Search item in the Action Bar layout:
操作栏布局中的搜索项:
<item
android:id="@+id/mi_search"
android:icon="@drawable/abs__ic_search_api_holo_light"
android:title="@string/search"
android:showAsAction="ifRoom|collapseActionView"
android:actionViewClass="com.actionbarsherlock.widget.SearchView"
/>
回答by Alex Curran
To make the SearchView expanded by default, call setIconifiedByDefault(false)on it when you initialise it (e.g. in onCreateOptionsMenu(..)
or onPrepareOptionsMenu(..)
). I've found in most cases this will give it focus automatically, but if not simply call requestFocus()
on it too.
要使 SearchView 默认展开,请在初始化时调用setIconifiedByDefault(false)(例如 inonCreateOptionsMenu(..)
或onPrepareOptionsMenu(..)
)。我发现在大多数情况下这会自动给它焦点,但如果不是也简单地调用requestFocus()
它。
回答by Pascalius
If you want to have it iconifiedByDefault
, this worked for me. setFocusable
and setIconified
are needed.
如果你想拥有它iconifiedByDefault
,这对我有用。setFocusable
并且setIconified
需要。
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setIconifiedByDefault(true);
searchView.setFocusable(true);
searchView.setIconified(false);
searchView.requestFocusFromTouch();
Update: If you are Using android.support.v7.widget.SearchView
the behaviour us very different. clearFocus
is needed if you don't want the keyboard pop-up all the time. For some reason the menu is recreated all the time, when using appcompat.
更新:如果您使用android.support.v7.widget.SearchView
的行为我们非常不同。clearFocus
如果您不想一直弹出键盘,则需要此选项。出于某种原因,使用 appcompat 时,菜单会一直重新创建。
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setIconified(false);
searchView.clearFocus();
回答by riwnodennyk
If you're using it in layout, you can call
如果您在布局中使用它,则可以调用
mSearchView.onActionViewExpanded()
mSearchView.onActionViewExpanded()
回答by Parth mehta
This worked for me :
这对我有用:
In the root layout :
在根布局中:
xmlns:app="http://schemas.android.com/apk/res-auto"
SearchView defined as follows :
SearchView 定义如下:
<android.support.v7.widget.SearchView
android:id="@+id/search_contacts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="15dp"
android:background="@drawable/search_view"
app:iconifiedByDefault="false"
app:queryHint="Search name or email"
>
<requestFocus />
</android.support.v7.widget.SearchView>
The difference is with app tag.
不同之处在于应用程序标签。
app:iconifiedByDefault="false"
app:queryHint="Search name or email"
回答by saibaba vali
If you are using inside an activity you need to use
如果您在活动中使用,则需要使用
view.onActionViewExpanded();
if you are using inside menu options you need to use
如果您使用的是内部菜单选项,则需要使用
MenuItem.expandActionView();
Note: it works only for SearchView
注意:它仅适用于 SearchView
these two situations are worked for me.
这两种情况对我有用。
回答by Kai Burghardt
This worked for me:
这对我有用:
menu.expandActionView();
回答by Joshua Michael Waggoner
You can use the SearchView#setIconified()
method on a SearchView handle in your Java code. More here:
您可以SearchView#setIconified()
在 Java 代码中的 SearchView 句柄上使用该方法。更多在这里:
http://developer.android.com/reference/android/widget/SearchView.html#setIconified(boolean)
http://developer.android.com/reference/android/widget/SearchView.html#setIconified(boolean)
You can also make use of SearchView#setIconifiedByDefault()
. More info here:
您还可以利用SearchView#setIconifiedByDefault()
. 更多信息在这里:
http://developer.android.com/reference/android/widget/SearchView.html#setIconifiedByDefault(boolean)
http://developer.android.com/reference/android/widget/SearchView.html#setIconifiedByDefault(boolean)
回答by manish poddar
use this
用这个
SearchManager searchManager = (SearchManager) mActivity.getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = new SearchView(mActivity.actionBar.getThemedContext());
searchView.setSearchableInfo(searchManager.getSearchableInfo(mActivity.getComponentName()));
searchView.setIconifiedByDefault(false);
searchView.setQueryHint("Search");
menu.findItem(R.id.action_search).setActionView(searchView);
回答by Javier
For Appcompat Searchview you can use this method:
对于 Appcompat Searchview,您可以使用此方法:
MenuItemCompat.expandActionView(mSearchMenuItem);