是否可以仅使用搜索窗口小部件(SearchView)传递搜索上下文数据?

Eug*_*yuk 2 android android-intent android-searchmanager searchview search-dialog

根据官方文档,有两种方法可以提供搜索界面:使用搜索对话框或SearchView小部件.我想注意使用这两种方式传递搜索上下文数据.

所以,文档说:

..您可以提供系统发送给您的可搜索活动的意图中的其他数据.您可以传递APP_DATA包中的附加数据,该包含在ACTION_SEARCH目的中.

要将此类数据传递给可搜索的活动,请覆盖用户可以执行搜索的活动的onSearchRequested()方法,使用其他数据创建Bundle,并调用startSearch()以激活搜索对话框.例如:

@Override
public boolean onSearchRequested() {
     Bundle appData = new Bundle();
     appData.putBoolean(SearchableActivity.JARGON, true);
     startSearch(null, false, appData, false);
     return true;
}
Run Code Online (Sandbox Code Playgroud)

..一旦用户提交查询,它就会与您添加的数据一起发送到您的可搜索活动.您可以从APP_DATA Bundle中提取额外数据以优化搜索.例如:

Bundle appData = getIntent().getBundleExtra(SearchManager.APP_DATA);
if (appData != null) {
    boolean jargon = appData.getBoolean(SearchableActivity.JARGON);
}
Run Code Online (Sandbox Code Playgroud)

这是指搜索对话框.那么搜索小部件呢?

是否可以SearchView仅使用小部件传递搜索上下文数据?

希望,有人可以给出明确的解释和/或建议另一种或类似的方式来实现目标.

谢谢!

Eug*_*yuk 8

我发现了解决方案.甚至两个解决方案

他们不需要调用onSearchRequested()因此根本没有搜索对话框:)

首先,我提供了一些常见的步骤来创建搜索界面,然后给出源问题的解决方案.

我们通过res/menu/options_menu.xml使用以下代码创建文件,将搜索视图添加到应用栏:

<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity" >

    <item
        android:id="@+id/action_search"
        android:icon="@drawable/ic_action_search"
        android:title="@string/search_string"
        app:showAsAction="collapseActionView|ifRoom"
        app:actionViewClass="android.support.v7.widget.SearchView" />

</menu>
Run Code Online (Sandbox Code Playgroud)

res/xml/searchable.xml文件中创建可搜索的配置:

<?xml version="1.0" encoding="utf-8"?>

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
        android:label="@string/app_name"
        android:hint="@string/search_hint" />
Run Code Online (Sandbox Code Playgroud)

AndroidManifest.xml以下位置声明两项活动:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.searchinterface">

    <application
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

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

    </application>

</manifest>
Run Code Online (Sandbox Code Playgroud)

创建一个Searchable Activity,我们处理ACTION_SEARCH从中传递的intent和搜索上下文数据MainActivity.我们从中提取额外数据APP_DATA Bundle以优化搜索:

public class SearchableActivity extends AppCompatActivity {
    public static final String JARGON = "com.example.searchinterface.jargon";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_searchable);

        handleIntent(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        handleIntent(intent);
    }

    private void handleIntent(Intent intent) {

        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            // use the query to search the data somehow

            Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
            if (appData != null) {
                boolean jargon = appData.getBoolean(SearchableActivity.JARGON);
                // use the context data to refine our search
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,我们需要实现我们的MainActivity课程.因此,我们扩充菜单并配置SearchView元素.我们还需要设置SearchView.OnQueryTextListener和实现其方法,尤其是onQueryTextSubmit().当用户按下提交按钮时它被调用,它包含将搜索上下文数据传递给的按钮的主要逻辑SearchableActivity.最后,我们到达了主要答案部分.正如我所说,有两种解决方案:

1.使用Bundleextra 创建一个intent 并将其发送到SearchableActivity手动;

这是MainActivity所有必要的内容:

public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
    private SearchView mSearchView;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.options_menu, menu);

        MenuItem searchItem = menu.findItem(R.id.action_search);
        mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);

        // associate searchable configuration with the SearchView
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        mSearchView.setSearchableInfo(searchManager.getSearchableInfo(
                new ComponentName(this, SearchableActivity.class)));

        mSearchView.setOnQueryTextListener(this);

        return true;
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        Intent searchIntent = new Intent(this, SearchableActivity.class);
        searchIntent.putExtra(SearchManager.QUERY, query);

        Bundle appData = new Bundle();
        appData.putBoolean(SearchableActivity.JARGON, true); // put extra data to Bundle
        searchIntent.putExtra(SearchManager.APP_DATA, appData); // pass the search context data
        searchIntent.setAction(Intent.ACTION_SEARCH);

        startActivity(searchIntent);

        return true; // we start the search activity manually
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

感谢/sf/answers/1552889621/.

第二个解决方案也被放入onQueryTextSubmit()(但没有必要):

2.创建搜索上下文数据Bundle并将其传递给setAppSearchData()方法SearchView.

因此,我们不需要创建并传递整个搜索意图并启动相应的可搜索活动,系统将负责处理它.

这是另一个代码片段:

/*
 You may need to suppress the “restrictedApi” error that you could possibly
 receive from this method "setAppSearchData(appData)”.I had to
 I’m targetSdkVersion 26. I’m also using Android Studio 3 
 with the new gradle plugin, which might be causing this.

 If you’re not running Android Studio 3 you can simply put
 “//noinspection RestrictedApi" 
  right above the line: mSearchView.setAppSearchData(appData);
 */
@SuppressWarnings("RestrictedApi")
@Override
public boolean onQueryTextSubmit(String query) {
    Bundle appData = new Bundle();
    appData.putBoolean(SearchableActivity.JARGON, true); // put extra data to Bundle
    mSearchView.setAppSearchData(appData); // pass the search context data

    return false; // we do not need to start the search activity manually, the system does it for us 
}
Run Code Online (Sandbox Code Playgroud)

感谢/sf/answers/2680713311/.

注意:只有支持库的SearchView(android.support.v7.widget.SearchView)版本包含该setAppSearchData()方法,所以请注意.