我想在工具栏中打开android searchview而不点击搜索图标。这里是XML代码如何在不点击的情况下打开android搜索?
<item
android:id="@+id/action_search"
android:title="@string/search_title"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always"/>
和MainActivity中的Java代码。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem menuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
searchView.setQueryHint("Type something...");
int searchPlateId = searchView.getContext()
.getResources()
.getIdentifier("android:id/search_plate", null, null);
View searchPlate = searchView.findViewById(searchPlateId);
if (searchPlate != null) {
searchPlate.setBackgroundColor(Color.DKGRAY);
int searchTextId = searchPlate.getContext()
.getResources()
.getIdentifier("android:id/search_src_text", null, null);
TextView searchText = (TextView) searchPlate.findViewById(searchTextId);
if (searchText != null) {
searchText.setTextColor(Color.WHITE);
searchText.setHintTextColor(Color.WHITE);
}
}
return true;
}
这工作和下面的输出屏幕...
所以,当我点击搜索图标下面它输出画面......
什么我想要的是直接的第二个屏幕!
'Activity'做'search.performClick();' –
@logic自动调用'onclik'但是..为什么你不直接在工具栏上放一个'Edittext'并且不要搜索图标?你也可以在你的编辑文字上添加一个图标 – Aspicas
@Aspicas EditText需要被设计成看起来像默认的搜索,而不是使用availbale一个更容易和优先 –