我正在将搜索功能添加到我的应用中。我将搜索小部件添加到工具栏上的片段中,但现在我希望它通过语音搜索扩展到最大值,并在搜索框中再添加一个按钮。
这是它的外观
这是我的工具栏菜单(SearchView)代码
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.searchmenu,menu);
MenuItem item=menu.findItem(R.id.search_PTL);
searchView=new SearchView(((Navigation_Drawer)getActivity()).getSupportActionBar().getThemedContext());
MenuItemCompat.setShowAsAction(item,MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW|MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
MenuItemCompat.setActionView(item,searchView);
Button voiceSearch=new Button(getActivity());
LinearLayout.LayoutParams navButtonsParams = new LinearLayout.LayoutParams(toolbar.getHeight() * 2 / 3, toolbar.getHeight() * 2 / 3);
voiceSearch.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_access_time_black_24dp));
((LinearLayout)searchView.getChildAt(0)).addView(voiceSearch,navButtonsParams);
((LinearLayout)searchView.getChildAt(0)).setGravity(Gravity.CENTER_VERTICAL);
// Toolbar.LayoutParams paramsSearch=new Toolbar.LayoutParams(Toolbar.LayoutParams.MATCH_PARENT,Toolbar.LayoutParams.MATCH_PARENT);
// searchView.setLayoutParams(paramsSearch);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
switchButton.setVisibility(View.VISIBLE);
Log.d(TAG,"Query Text Submit "+query);
searchView.clearFocus();
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
Log.d(TAG,"Query Text Change "+newText);
return false;
}
});
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
Log.d(TAG,"On Focus Change Listener "+b);
switchButton.setVisibility(View.GONE);
searchView.setQueryHint("Search ");
}
});
searchView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG,"Search Button On Clicked Pressed");
// THIS NEVER CALL ON PRESSING OR SUBMIT SEARCH BUTTON
}
});
}
searchmenu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/search_PTL"
android:title="Search"
android:actionLayout="@layout/search_toolbar"
android:icon="@drawable/button_ic_search"
android:actionViewClass="android.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView">
</item>
<item android:id="@+id/toggle_Button"
android:title=""
android:visible="true"
app:actionLayout="@layout/toggle_button"
app:showAsAction="ifRoom">
</item>
</menu>
search_toolbar布局
<?xml version="1.0" encoding="utf-8"?>
<SearchView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</SearchView>
我希望我的搜索视图
答案 0 :(得分:0)
使用此:
<item android:id="@+id/search_PTL"
android:title="Search"
app:actionLayout="@layout/search_toolbar"
android:icon="@drawable/button_ic_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView">
</item>
而不是:
<item android:id="@+id/search_PTL"
android:title="Search"
android:actionLayout="@layout/search_toolbar"
android:icon="@drawable/button_ic_search"
android:actionViewClass="android.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView">
</item>
请按照以下步骤操作。
在onCreateOptionsMenu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.searchmenu, menu);
final MenuItem item = menu.findItem(R.id.search_PTL);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(this);
return true;
}
对您的活动实施SearchView.OnQueryTextListener
。