如何在android中创建搜索按钮

时间:2015-08-05 11:32:05

标签: android android-actionbar

我是android developpement的新手。我想在我的漫画中创建一个搜索功能:

enter image description here

我该怎么办?

  • 当我点击搜索按钮时,它会显示输入文本框。
  • 输入时,会显示匹配项目列表。

1 个答案:

答案 0 :(得分:1)

您需要标记为可搜索的活动。首先将meta和intent过滤器添加到标记为单顶的活动的清单中。

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="...">
<application android:name="..." android:icon="@drawable/logo" android:label="@string/app_name" android:theme="@style/...">
    <activity android:name=".MainActivity" android:label="@string/app_name"
        android:launchMode="singleTop" android:theme="@style/AppTheme">
        <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        ...
    </activity>
    ...
</application>

然后将搜索视图添加到菜单xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/search" android:title="@string/hint_search"
         android:orderInCategory="500" android:icon="@android:drawable/ic_menu_search"
         app:showAsAction="collapseActionView|ifRoom"
         app:actionViewClass="android.support.v7.widget.SearchView" />
    ...
</menu>

然后将您的活动设置为搜索视图

@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.search).getActionView();
    if (searchView != null) {
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setOnCloseListener(new SearchView.OnCloseListener() {
            @Override
            public boolean onClose() {
                //TODO: Reset your views
                return false;
            }
        });
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String s) {
                    return false; //do the default
                }

                @Override
                public boolean onQueryTextChange(String s) {
                    //NOTE: doing anything here is optional, onNewIntent is the important bit
                    if (s.length() > 1) { //2 chars or more
                        //TODO: filter/return results
                    } else if (s.length() == 0) {
                        //TODO: reset the displayed data
                    }
                    return false;
                }

            });
        }
    }
    return true;
}

然后确保您的活动响应搜索意图

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        final String query = intent.getStringExtra(SearchManager.QUERY);
        //TODO: actually do some filtering / set results
    }
}