Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

java - Android: how do I get SearchView close button to return the setQueryHint()?

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

Pic 1:

enter image description here

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

Pic 2:

enter image description here

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:

Pic 3:

enter image description here

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);
}            
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try using mSearchView.setIconified(false).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...