我正在实施Action Bar
。我想在Action Bar SearchView
的搜索字段区域中使用2个按钮,例如Flipkart app。
与上面的截图一样,我想要2个按钮,第一个用于语音搜索,另一个用于条形码扫描,当点击搜索图标并出现SearchView
Actionbar
时。我可以使用搜索图标实现actionbar serahcview
。但我有点困惑为什么语音搜索没有出现在我的应用程序中。看看我的源代码。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
setSearchTextColour(searchView);
setCloseSearchIcon(searchView);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.action_search:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void setSearchTextColour(SearchView searchView) {
int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
EditText searchPlate = (EditText) searchView.findViewById(searchPlateId);
searchPlate.setTextColor(getResources().getColor(R.color.novoda_blue));
// searchPlate.setBackgroundResource(R.drawable.edit_text_holo_light);
searchPlate.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
}
private void setCloseSearchIcon(SearchView searchView) {
try {
Field searchField = SearchView.class.getDeclaredField("mCloseButton");
searchField.setAccessible(true);
ImageView closeBtn = (ImageView) searchField.get(searchView);
closeBtn.setImageResource(R.drawable.action_cancel);
searchField = SearchView.class.getDeclaredField("mVoiceButton");
searchField.setAccessible(true);
ImageView voiceBtn = (ImageView) searchField.get(searchView);
voiceBtn.setImageResource(R.drawable.ic_launcher);
} catch (NoSuchFieldException e) {
Log.e("SearchView", e.getMessage(), e);
} catch (IllegalAccessException e) {
Log.e("SearchView", e.getMessage(), e);
}
}
我从上面的代码中获得的屏幕截图:
任何帮助都将受到高度赞赏。
答案 0 :(得分:20)
首先,您应该阅读this
使用searchview小部件进行活动的AndroidManifest.xml(必须使用singleTop,请阅读上面的链接):
<activity android:name=".activities.MainActivity"
android:configChanges="orientation|screenSize"
android:launchMode="singleTop" >
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
使用以下方式创建 ... / res / xml / searchable.xml :
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/hint"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"/>
修改onCreateOptionsMenu(最后两行是重要的)
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main_action_bar_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint(getResources().getString(R.string.hint));
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
}
最后覆盖 onNewIntent():
@Override
protected void onNewIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doSearch(query);
}
}
完成这些更改后,您将在搜索视图中看到麦克风按钮。
答案 1 :(得分:0)
如果您没有打电话,请检查您的密码:
searchView.setIconifiedByDefault(true)
删除它后,您应该会看到“声音”图标。
答案 2 :(得分:-1)
只需将WHERE PageID = 1
放入您的Manifest活动代码
android:launchMode="singleTop"