可搜索的活动没有打电话

时间:2018-03-19 07:17:41

标签: android search widget

我正在关注android search interface并开发了以下代码。

  • 能够在其他活动中看到搜索图标。
  • 点击搜索图标获取搜索视图。
  • 搜索和点击输入搜索活动未被调用或调用

  • searchManager.getSearchableInfo(getComponentName())正在返回null

我的代码低于

SearchableActivity.java

public class SearchableActivity extends ListActivity {
    public static String ACTIVITY = "activity";
    final private String TAG = Common.TAG + getClass().getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_searchable);
        // Get the intent, verify the action and get the query
        handleIntent(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        Log.d(TAG, "onNewIntent");
        handleIntent(intent);
    }

    private void handleIntent(Intent intent) {
        Log.d(TAG, "handleIntent");
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            doMySearch(query);
        }

        Bundle appData = getIntent().getBundleExtra(SearchManager.APP_DATA);
        if (appData != null) {
            boolean jargon = appData.getBoolean(SearchableActivity.ACTIVITY);
            Log.i(TAG, "jargon" + jargon);
        }
    }
    public void doMySearch(String query) {
    }
}

menu_other.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.techapps.dhruv.financial.calculator.ScrollingActivity">
    <item
        android:id="@+id/action_search"
        android:orderInCategory="100"
        android:title="Search"
        android:icon="@android:drawable/ic_menu_search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="ifRoom" />
</menu>

OtherActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_other, menu);

    try {
        // Get the SearchView and set the searchable configuration
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        // Assumes current activity is the searchable activity
        if (searchManager.getSearchableInfo(getComponentName()) != null)
            searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(true); // Do not iconify the widget; expand it by default
    } catch (NullPointerException e) {
        e.printStackTrace();
    }
    return true;
}

RES / XML / searchable.xml

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

的AndroidManifest.xml

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <meta-data
      android:name="android.app.default_searchable"
      android:value=".SearchableActivity" />

    <activity android:name=".MainActivity">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <activity
      android:name=".OtherActivity"
      android:label=""
      android:theme="@style/AppTheme.NoActionBar">
      <meta-data
        android:name="android.app.default_searchable"
        android:value=".SearchableActivity" />
    </activity>
    <activity
      android:name=".SearchableActivity"
      android:label="@string/title_activity_searchable"
      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>

1 个答案:

答案 0 :(得分:0)

将以下代码用于onCreateOptionsMenu。用文件名替换菜单文件名和菜单项名称

    @Override
     public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);

    MenuItem search_item = menu.findItem(R.id.mi_search);

    SearchView searchView = (SearchView) search_item.getActionView();
    searchView.setFocusable(false);
    searchView.setQueryHint("Search");
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {


        @Override
        public boolean onQueryTextSubmit(String s) {

            keyword = s.toUpperCase();
            //place your search code here              
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            return false;
        }
    });


    return true;
}

欲了解更多信息,请阅读以下文章, How to implement search in android