Java 操作栏中的搜索小部件中的提示未显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20082535/
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
Hint in Search Widget within Action Bar is not showing up
提问by Rohan
I have a Search Widget within an Action Bar. I have set up an android:hint value however It is not showing up when I click on the Search Icon.
我在操作栏中有一个搜索小部件。我已经设置了一个 android:hint 值,但是当我点击搜索图标时它没有显示出来。
Relevant Files :
相关文件:
MainActivity.java
主活动.java
package com.example.myApp;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.view.Menu;
import android.widget.SearchView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//creates an action bar.
ActionBar actionBar = getActionBar();
//sets the homebutton.
actionBar.setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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;
}
}
Main.xml => res/menu/Main.xml
Main.xml => res/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"
/>
</menu>
searchable.xml => res/XML/searchable.XMl
searchable.xml => res/XML/searchable.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="search"
android:textColorHint= "#FF0000"
/>
Mainfest.XMl
Mainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.MyApp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.MyApp.MainActivity"
android:label="@string/app_name" >
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
My attempt :
我的尝试:
My action-bar has a black background so I assumed that I initially could not see the :hint value as the hint text color is black is as well. However even after changing the hintTextColor, I still can not see the hint value.
我的操作栏有黑色背景,所以我假设我最初看不到 :hint 值,因为提示文本颜色也是黑色。但是,即使更改了hintTextColor,我仍然看不到提示值。
Any ideas?
有任何想法吗?
Thanks.
谢谢。
采纳答案by Stanislav Bajt
For some reason you need to put <action android:name="android.intent.action.SEARCH"/>
inside your tag. That's what fixed the problem for me.
But be careful to put it in this order
出于某种原因,您需要将其放入<action android:name="android.intent.action.SEARCH"/>
标签中。这就是为我解决问题的原因。但是要注意按这个顺序
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
otherwise your activity won't be recognized as launcher
否则您的活动将不会被识别为启动器
回答by Rohan
No one has been able to answer my question yet...
还没有人能回答我的问题...
I have found a way to change the hint value in the .java file but not in the .xmlfile.
我找到了一种方法来更改 .java 文件中的提示值,而不是.xml文件中的提示值。
Within the onCreateOptionsMenu(Menu menu)
method of MainActivity.java
, I can do:
在 的onCreateOptionsMenu(Menu menu)
方法中MainActivity.java
,我可以执行以下操作:
searchView.setQueryHint("HINT TEXT...");
I believe I should still be able to preset a hint value of a SearchWidget in XML just like a simple EditText
.
我相信我仍然应该能够像一个简单的EditText
.
回答by Neha Nathani
You can use
您可以使用
android:queryHint="Hint text"
in your Search Widget in XML.
在 XML 格式的搜索小部件中。
回答by Upvote
Your have to provide a string resource for the hint attribute. Hardcoded text won't work. Instead of
您必须为提示属性提供字符串资源。硬编码文本不起作用。代替
android:hint="search"
use a string resource
使用字符串资源
android:hint="@string/search_hint"
The doc is clear about it: http://developer.android.com/guide/topics/search/searchable-config.html
该文档对此很清楚:http: //developer.android.com/guide/topics/search/searchable-config.html
android:hint="string resource"
回答by Marko
Accepted answer does indeed work, but only for the MAIN activity. If you would want to use it in another activity it would not work.
接受的答案确实有效,但仅适用于主要活动。如果您想在其他活动中使用它,它将不起作用。
The below code is taken from android developer site
以下代码取自android 开发者网站
<activity
android:name="com.example.Activities.MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
回答by Moeri
If you want to skip to the solution, just scroll down
如果你想跳到解决方案,只需向下滚动
Explanation
解释
It appears this would be a bug in SearchView
.
看来这将是SearchView
.
Edit: I reported this bug here. Edit 2 : seems to be fixed for a future release, see here.
编辑:我在这里报告了这个错误。编辑 2 :似乎已在未来版本中修复,请参见此处。
In the constructor, it assigns a value to the field mQueryHint
.
在构造函数中,它为字段赋值mQueryHint
。
public SearchView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
...
final CharSequence queryHint = a.getText(R.styleable.SearchView_queryHint);
if (!TextUtils.isEmpty(queryHint)) {
setQueryHint(queryHint);
}
...
}
public void setQueryHint(CharSequence hint) {
mQueryHint = hint;
updateQueryHint();
}
Later, when you set the SearchableInfo
(you know, the object it should read the hint from), the following happens:
稍后,当您设置SearchableInfo
(您知道,它应该从中读取提示的对象)时,会发生以下情况:
public void setSearchableInfo(SearchableInfo searchable) {
mSearchable = searchable;
if (mSearchable != null) {
...
updateQueryHint();
}
...
}
All seems ok until now. It saves the searchableInfo
in a field called mSearchable
. Let's see what updateQueryHint
does:
到目前为止一切似乎都很好。它将 保存searchableInfo
在名为 的字段中mSearchable
。让我们看看有什么updateQueryHint
作用:
private void updateQueryHint() {
if (mQueryHint != null) {
mSearchSrcTextView.setHint(getDecoratedHint(mQueryHint));
} else if (IS_AT_LEAST_FROYO && mSearchable != null) {
CharSequence hint = null;
int hintId = mSearchable.getHintId();
if (hintId != 0) {
hint = getContext().getString(hintId);
}
if (hint != null) {
mSearchSrcTextView.setHint(getDecoratedHint(hint));
}
} else {
mSearchSrcTextView.setHint(getDecoratedHint(""));
}
}
What? So if mQueryHint
already has a value, we never even read the searchable info? That explains why we never see our hint, because mQueryHint
is assigned to a default value in the SearchView constructor (see above).
什么?所以如果mQueryHint
已经有了一个值,我们甚至从来没有读过可搜索的信息?这解释了为什么我们从来没有看到我们的提示,因为mQueryHint
在 SearchView 构造函数中分配了一个默认值(见上文)。
Solution
解决方案
So how do we work around this bug?
那么我们如何解决这个错误呢?
Two options:
两种选择:
- Use setQueryHint with the new value.
- 使用带有新值的 setQueryHint。
Example:
例子:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the options menu from XML
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.your_menu, menu);
// 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();
searchView.setOnQueryTextListener(this);
// Assumes current activity is the searchable activity
SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());
searchView.setSearchableInfo(searchableInfo);
// Override the hint with whatever you like
searchView.setQueryHint(getResources().getString(R.strings.your_hint));
return true;
}
- Use setQueryHint with null BEFORE you set the SearchableInfo
- 在设置 SearchableInfo 之前,将 setQueryHint 与 null 一起使用
Example:
例子:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the options menu from XML
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.your_menu, menu);
// 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();
searchView.setOnQueryTextListener(this);
// Assumes current activity is the searchable activity
SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());
// DO THIS BEFORE setSearchableInfo !!!
searchView.setQueryHint(null);
searchView.setSearchableInfo(searchableInfo);
return true;
}
Both confirmed working with appcompat v7.
两者都确认使用 appcompat v7。