机器人辅助搜索:搜索按钮不会调用搜索活动(其它解决方案并没有帮助) [英] Android assisted Search: The search button does not invoke the searchable Activity (Other Solutions did not help)

查看:155
本文介绍了机器人辅助搜索:搜索按钮不会调用搜索活动(其它解决方案并没有帮助)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这个小的测试应用程序来说明问题,那就是当用户presses键盘上的搜索按钮可搜索活动未启动。

I wrote this small test application to demonstrate the problem, that is the searchable activity is not started when the user presses the search button on the keyboard.

我一直遵循开发者指南的,但是从我的网络搜索,事实证明,官方开发人员指南错过一些点 从我所以搜索(这并没有帮助):

I have been following the developer guides, but from my web search, it turns out that the official developer guide misses some points. From my SO search (which did not help):

  • <一个href="http://stackoverflow.com/questions/11859479/search-bar-widget-not-starting-searchable-activity">Reference 1:通过添加标签的解决  元素清单。我也看了成 清单中的样品用户词典(我不知道在哪里可以 发现样品在线,否则我就链接到它)。这个标签是那里 该应用程序的元素。

  • Reference 1: Solved by Adding tag in the element in the manifest. I also looked into the manifest of the sample "User Dictionary" (I don't know where can I find the samples online, or I would link to it). This tag is there in the application element.

<一个href="http://stackoverflow.com/questions/11987937/search-widget-on-action-bar-doesnt-trigger-my-search-activity">Reference 2:的机器人:标签和机器人:提示中 RES / XML / searchable.xml必须引用字符串资源,而不是 硬codeD字符串。我的是。

Reference 2: The "android:label" and "android:hint" in res/xml/searchable.xml must be references to string resources and not hard coded strings. Mine are.

<一个href="http://stackoverflow.com/questions/11699206/cannot-get-searchview-in-actionbar-to-work/15735869#15735869">Reference 3:添加标签带 机器人:NAME =android.app.default_searchable(和 机器人:值=&LT ;.搜索活性名>)在清单中 要启动从其中进行搜索会活动。尝试 这一点,似乎并没有工作。

Reference 3: Add a tag with "android:name="android.app.default_searchable" " (and " android:value="<. searchable-activity-name>" ") in the manifest in the Activity from where the search is going to be initiated. Tried this, did not seem to work.

参考4:您检索的活动有做一些事情 - 和 实际显示的结果。我的根本,它接收的意图与 ACTION_SEARCH动作,并传递检索所述搜索查询串 从意向到名为performSearch(串)的方法,其 显示字符串的一个TextView。

Reference 4: "Your Searchable activity has to do something - and actually display results." Mine does, it receives the intent with the ACTION_SEARCH action, and passes the search query string retrieved from the intent to a method named "performSearch(string)" which displays the string in a textview.

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

code: MainActivity.java - 有一个单一的搜索查看 - 用户输入的查询和presses搜索键盘上的按钮。

Code: MainActivity.java - Has a single SearchView - the user enters the query and presses the Search button on the keyboard.

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 - 的检索配置

res/xml/searchable.xml - The Searchable Configuration

<?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>

布局:

Layouts:

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:这是疯狂的,我写的搜索dilogue,而不是搜索小插件,这完美的作品一个类似的应用程序


EDIT 1: it's crazy that I wrote a similar app with the search dilogue instead of the search widget, that works perfectly.

我想它在Eclipse中进行调试,但调试停止,因为 TestTwoActivity (可搜索的活动),根本不会启动。

I tried to debug it in Eclipse but debugging stops because the TestTwoActivity (the searchable activity) simply won't start.

推荐答案

我不知道,如果你忘了添加,但你的 MainActivity 惦记设置搜索对信息的搜索查看

I'm not sure if you've forgotten to add it but your MainActivity misses setting the searchable info on the 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()));
}

作为一个方面说明:

As a side note:

我已经使用香精,当有问题的 default_searchable 元标记。它使用的完整路径(跳过味)的搜索活动,例如,当似乎唯一的工作:

I've had problems with the default_searchable meta-tag, when using flavors. It seemed to only work when using the full path (skipping the flavor) to the search activity e.g.:

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

这篇关于机器人辅助搜索:搜索按钮不会调用搜索活动(其它解决方案并没有帮助)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆