简单 Android SearchView 无法正常运行

Simple Android SearchView not functioning properly

我正在尝试关注 android.com 中的 search interface manual。我几乎没有问题,我不确定我到底做错了什么。

如果你想看,这里是 project as zip 文件。

1- 当我触摸搜索图标时,android:hint 不可见。它是空的。为什么?如果我以编程方式设置它,它会起作用,例如。 searchView.setQueryHint(getString(R.string.search_hint)); 为什么它在 XML 后不起作用?

2- 当我触摸搜索图标时,搜索字段没有获得焦点并且键盘没有自动出现。我需要触摸搜索文本字段以获得焦点。我相信它应该自动获得焦点?

3- 当我写东西并触摸键盘上的搜索图标时。我没有看到任何记录在 logcat window 的日志条目。缺少什么?

4-(已解决)我在 XML 和 searchView.setIconifiedByDefault(false); 中尝试了 android:iconifiedByDefault="false",当我启动应用程序时它总是图标化。为什么会这样?

我猜我错过了什么地方,但我不确定到底是什么...

下面是我遵循的步骤:

我在 Android Studio 3 中与 'Basic Activity' 开始了一个新项目。 它有一个现成的工具栏和中间的 'Hello World'。

然后到 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="Search hint text"
    android:voiceSearchMode="showVoiceSearchButton|launchRecognizer">
</searchable>

然后更改了 AndroidManifest.xml 文件并添加了一个 activity:

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

然后创建一个简单的SearchableActivity.class。只是为了记录一些行。

package com.test.myapplication;

import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class SearchableActivity extends ListActivity {
    private static String TAG = SearchableActivity.class.getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate");
        super.onCreate(savedInstanceState);

        handleIntent(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        handleIntent(intent);
    }

    private void handleIntent(Intent intent) {
        Log.d(TAG, "handleIntent");
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            //use the query to search your data somehow
        }
    }
}

然后添加到现有 menu_main.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="com.test.myapplication.MainActivity">
    <item android:id="@+id/search"
        android:title="@string/app_name"
        android:icon="@android:drawable/ic_menu_search"
        app:showAsAction="collapseActionView|ifRoom"
        app:actionViewClass="android.widget.SearchView" />
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never" />
</menu>

更新:如果我更改为 app:actionViewClass="android.support.v7.widget.SearchView" 并且在 MainActivity.class 中如果我导入 import android.support.v7.widget.SearchView; 那么当我点击搜索图标时,它会显示默认的 Search... 文本自动获得焦点。但是不显示 XML 的提示,并且当我提交任何信息时仍然没有日志条目。

更新:

我意识到,如果我将 intent-filtermeta 部分放在 MainActivity 下,我就会在 main activity 中获取搜索意图。但是我不明白为什么它不启动SearchableActivity。因为 SearchView 的手册页清楚地写着:

When a user executes a search from the search dialog or widget, the system starts your searchable activity and sends it a ACTION_SEARCH intent.

使用这个

app:actionViewClass="android.support.v7.widget.SearchView"

而不是这个

app:actionViewClass="android.widget.SearchView"

显然,我需要做的就是转发 onCreateOptionsMenu()

中更正 activity 的意图
// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
ComponentName componentName = new ComponentName(this, SearchableActivity.class);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
if (searchManager != null)
    searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName));