安卓搜索对话框无法正常工作。

5
我使用了Android指南上提供的教程并按照要求完成,但是在设备或菜单项上点击搜索按钮后,搜索对话框仍未打开。 我从这里获取了教程:http://developer.android.com/guide/topics/search/search-dialog.html 我必须在此发布我的示例代码,以便您可以查看并帮助我纠正错误。需要在其中显示对话框的活动。
<activity android:name=".testAndroid"
                  android:theme="@android:style/Theme.NoTitleBar">
                  <meta-data android:name="android.app.default_searchable"
                   android:value="com.tester.android.Search" />
        </activity>

返回搜索结果的活动

<activity android:name=".Search" android:label="Search">
            <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"/>
        </activity>

我的可搜索文件在res/xml文件夹中

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

显示结果的活动

public class Search extends ListActivity {

    private Vector<?> loadedArticles;

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        setContentView(R.layout.articles);
        Intent intent = getIntent();
        String query="";
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            query = intent.getStringExtra(SearchManager.QUERY);
        }

        boolean articlesLoaded = findArticles(query); // it shows results and renders it.

    }
}

请问有人能帮助我吗?为什么我按下搜索硬件按钮后无法看到搜索对话框。

涉及技术方面的问题,请您自行查找相关资料。

你可以轻松地完成它。在这里查看答案:https://stackoverflow.com/a/44131089/3649347 - Tell Me How
1个回答

13

最终我自己找到了解决方案

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="XYZ.com"
    android:hint="Search XYZ.com" >
</searchable>

这里我使用了直接字符串做为标签和提示。相反,我们需要使用@string/label。 我的意思是我们的字符串应该在strings.xml文件中定义,然后再使用它们。 这是Android中的一个错误。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接