本文介绍了动作条麦克风按钮(语音搜索)在搜索查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我在执行操作栏。我想在搜索领域的区域操作栏搜索查看像Flipkart应用2个按钮。I am implementing Action Bar. I want 2 buttons in Search field area of Action Bar SearchView like Flipkart app.就像上面的截图,我要2个按钮,第一个是语音搜索,另一个是吧code扫描仪,当搜索图标点击,搜索查看的出现code>动作条。我能够实施动作条serahcview 与搜索图标。但我有点困惑,为什么语音搜索未出现在我的应用程序。对我的源$ C $ C一起来看看。Like above screenshot, I want 2 buttons, first one is for Voice Search and another is for Barcode Scanner, when Search icon clicked and SearchView of Actionbar appears. I am able to implement actionbar serahcview with search icon. But I am bit confused why voice search not appearing in my app. Have a look on my source code.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); } } 是我从上面code有截图:任何帮助将是非常美联社preciated。Any help would be highly appreciated.推荐答案首先,你应该阅读这 AndroidManifest.xml中与搜索查看小部件的活动(singleTop是必要的,请仔细阅读上面的链接):AndroidManifest.xml for your activity with searchview widget (singleTop is necessary, please read the link above):<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 的这一点:Create .../res/xml/searchable.xml with this:<searchable xmlns:android="http://schemas.android.com/apk/res/android" android:label="@string/app_name" android:hint="@string/hint" android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"/>修改onCreateOptionsMenu(最后两行是物质)Modify onCreateOptionsMenu (last two lines are matter)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()@Overrideprotected void onNewIntent(Intent intent) { if (Intent.ACTION_SEARCH.equals(intent.getAction())) { String query = intent.getStringExtra(SearchManager.QUERY); doSearch(query); }}在这些改变,你会看到一个搜索查看一个麦克风按钮。After these changes you will see a mic button in a searchview. 这篇关于动作条麦克风按钮(语音搜索)在搜索查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-24 17:11