Set search hint dynamically

后端 未结 7 1445
悲哀的现实
悲哀的现实 2021-02-08 04:38

Does anybody knows how to set android search dialog hint dynamically? T have try to do something like:




        
7条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-08 04:46

    This feels pretty hacky but worked for me - not truly dynamic but worked for alternating between the two search hints I needed:

    1. I created a second searchable.xml (as described in the Android search docs), called searchable2.xml, with my second search hint defined.
    2. I created a dummy activity extended from my original activity and overriding nothing.
    3. In the manifest the dummy activity was associated with the new searchable2.xml:

      
          
              
              
          
          
      
      
      
          
              
              
          
          
      
      
    4. In 'MainActivity' I overrode 'onSearchRequested()' to reference the appropriate searchable activity:

      
      public boolean onSearchRequested()
       {
             SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
      if(searchManager!=null) { // start the search with the appropriate searchable activity // so we get the correct search hint in the search dialog if(/* your condition here */) searchManager.startSearch(null, false,new ComponentName(this, MainActivity.class), null, false); else searchManager.startSearch(null, false,new ComponentName(this, DummyActivity.class), null, false);

      return true; } return false; }

    Nasty. But desperate times call for desperate measures...

    AFAIK, and from looking at the Android source the class is only used to look up the metadata, but if anybody knows differently then please let me know.

提交回复
热议问题