Java 操作栏中搜索小部件中的提示未显示

Java 操作栏中搜索小部件中的提示未显示,java,android,colors,android-actionbar,hint,Java,Android,Colors,Android Actionbar,Hint,我在操作栏中有一个搜索小部件。我已经设置了一个android:hint值,但是当我点击搜索图标时,它不会显示出来 相关文件: MainActivity.java package com.example.myApp; import android.os.Bundle; import android.app.ActionBar; import android.app.Activity; import android.app.SearchManager; import android.conten

我在操作栏中有一个搜索小部件。我已经设置了一个android:hint值,但是当我点击搜索图标时,它不会显示出来

相关文件:

MainActivity.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

<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

 <?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

<?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>

我的尝试:

我的操作栏有一个黑色背景,所以我假设我最初看不到:hint值,因为提示文本颜色也是黑色的。但是,即使在更改hintTextColor之后,我仍然看不到提示值

有什么想法吗


谢谢。

还没有人能回答我的问题

我找到了一种方法来更改.java文件中的提示值,但不更改.xml文件中的提示值

MainActivity.java
onCreateOptions菜单(菜单菜单菜单)
方法中,我可以执行以下操作:

searchView.setQueryHint("HINT TEXT...");

我相信我仍然能够在XML中预设SearchWidget的提示值,就像一个简单的
EditText
一样。出于某种原因,您需要将
放入标记中。这就是我解决问题的原因。 但请注意按此顺序排列

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

否则,您的活动将不会被识别为您可以使用的启动器

android:queryHint="Hint text"

在XML格式的搜索小部件中。

必须为提示属性提供字符串资源。硬编码文本不起作用。而不是

android:hint="search"
使用字符串资源

android:hint="@string/search_hint"
文件对此很清楚:


公认的答案确实有效,但仅适用于主要活动。如果您想在其他活动中使用它,它将不起作用

下面的代码取自


如果要跳到解决方案,只需向下滚动

解释 这似乎是
SearchView
中的一个bug

编辑:我报告了这个错误。 编辑2:似乎在将来的版本中已修复,请参阅

在构造函数中,它为字段
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();
}
稍后,当您设置
SearchableInfo
(您知道,它应该从中读取提示的对象)时,会发生以下情况:

public void setSearchableInfo(SearchableInfo searchable) {
    mSearchable = searchable;
    if (mSearchable != null) {
        ...
        updateQueryHint();
    }
    ...
}
到目前为止一切似乎都很好。它将
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(""));
    }
}
什么?因此,如果
mQueryHint
已经有了一个值,我们甚至从未读取过可搜索的信息?这解释了为什么我们从来没有看到我们的提示,因为
mQueryHint
被分配给SearchView构造函数中的默认值(见上文)

解决方案 那么我们如何解决这个bug呢

两种选择:

  • 将setQueryHint与新值一起使用 例如:

    @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;
    }
    
    @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;
    }
    
  • 在设置SearchableInfo之前,将setQueryHint与null一起使用
  • 例如:

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

    双方均确认与appcompat v7合作。

    I。。。我不明白为什么要强制这样做,但是谢谢你给我一个很棒的建议!不适合我。必须有更多的东西。出于某种原因,您必须使用搜索提示资源。不能使用其他资源名称。像这样:android:hint=“@string/search\u hint”这似乎是android中的一个bug。请参阅下面我的答案,以获取解释+解决方案。调用searchView.setQueryHint(null)对我来说适用于'com.android.support:appcompat-v7:22.0.0'。我希望所有答案都能在这里得到彻底解释。干得好!这是因为文档中说它是需要的,请查看此链接