java 在用作菜单的一部分时为搜索小部件设置提示文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7456763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:03:29  来源:igfitidea点击:

Setting hint text for search widget when used as part of menu

javaandroid

提问by jcvandan

I have a menu item as part of my action bar menu, and I am setting the action view class to the search widget like so:

我有一个菜单项作为我的操作栏菜单的一部分,我将操作视图类设置为搜索小部件,如下所示:

   <item android:id="@+id/menu_search"
        android:title="Search"
        android:icon="@drawable/ic_menu_search"
        android:showAsAction="always"
        android:actionViewClass="android.widget.SearchView" />

This makes the search widget pop out when it is clicked - however, I want it to always be visible, and I also want to be able to set the query hint etc.

这会使搜索小部件在单击时弹出 - 但是,我希望它始终可见,并且我还希望能够设置查询提示等。

When I attempt to call SearchView searchItem = (SearchView) menu.findItem(R.id.menu_search)to get a reference to it, it throws an error as the item cannot be cast to a SearchView.

当我尝试调用SearchView searchItem = (SearchView) menu.findItem(R.id.menu_search)以获取对它的引用时,它会引发错误,因为该项目无法强制转换为SearchView.

采纳答案by Jerome

This will make the ActionView search box always show, by default:

默认情况下,这将使 ActionView 搜索框始终显示:

searchView.setIconifiedByDefault(false);

This tells the search box to always be expanded. Iconified means that you need to tap the icon to make the search box appear.

这告诉搜索框始终展开。图标化意味着您需要点击图标才能显示搜索框。

回答by Raneez Ahmed

You can set hint text to searchview using the api setQueryHint (CharSequence hint)

您可以使用 api setQueryHint(CharSequence 提示)将提示文本设置为 searchview

SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setQueryHint("Query Hint");

Refer the documentation

参考文档

回答by Christopher Souvey

Try getActionView; findItem is returning a MenuItem, not the View it uses

尝试getActionView;findItem 正在返回一个 MenuItem,而不是它使用的 View

(SearchView)menu.findItem(R.id.menu_search).getActionView()

回答by Muhammad Sadiq

minimum API level should be 11 then The error will not be shown

最低 API 级别应为 11,则不会显示错误

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="18" />

回答by Nelson Katale

I added these lines of code and it worked.

我添加了这些代码行并且它起作用了。

searchView.setHint(getString(R.string.places));
searchView.setHintTextColor(R.color.black);

Sample code

示例代码

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.search_item,menu);
    MenuItem item = menu.findItem(R.id.action_search);
    MaterialSearchView searchView = findViewById(R.id.searchview);
    searchView.setMenuItem(item);
    searchView.setHint(getString(R.string.places));
    searchView.setHintTextColor(R.color.black);
    return true;
}

Material Search View library github link

材料搜索查看库github链接