web-dev-qa-db-ja.com

AndroidツールバーのSearchView

アクティビティにツールバーがあり、メニューから検索ビューを追加しようとしましたが、ツールバーに検索ビューを追加できませんでした。アクティビティツールバーに検索ビューを追加する方法は?

12
Vishwanath.M

このようにして、Fragmentを使用しました。

onTextChangeListenersearchビューで、Logcat

スクリーンショット参照:

enter image description here

menu.xml-(res-> menu

<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=".HomeActivity">

    <item
        Android:id="@+id/action_search"
        Android:icon="@Android:drawable/ic_menu_search"
        Android:title="Search"
        app:actionViewClass="Android.support.v7.widget.SearchView"
        app:showAsAction="always" />

</menu>

MainFragment.Java

public class MainFragment extends Fragment {

    private SearchView searchView = null;
    private SearchView.OnQueryTextListener queryTextListener;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_main, container, false);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu, menu);
        MenuItem searchItem = menu.findItem(R.id.action_search);
        SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);

        if (searchItem != null) {
            searchView = (SearchView) searchItem.getActionView();
        }
        if (searchView != null) {
            searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));

            queryTextListener = new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextChange(String newText) {
                    Log.i("onQueryTextChange", newText);

                    return true;
                }
                @Override
                public boolean onQueryTextSubmit(String query) {
                    Log.i("onQueryTextSubmit", query);

                    return true;
                }
            };
            searchView.setOnQueryTextListener(queryTextListener);
        }
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_search:
                // Not implemented here
                return false;
            default:
                break;
        }
        searchView.setOnQueryTextListener(queryTextListener);
        return super.onOptionsItemSelected(item);
    }
}

完了

29
Hiren Patel

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:

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

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>

styles.xml:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Android:Theme.Holo.Light">
    <!-- Customize your theme here. -->
</style>

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;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.

    return super.onOptionsItemSelected(item);
}
}

enter image description here

3
BNK