【问题标题】:Hint in Search Widget within Action Bar is not showing up操作栏中的搜索小部件中的提示未显示
【发布时间】:2013-12-03 15:42:16
【问题描述】:

我在操作栏中有一个搜索小部件。我已经设置了一个 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 android colors android-actionbar hint


    【解决方案1】:

    出于某种原因,您需要将&lt;action android:name="android.intent.action.SEARCH"/&gt; 放入您的标签中。这就是为我解决问题的原因。 但是要注意按这个顺序排列

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

    否则您的活动将不会被识别为启动器

    【讨论】:

    【解决方案2】:

    还没有人能回答我的问题……

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

    MainActivity.javaonCreateOptionsMenu(Menu menu)方法中,我可以做到:

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

    我相信我仍然可以像简单的EditText 一样在 XML 中预设 SearchWidget 的提示值。

    【讨论】:

    • 这似乎是 Android 中的一个错误。请参阅下面的答案以获取解释+解决方案。
    【解决方案3】:

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

    android:hint="search"
    

    使用字符串资源

    android:hint="@string/search_hint"
    

    文档很清楚:http://developer.android.com/guide/topics/search/searchable-config.html

    android:hint="string resource"
    

    【讨论】:

    • 我...不明白为什么要强制执行,但感谢您的精彩提示!
    • 对我不起作用。必须有更多的东西。
    • 出于某种原因,您必须使用 search_hint 资源。您不能使用其他资源名称。像这样:android:hint="@string/search_hint"
    【解决方案4】:

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

    说明

    这似乎是SearchView 中的一个错误。

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

    在构造函数中,它为字段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 构造函数中被分配了一个默认值(见上文)。

    解决方案

    那么我们如何解决这个错误呢?

    两种选择:

    1. 将 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;
    }
    
    1. 在设置 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());
    
        // DO THIS BEFORE setSearchableInfo !!!
        searchView.setQueryHint(null);
    
        searchView.setSearchableInfo(searchableInfo);
    
        return true;
    }
    

    两者都确认使用 appcompat v7。

    【讨论】:

    • 调用 searchView.setQueryHint(null) 对我有用 'com.android.support:appcompat-v7:22.0.0'
    • 我希望所有 SO 的答案都得到了彻底的解释。干得好!
    【解决方案5】:

    接受的答案确实有效,但仅适用于 MAIN 活动。如果您想在其他活动中使用它,它将无法正常工作。

    以下代码取自android developer site

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

    【讨论】:

      【解决方案6】:

      你可以使用

      android:queryHint="Hint text"
      

      在您的 XML 搜索小部件中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-20
        • 1970-01-01
        • 2014-11-29
        • 2014-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多