如何在android的操作栏中添加搜索小工具?

时间:2014-02-10 05:38:02

标签: android xml

我想在我的操作栏中添加搜索小部件。我已阅读谷歌开发人员的教程,但他们的代码不起作用..请帮助我。我的代码如下:

public boolean onCreateOptionsMenu(android.view.Menu menu) {
    // TODO Auto-generated method stub
    super.onCreateOptionsMenu(menu);

    /* For The Action Bar Menu. Edit Menu For Adding icons */
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.action_menu, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
 return super.onCreateOptionsMenu(menu);
}

首先我使用此代码如下所示,但这也不起作用:

// Associate searchable configuration with the SearchView
     /*SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
                .getActionView();
        searchView.setSearchableInfo(searchManager
                .getSearchableInfo(getComponentName()));*/

    return super.onCreateOptionsMenu(menu);
}

我的舱单代码:

<activity
        android:uiOptions="splitActionBarWhenNarrow"
        android:name="com.usable.mycodes.MainActivity"
        android:label="@string/app_name" >
         <meta-data
            android:name="android.app.default_searchable"
            android:value=".SearchResultsActivity" />
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

搜索活动结果用于处理搜索。

问题是没有点击搜索按钮后显示编辑文本..

1 个答案:

答案 0 :(得分:1)

使用这个......

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.searchview_, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    mSearchView = (SearchView) searchItem.getActionView();
    SearchViewShow(searchItem);

    return true;
}



 private void SearchViewShow(MenuItem searchItem) {

    if (isAlwaysExpanded()) {
        mSearchView.setIconifiedByDefault(false);
    } else {
        searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM
                | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
    }

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    if (searchManager != null) {
        List<SearchableInfo> searchables = searchManager.getSearchablesInGlobalSearch();

        SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
        for (SearchableInfo inf : searchables) {
            if (inf.getSuggestAuthority() != null
                    && inf.getSuggestAuthority().startsWith("applications")) {
                info = inf;
            }
        }
        mSearchView.setSearchableInfo(info);
    }

    mSearchView.setOnQueryTextListener(this);
}

public boolean onQueryTextChange(String newText) {

    return false;
}

public boolean onQueryTextSubmit(String query) {

    return false;
}

public boolean onClose() {

    return false;
}

protected boolean isAlwaysExpanded() {
    return false;
}

使用 SearchView.OnQueryTextListener

实现您的课程