【问题标题】:Android assisted Search: The search button does not invoke the searchable Activity (Other Solutions did not help)Android 辅助搜索:搜索按钮不调用可搜索的 Activity(其他解决方案没有帮助)
【发布时间】:2015-02-18 11:49:27
【问题描述】:

我写了这个小测试应用程序来演示这个问题,即当用户按下键盘上的搜索按钮时,可搜索的活动没有启动。

我一直在关注developer guides,但是从我的网络搜索中,事实证明官方开发者指南遗漏了一些要点。 从我的 SO 搜索中(没有帮助) :

  • Reference 1: 通过在 清单中的元素。我也调查了 示例“用户词典”的清单(我不知道在哪里可以 在线查找示例,或者我会链接到它)。这个标签在 应用程序元素。

  • Reference 2: 中的“android:label”和“android:hint” res/xml/searchable.xml 必须是对字符串资源的引用,而不是 硬编码字符串。我的是。

  • Reference 3: 添加标签 “android:name="android.app.default_searchable"”(和“ android:value="<.searchable-activity-name>" ") 在清单中 开始搜索的活动。试过了 这个,好像没用。

  • Reference 4: "您的可搜索活动必须做一些事情 - 并且 实际显示结果。” 我的确实如此,它通过 ACTION_SEARCH 操作,并传递检索到的搜索查询字符串 从意图到名为“performSearch(string)”的方法 在文本视图中显示字符串。

那么我做错了什么,我能做些什么来解决这个问题?

代码: MainActivity.java - 有一个 SearchView - 用户输入查询并按下键盘上的搜索按钮。

public class MainActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

TestTwoActivity.java

    public class TestTwoActivity extends Activity {
        TextView tv;
        private static final String TAG = TestTwoActivity.class.getSimpleName();

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

            /**
             * The following code enables assisted search on the SearchView by calling setSearchableInfo() and passing it our SearchableInfo object.
             */
            SearchView searchView = (SearchView) findViewById(R.id.searchActivity_searchView);
            // SearchManager => provides access to the system search services.

            // Context.getSystemService() => Return the handle to a system-level
            // service by name. The class of the returned object varies by the
            // requested name.

            // Context.SEARCH_SERVICE => Returns a SearchManager for handling search

            // Context = Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android
            // system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching
            // activities, broadcasting and receiving intents, etc.

            // Activity.getComponentName = Returns a complete component name for this Activity 

            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

            /**
             * If the search is executed from another activity, the query is sent to this (searchable) activity in an Intent with ACTION_SEARCH action.
             */
            // getIntent() Returns the intent that started this Activity
            Intent intent = getIntent();
            if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
                Log.i(TAG, "Search Query Delivered");//check
                String searchQuery = intent.getStringExtra(SearchManager.QUERY);
                performSearch(searchQuery);
            }

        }

        private void performSearch(String searchQuery) {
            //Just for testing purposes, I am simply printing the search query delivered to this searchable activity in a textview.
            tv = (TextView) findViewById(R.id.testTwoActivity_textView);
            tv.setText(searchQuery);
        }
}

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/searchViewHint" >
</searchable>

清单文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tests"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/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=".TestTwoActivity"
            android:label="@string/title_activity_test_two" >
            <intent-filter>
                <action android:name="android.intent.action.SEARCH"/> <!-- Declares the activity to accept ACTION_SEARCH intent -->
            </intent-filter> 
                <meta-data 
                    android:name="android.app.searchable"
                    android:resource="@xml/searchable" /> <!-- Specifies the searchable configuration to use --> 
        </activity>

        <!-- Points to searchable activity so the whole app can invoke search. -->
        <meta-data android:name="android.app.default_searchable"
                   android:value=".TestTwoActivity" />

    </application>

</manifest>

布局:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:orientation="vertical"

    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context="com.tests.MainActivity" >

    <android.support.v7.widget.SearchView
        android:id="@+id/searchActivity_searchView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         /> 

</LinearLayout>

activity_test_two.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context="${relativePackage}.${activityClass}" >

    <android.support.v7.widget.SearchView
        android:id="@+id/searchActivity_searchView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         /> 

    <TextView
        android:id="@+id/testTwoActivity_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

编辑 1: 很疯狂,我用搜索对话而不是搜索小部件编写了一个类似的应用程序,它运行良好。

我尝试在 Eclipse 中调试它,但调试停止,因为 TestTwoActivity(可搜索活动)根本无法启动。

【问题讨论】:

    标签: java android search android-search android-searchmanager


    【解决方案1】:

    我不确定您是否忘记添加它,但您的MainActivity 错过了在SearchView 上设置可搜索信息:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        SearchView searchView = (SearchView) findViewById(R.id.searchActivity_searchView);
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    }
    

    附注:

    我在使用风味时遇到了default_searchable 元标记的问题。它似乎只在使用搜索活动的完整路径(跳过风味)时才有效,例如:

    <meta-data 
        android:name="android.app.default_searchable"
        android:value="com.example.SearchActivity"/>
    

    【讨论】:

    • 我忘记了这一点,我看了我的代码数百次,但没有看到这个,我的错。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-20
    • 2012-05-06
    • 2017-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多