对当前活动运行搜索意图

时间:2015-03-10 02:25:34

标签: android searchview android-toolbar

我想在位于AboutActivity的WebView上实现文本搜索。问题是,当我运行查询时,会发生辅助活动。我希望我的AboutActivity保持不变, SearchableActivity取而代之。这是我的代码,它是根据documendation实现的。

的Manifest.xml

<meta-data android:name="android.app.default_searchable" android:value=".SearchableActivity"/> <activity android:name=".AboutActivity" android:label="@string/app_name"> </activity> .... <activity android:name=".SearchableActivity" android:label="@string/title_activity_searchable" android:launchMode="singleTop"> <intent-filter> <action android:name="android.intent.action.SEARCH"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/>

AboutActivity.java (工具栏所在的位置)

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.menu_searchable, menu);

    MenuItem searchItem = menu.findItem(R.id.searchItem);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

    SearchableInfo info = searchManager.getSearchableInfo(getComponentName());

    searchView.setSearchableInfo(info);
    //searchView.setIconifiedByDefault(true);
    //searchView.setQueryRefinementEnabled(true);
    //searchView.setQueryHint(getString(R.string.search_hint));

    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings)
    {
        System.out.println("action_settings");
        return true;
    }

    return super.onOptionsItemSelected(item);
}

SearchableActivity.java (我做的活动想要出现)

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_searchable);
    handleIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent)
{
    //super.onNewIntent(intent);
    //setIntent(intent);
    handleIntent(intent);
}

private void handleIntent(Intent intent)
{
    if (Intent.ACTION_SEARCH.equals(intent.getAction()))
    {
        String query = intent.getStringExtra(SearchManager.QUERY);
        System.out.println("query:" + query);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    getMenuInflater().inflate(R.menu.menu_searchable, menu);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
    {
        SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView search = (SearchView) menu.findItem(R.id.searchItem).getActionView();
        search.setIconifiedByDefault(false);
        search.setSearchableInfo(manager.getSearchableInfo(getComponentName()));
        search.setOnQueryTextListener(new SearchView.OnQueryTextListener()
        {
            @Override
            public boolean onQueryTextChange(String query)
            {
                System.out.println("onQueryTextChange");
                return true;
            }

            @Override
            public boolean onQueryTextSubmit(String query)
            {
                return false;
            }
        });
    }
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings)
    {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

MenuSearchable.xml

<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="menelaos.example.com.geolearning.SearchableActivity">
<item android:id="@+id/action_settings"
      android:title="@string/action_settings"
      android:orderInCategory="100"
      app:showAsAction="never"/>
<item
    android:id="@+id/searchItem"
    app:actionViewClass="android.support.v7.widget.SearchView"
    android:orderInCategory="200"
    android:title="@string/search_hint"
    app:showAsAction="ifRoom" />

2 个答案:

答案 0 :(得分:0)

最简单的方法是让您的活动透明化。

将以下内容放在style.xml

<style name="Theme.Transparent" parent="AppBaseTheme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>

并将其应用于清单

中的SearchableActivity
<activity android:name="xxxxx" android:theme="@style/Theme.Transparent">

您的活动现在应该是透明的。

但为什么不使用线程/或asynctask来做呢?

答案 1 :(得分:0)

无需为搜索创建新意图。我对文档感到困惑......我通过简单地处理当前活动中的事件来解决它:

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
    {
        @Override
        public boolean onQueryTextSubmit(String s)
        {
            System.out.println("text Sumbited: "+s);
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s)
        {
            System.out.println("textChanged");
            return false;
        }
    });