android.support.v4.widget.SearchViewCompat例子吗? [英] android.support.v4.widget.SearchViewCompat example?

查看:244
本文介绍了android.support.v4.widget.SearchViewCompat例子吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 SearchViewCompat ActionBarSherlock API中的8应用。

I am trying to use SearchViewCompat with ActionBarSherlock in an API 8 app.

public boolean onCreateOptionsMenu(Menu menu) {
    MenuItem item = menu.add("Search")
        .setIcon(isLight ? R.drawable.ic_search_inverse : R.drawable.ic_search)
        .setActionView(R.layout.collapsible_edittext);
    item.setShowAsAction(
        MenuItem.SHOW_AS_ACTION_ALWAYS | 
        MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

    // To use SearchViewCompat, I need to add it to the Menu item as well:
    View searchView = SearchViewCompat.newSearchView(this);
    // ...
    SearchViewCompat.setOnQueryTextListener(...);
    // ...
    item.setActionView(searchView);

请注意,无论是顶部和底部code需要调用 setActionView()。这是否意味着它是不可能做搜索?

Please note that both the top and bottom code needs to call setActionView(). Does that mean it is not possible to do search?

推荐答案

如果您使用的是ActionBarSherlock库版本4.2,可以更换API 11搜索查看小部件与ActionBarSherlock搜索查看窗口小部件,使其向后兼容的:

If you are using the ActionBarSherlock Library ver 4.2, you can replace the API 11 SearchView Widget with a ActionBarSherlock SearchView Widget to make it backward compatible:

search.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menu_search"
        android:icon="@drawable/ic_action_search"
        android:title="@string/description_search"
        android:orderInCategory="0"
        android:actionViewClass="com.actionbarsherlock.widget.SearchView"
        android:showAsAction="ifRoom|collapseActionView" /> 
</menu>

活动类

//IMPORTANT!!!
import com.actionbarsherlock.widget.SearchView;

...

@Override 
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getSupportMenuInflater().inflate(R.menu.search, menu);
    setupSearchMenuItem(menu);
    return true;
}

private void setupSearchMenuItem(Menu menu) {
    MenuItem searchItem = menu.findItem(R.id.menu_search);
    if (searchItem != null) {
        SearchView searchView = (SearchView) searchItem.getActionView();
        if (searchView != null) {
            SearchManager searchManager = 
                 (SearchManager) getSystemService(SEARCH_SERVICE);
            searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));
            }
        }
    }
}

这篇关于android.support.v4.widget.SearchViewCompat例子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆