繁体   English   中英

为什么语音搜索未显示在工具栏搜索操作视图中?

[英]Why do Voice Search isn't shown on my toolbar search action view?

我从http://developer.android.com/training/search/setup.html了解如何在操作栏上添加搜索视图(我使用工具栏)。 我可以很好地添加该功能,但是当我要添加语音搜索时,麦克风图标不会显示。 看来我只需要添加android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"

我还有其他问题。

  1. 我设置了searchView.setIconifiedByDefault(false); 但是为什么默认情况下仍将其图标化?
  2. 当我专注于另一个小部件时,如何使内容视图恢复为图标?

http://i.stack.imgur.com/FUk3T.png

http://i.stack.imgur.com/6cFnV.png

这是我的代码:

MainActivity.java

 public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); handleIntent(getIntent()); } private void handleIntent(Intent intent) { if (Intent.ACTION_SEARCH.equals(intent.getAction())) { String query = intent.getStringExtra(SearchManager.QUERY); TextView textView = (TextView) findViewById(R.id.query); textView.setText(query); } } @Override protected void onNewIntent(Intent intent) { setIntent(intent); handleIntent(intent); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView(); searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); searchView.setIconifiedByDefault(false); return true; } } 

activity_main.xml中

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toolbar" android:layout_height="wrap_content" android:layout_width="match_parent" android:minHeight="?attr/actionBarSize" /> <TextView android:id="@+id/query" android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/toolbar"/> </RelativeLayout> 

searchable.xml

 <?xml version="1.0" encoding="utf-8"?> <searchable xmlns:android="http://schemas.android.com/apk/res/android" android:hint="@string/search_hint" android:label="@string/app_name" android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"/> 

试试这个

    public boolean onCreateOptionsMenu(Menu menu) {
        this.menu = menu;
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu_main, 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()));

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM