Android:如何获取SearchView关闭按钮以返回setQueryHint()?

时间:2017-04-30 01:30:32

标签: android searchview

我的工具栏中有一个搜索图标,用于搜索RecyclerView列表。

图1:

enter image description here

单击图标时,工具栏显示SearchView的setQueryHint“  在这里搜索......“。

图2:

enter image description here

在SearchView行输入字符时,在本例中为数字“4”,然后在RecyclerView列表中搜索。如果找不到该字符,则字符右侧会出现一个关闭按钮“x”:

图3:

enter image description here

我的问题是,单击关闭按钮“x”时的默认行为是关闭SearchView,它将视图返回到Pic 1,即初始图标View。我想让View返回到SearchView行“Search here ...”,这是Pic 2.我尝试在mCloseButton.OnClickListener中的SearchView上使用setQueryHint,但它不起作用。请参阅下面的mCloseButton OnClickListener()中的**,以了解我尝试过的其他片段无法正常工作。我在这里缺少什么?

menu.xml

<item android:id="@+id/action_search"
    android:title="@string/search_title"
    android:icon="@drawable/ic_action_search"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="always|collapseActionView"  />   

Activity

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.cardview_menu, menu);

    final MenuItem searchItem = menu.findItem(R.id.action_search);

    if (adapter == null || adapter.getItemCount() == 0) {
        searchItem.setVisible(false);
    } else {
        searchItem.setVisible(true);
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        final SearchView mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);            
        mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        final EditText mSearchEditText = (EditText)mSearchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
        final ImageView mCloseButton = (ImageView) mSearchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
        mSearchView.setQueryHint(" Search here...");

        MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {

            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                return true;  // Return true to expand action view
            }

            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {

                adapter.clear();
                adapter.addAll(allList);
                return true;  // Return true to collapse action view
            }
        });

        mCloseButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //Clear the query
                mSearchView.setQuery("",false);
                // below lines were all tried and do not work
                **mSearchView.setQueryHint(" Search here...");**
                **mSearchView.setIconified(false);**
                **mSearchView.onActionViewCollapsed);**
                **MenuItemCompat.expandActionView(searchitem);**
                adapter.clear();
                adapter.addAll(allList);
            }
        });

...
return super.onCreateOptionsMenu(menu);
}            

1 个答案:

答案 0 :(得分:0)

尝试使用mSearchView.setIconified(false)。