【问题标题】:Search bar widget in android? [closed]Android中的搜索栏小部件? [关闭]
【发布时间】:2011-05-25 07:37:09
【问题描述】:


是否有任何可用的搜索栏小部件,就像 android 默认搜索栏一样?我只想要像默认的搜索栏,我要处理搜索功能。我可以通过使用编辑文本小部件来做到这一点,但我想知道是否有任何现成的小部件可用?

【问题讨论】:

    标签: android searchbar


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      在你的 onCreate 方法中你可以使用:

      onCreate 方法:

      // enabling action bar app icon and behaving it as toggle button
      getActionBar().setDisplayHomeAsUpEnabled(true);
      getActionBar().setHomeButtonEnabled(true);
      handleIntent(getIntent());
      

      OnCreateOptionsMenu:

      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.feed, menu);
      
          // Associate searchable configuration with the SearchView
          SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
          SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
                  .getActionView();
          searchView.setSearchableInfo(searchManager
                  .getSearchableInfo(getComponentName()));
      
          return true;
      }
      

      菜单:

      <menu xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          tools:context="br.com.srtorcedor.FeedActivity">
      
          <item android:id="@+id/action_search"
              android:icon="@drawable/abc_ic_search"
              android:title="@string/search_title"
              android:showAsAction="always"
              android:actionViewClass="android.widget.SearchView"/>
      
      </menu>
      

      Searchable.xml

      <?xml version="1.0" encoding="utf-8"?>
      <searchable xmlns:android="http://schemas.android.com/apk/res/android"
          android:label="@string/app_label"
          android:hint="@string/search_hint" >
      </searchable>
      

      在fragment中,需要放入以下方法:

      private void handleIntent(Intent intent) {
          if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
              String query = intent.getStringExtra(SearchManager.QUERY);
              doSearch(query);
          }
      }
      
      
      public void onNewIntent(Intent intent) {
          setIntent(intent);
          handleIntent(intent);
      }
      
      private void doSearch(String queryStr) {
          Log.i("Your search: ",queryStr);
      }
      

      这样做,您将在操作栏上看到搜索小部件。我希望它可以与你们一起工作。

      【讨论】:

      • 您将如何在 doSearch 方法中实际进行搜索
      • 你没有提供 setIntent()
      【解决方案3】:

      您可以使用 TextWatcher 类使编辑文本充当搜索栏。

      【讨论】:

        猜你喜欢
        • 2017-06-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多