Android SearchView禁用/隐藏QuickSearchBox /建议

时间:2018-11-16 14:44:08

标签: android search searchview

我不知道我在做什么错。
我已经读过一些关于stackoverflow和网络的文章,我已经读过official documentation,但是当我点击它时,我不知道如何隐藏或阻止该框在searchview下展开。
查看image以获得更好的解释。 对于实现,我已经:

searchable.xml

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search_hint"
android:includeInGlobalSearch="false"
android:searchSuggestSelection="null"/>

search_menu.xml

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

AndroidManifest.xml

<meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable">
        </meta-data>
<intent-filter
    ...
    <action android:name="android.intent.action.SEARCH" />
</intent-filter>

onCreateOptionsMenu的实现

MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.search_menu, menu);
    // Associate searchable configuration with the SearchView
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    final SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            // perform query here
            query = Uri.encode(query);
            // workaround to avoid issues with some emulators and keyboard devices firing twice if a keyboard enter is used
            // see https://code.google.com/p/android/issues/detail?id=24599
            searchView.clearFocus();

            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return true;
        }
    });
    return super.onCreateOptionsMenu(menu);

0 个答案:

没有答案