如何使用Android中的xml菜单更改操作栏中搜索框的提示文本?

时间:2016-04-08 10:16:15

标签: android android-actionbar android-menu

我正在开发一款Android应用。在我的应用中,我使用AppCompactActivity使用操作栏。我成功地在操作栏中实现了搜索框。但我有一个小问题。我想更改搜索框中的提示文本。请查看以下屏幕截图:

enter image description here

这是我的带有搜索项

的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/office_action_search"
        android:orderInCategory="97"
        android:title="Search"
        android:icon="@android:drawable/ic_menu_search"
        app:showAsAction="always"
        app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

这是我为操作栏设置带搜索功能的菜单的方法:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.office_main_menu, menu);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

        SearchView search = (SearchView) menu.findItem(R.id.office_action_search).getActionView();
        search.setSearchableInfo(manager.getSearchableInfo(getComponentName()));
        search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                //tabLayout.getTabAt(0).select();
                return true;
            }

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

    return true;
}

如何更改搜索框的提示文字?

3 个答案:

答案 0 :(得分:2)

您可以使用方法SearchView自定义setQueryHint的提示。

像这样修改你的代码:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.office_main_menu, menu);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

        SearchView search = (SearchView) menu.findItem(R.id.office_action_search).getActionView();
        search.setSearchableInfo(manager.getSearchableInfo(getComponentName()));
        search.setQueryHint("Your custom hint");
        search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                //tabLayout.getTabAt(0).select();
                return true;
            }

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

    return true;
}

答案 1 :(得分:0)

请添加以下内容

<style name="SearchViewMy" parent="Widget.AppCompat.Light.SearchView">
    <item name="searchHintIcon">@null</item>
</style>

答案 2 :(得分:0)

我做了一点点不同。在我的情况下,我以这种方式使用 SearchView.SearchAutoComplete setText 方法:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_search, menu);

    final SearchManager searchManager = (SearchManager) getContext().getSystemService(Context.SEARCH_SERVICE);
    final MenuItem searchItem = menu.findItem(R.id.action_search);
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));

    SearchView.SearchAutoComplete searchAutoComplete = (SearchView.SearchAutoComplete) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
    searchAutoComplete.setText("Type your music...");

    // rest of code 
    .....
}