Android:如何获取SearchView关闭按钮以返回setQueryHint()? [英] Android: how do I get SearchView close button to return the setQueryHint()?

查看:201
本文介绍了Android:如何获取SearchView关闭按钮以返回setQueryHint()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Search icon in my toolbar that searches a RecyclerView list.

图片1:

单击图标后,工具栏将显示SearchView的setQueryHint 在这里搜索...".

When the icon is clicked, the toolbar shows the SearchView's setQueryHint " Search here...".

图片2:

在SearchView行上输入一个字符时,在这种情况下为数字"4",然后在RecyclerView列表中对其进行搜索.如果找不到该字符,则该字符右侧将显示一个关闭按钮"x":

When a character is entered on the SearchView line, in this case the number "4", it is then searched in the RecyclerView list. If the character is not found, a close button "x" appears to the right of the character:

图片3:

我的问题是,单击关闭按钮"x"时,默认行为是关闭SearchView,该视图将视图返回到Pic 1(初始图标View).我想让View返回到SearchView行"Search here ...",这是Pic2.我尝试在mCloseButton.OnClickListener的SearchView上使用setQueryHint,但它不起作用.请参阅以下mCloseButton OnClickListener()中的**,以了解我尝试的其他无效片段.我在这里想念什么?

My problem is that the default behavior when clicking on the close button "x" is to close the SearchView which returns the view back to Pic 1, the initial icon View. I would like to have the View return to the SearchView line "Search here...", which is Pic 2. I try to use the setQueryHint on the SearchView in the mCloseButton.OnClickListener but it doesn't work. See the ** below in the mCloseButton OnClickListener() for the other snippets I tried that did not work. What am I missing here?

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);
}            

推荐答案

尝试使用mSearchView.setIconified(false).

Try using mSearchView.setIconified(false).

这篇关于Android:如何获取SearchView关闭按钮以返回setQueryHint()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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