SearchView's OnCloseListener doesn't work

前端 未结 18 2330
有刺的猬
有刺的猬 2020-11-27 02:44

I\'m trying to add support for the SearchView in the Android 3.0+ ActionBar, but I can\'t get the OnCloseListener to work.

Here\'s my code:

相关标签:
18条回答
  • 2020-11-27 03:11

    For this problem I came up with something like this,

    private SearchView mSearchView;
    
    @TargetApi(14)
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
    
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.conversation_index_activity_menu, menu);
    
        mSearchView = (SearchView) menu.findItem(R.id.itemSearch).getActionView();
    
        MenuItem menuItem = menu.findItem(R.id.itemSearch);
    
        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH)
        {
            menuItem.setOnActionExpandListener(new OnActionExpandListener()
            {
    
                @Override
                public boolean onMenuItemActionCollapse(MenuItem item)
                {
                    // Do something when collapsed
                    Log.i(TAG, "onMenuItemActionCollapse " + item.getItemId());
                    return true; // Return true to collapse action view
                }
    
                @Override
                public boolean onMenuItemActionExpand(MenuItem item)
                {
                    // TODO Auto-generated method stub
                    Log.i(TAG, "onMenuItemActionExpand " + item.getItemId());
                    return true;
                }
            });
        } else
        {
            // do something for phones running an SDK before froyo
            mSearchView.setOnCloseListener(new OnCloseListener()
            {
    
                @Override
                public boolean onClose()
                {
                    Log.i(TAG, "mSearchView on close ");
                    // TODO Auto-generated method stub
                    return false;
                }
            });
        }
    
    
        return super.onCreateOptionsMenu(menu);
    
    }
    
    0 讨论(0)
  • 2020-11-27 03:12

    Create the menu item with the app:showAsAction set to always.

    <item   
     android:id="@+id/action_search"  
     android:title="..."  
     android:icon="..."  
     app:actionViewClass="android.support.v7.widget.SearchView"  
     app:showAsAction="always"/>
    

    When creating the SearchView in the onCreateOptionsMenu method do something similar

    inflater.inflate(R.menu.menu_search, menu);
    final MenuItem item = menu.findItem(R.id.action_search);
    final SearchView search = (SearchView) item.getActionView();
    search.setQueryHint(getString(R.string.search_brand_item));
    search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
      @Override
      public boolean onQueryTextSubmit(String query) {
        // add your code
        return false;
      }
    
      @Override
      public boolean onQueryTextChange(String newText) {
        // add your code 
        return false;
      }
    });
    search.setOnCloseListener(new SearchView.OnCloseListener() {
      @Override
      public boolean onClose() {
        // add your code here
        return false;
      }
    });
    search.setIconifiedByDefault(true); // make sure to set this to true
    

    The search.setIconifiedByDefault(true) needs to be set to true to call the onClose() method on the SearchView.OnCloseListener() created above.

    0 讨论(0)
  • 2020-11-27 03:14

    It's a workaround but has worked for me

      searchView.setOnQueryTextListener(new android.widget.SearchView.OnQueryTextListener() {
    
                    String lastText;
    
                    @Override
                    public boolean onQueryTextChange(final String newText) {
                        if (lastText != null && lastText.length() > 1 && newText.isEmpty()) {
                            // close ctn clicked
    
                            return true;
                        }
    }
    
    0 讨论(0)
  • 2020-11-27 03:17

    I ended up using a bit of a hack, that works well for my purpose - not sure it'll work with all purposes. Anyway, I'm doing a check to see if the search query is empty. This is not really related to the SearchView's OnCloseListener though - that still doesn't work!

    searchView.setOnQueryTextListener(new OnQueryTextListener() {
                @Override
                public boolean onQueryTextChange(String newText) {
                    if (newText.length() > 0) {
                        // Search
                    } else {
                        // Do something when there's no input
                    }
                    return false;
                }
                @Override
                public boolean onQueryTextSubmit(String query) { return false; }
            });
    
    0 讨论(0)
  • 2020-11-27 03:17

    I used the SearchView close button and set a setOnClickListener on it

    searchView.findViewById<ImageView>(R.id.search_close_btn).setOnClickListener {
        searchView.setQuery("", false)
        searchView.clearFocus()
    }
    
    0 讨论(0)
  • 2020-11-27 03:18

    I have encountered the same problem with onCloseListener not invoking for the SearchView. Understand from the bug issue raised in 25758, and some postings I have read, to invoke onCloseListener, you need to set:

    searchView.setIconifiedByDefault(true);
    

    But for my case I wanted to have the search view opened & not iconified all the time. I manage to resolve this by adding one more line below:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.search_bar, menu);
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        searchView = (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setOnQueryTextListener(queryTextListener);
        searchView.setIconifiedByDefault(true);
        searchView.setIconified(false);
        return true;
    }
    

    The searchView.setIconified(false) will cause the searchView to open up, despite setting the default to iconified to true in the previous line. In this way, I managed to have both a SearchView that opens up all the time & having it invoke the onCloseListener.

    0 讨论(0)
提交回复
热议问题