简体   繁体   English

在ActionBar Android中显示SearchView

[英]Display SearchView in ActionBar Android

I am trying to display a SearchView inside the ActionBar of my activity. 我正在尝试在活动的ActionBar内显示SearchView I followed android developer guide here here But no SearchView is displayed and I cannot figure out why. 我在这里遵循了android开发人员指南,但是没有显示SearchView ,我也不知道为什么。 Here there is the code of my activity 这是我的活动代码

    public class SearchActivity extends ActionBarActivity {


    private String query;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        FacebookSdk.sdkInitialize(getApplicationContext());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_activity_layout);


        Intent intent = getIntent();
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            this.query = intent.getStringExtra(SearchManager.QUERY);
            Toast.makeText(this,query, Toast.LENGTH_LONG);
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);

        return true;
    }

}

here the is the serachable.xml file 这是serachable.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/search_hint" />

here there is the options_menu.xml file 这里有options_menu.xml文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/search"
        android:title="@string/search_title"
        android:icon="@drawable/ic_action_search"
        android:showAsAction="collapseActionView|ifRoom"
        android:actionViewClass="android.widget.SearchView" />
</menu>

and this is the manifest 这是清单

<activity android:name=".SearchActivity"
              android:label="SearchActivity">

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


    </activity>

the only strange thing is that this line ( android:showAsAction="collapseActionView|ifRoom" ) in the options_menu.xml file is highlighted in red. 唯一奇怪的是, options_menu.xml文件中的这一行( android:showAsAction="collapseActionView|ifRoom" )以红色突出显示。

Try putting this in your options_menu.xml: 尝试将其放在options_menu.xml中:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/search"
        android:title="@string/search_title"
        android:icon="@drawable/ic_action_search"
        app:actionViewClass="android.widget.SearchView"
        app:showAsAction="collapseActionView|ifRoom" />
</menu>

And this in your AndroidManifest.xml 这在您的AndroidManifest.xml中

<activity android:name=".SearchActivity"
    android:label="SearchActivity">

    <intent-filter>
        <action android:name="android.intent.action.SEARCH" />
    </intent-filter>

    <meta-data android:name="android.app.searchable"
        android:resource="@xml/searchable"/>

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

And this in your SearchActivity.java 这在您的SearchActivity.java中

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

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(false);

    return true;
}

@Override
protected void onNewIntent(Intent intent) {
    setIntent(intent);

    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        Log.e("query", intent.getStringExtra(SearchManager.QUERY));
    }
}

For your reference, hope this helps! 供您参考,希望对您有所帮助!

menu_main.xml: menu_main.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/search"
    android:title="@string/search_title"
    android:icon="@drawable/ic_search"
    android:showAsAction="collapseActionView|ifRoom"
    android:actionViewClass="android.widget.SearchView" />

\\res\\xml\\searchable.xml: \\ res \\ xml \\ searchable.xml:

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search_hint" />

AndroidManifest.xml: AndroidManifest.xml:

<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>
        <meta-data android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>

MainActivity.java: MainActivity.java:

package com.example.searchview;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SearchView;

public class MainActivity extends Activity {

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

@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);

    // Associate searchable configuration with the SearchView
    SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView =
            (SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setSearchableInfo(
            searchManager.getSearchableInfo(getComponentName()));


    return true;
}

...
}

Try out the following code 试用以下代码

menu_search.xml menu_search.xml

     <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context=".SearchActivity">
    <item android:id="@+id/search"
        android:title="Search"
        android:icon="@android:drawable/ic_menu_search"
        android:showAsAction="collapseActionView|ifRoom"
        android:actionViewClass="android.widget.SearchView" />
</menu>

I think you are making a mistake in the above code. 我认为您在上述代码中犯了一个错误。 Are you using appcompat library? 您正在使用appcompat库吗? if no, the above code should work. 如果否,则上面的代码应该起作用。 if yes, try using app:showAsAction instead of android:showAsAction and app:actionViewClass="android.support.v7.widget.SearchView" instead of android:actionViewClass="android.widget.SearchView" 如果是,请尝试使用app:showAsAction代替android:showAsActionapp:actionViewClass="android.support.v7.widget.SearchView"代替android:actionViewClass="android.widget.SearchView"

manifest 表现

<activity
        android:name=".SearchActivity"
        android:label="SearchActivity">

        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>

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

        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />

    </activity>

SearchActivity.java SearchActivity.java

@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_search, menu);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    return true;
}

    @Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    System.out.println("onNewIntent SearchResults");
    handleIntent(intent);
}

private void handleIntent(Intent intent) {

    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        System.out.println("Received query: " + query);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM