SearchView's OnCloseListener doesn't work

前端 未结 18 2574
有刺的猬
有刺的猬 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:20

    seems an old thread already, but I thought I got the same problem API 18 in the first beginning. After googled around, found this thread, another hour read the javadoc tried and errored for something I don't pretend fully understand in javadoc, the following work for me now:

    searchView.setIconifiedByDefault(true);
    
       // OnQueryTextListener
       @Override
       public boolean onQueryTextSubmit(String query) {
          Log.d(tag, "onQueryTextSubmit: " + query);
          return true;
       }
    
       @Override
       public boolean onQueryTextChange(String query) {
          Log.d(tag, "onQueryTextChange: " + query);
          return true;
       }
    
       // OnCloseListener
       @Override
       public boolean onClose() {
          Log.w(tag, "onClose: ");
          return false;
       }
    

    I played with true/false a bit, that somehow makes the difference, and it works for me now. Hopefully, it could save someone time.

提交回复
热议问题