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

Roh*_*han 17 java android colors hint android-actionbar

我在Action Bar中有一个Search Widget.我已经设置了一个android:提示值但是当我点击搜索图标时它没有显示出来.

相关文件:

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;
  }   
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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"      
    />  
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

我的尝试:

我的动作条有黑色背景所以我假设我最初看不到:提示值,因为提示文字颜色是黑色也是如此.但是即使在更改了hintTextColor之后,我仍然看不到提示值.

有任何想法吗?

谢谢.

小智 23

出于某种原因,您需要放入<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>
Run Code Online (Sandbox Code Playgroud)

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

  • 这是有效的,因为文档说它是必要的,请看这个链接https://developer.android.com/intl/es/training/search/setup.html#create-sa (2认同)

Roh*_*han 18

还没有人能够回答我的问题......

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

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

searchView.setQueryHint("HINT TEXT...");
Run Code Online (Sandbox Code Playgroud)

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

  • 这似乎是Android中的一个错误.请参阅下面的答案以获得解释+解决方案. (2认同)

Upv*_*ote 14

您必须为提示属性提供字符串资源.硬编码文本无效.代替

android:hint="search"
Run Code Online (Sandbox Code Playgroud)

使用字符串资源

android:hint="@string/search_hint"
Run Code Online (Sandbox Code Playgroud)

该文档很明确:http://developer.android.com/guide/topics/search/searchable-config.html

android:hint="string resource"
Run Code Online (Sandbox Code Playgroud)

  • 我...不明白为什么这是强制执行,但感谢一个很棒的提示! (2认同)

Moe*_*eri 13

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

说明

看起来这将是一个错误SearchView.

编辑:我在这里报告了这个错误.编辑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();
}
Run Code Online (Sandbox Code Playgroud)

稍后,当您设置SearchableInfo(您知道它应该从中读取提示的对象)时,会发生以下情况:

public void setSearchableInfo(SearchableInfo searchable) {
    mSearchable = searchable;
    if (mSearchable != null) {
        ...
        updateQueryHint();
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

直到现在一切似乎还可以.它保存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(""));
    }
}
Run Code Online (Sandbox Code Playgroud)

什么?所以,如果mQueryHint已经有价值,我们甚至从未阅读过可搜索的信息?这就解释了为什么我们永远不会看到我们的提示,因为它mQueryHint被分配给SearchView构造函数中的默认值(参见上文).

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

两种选择:

  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;
}
Run Code Online (Sandbox Code Playgroud)
  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;
}
Run Code Online (Sandbox Code Playgroud)

两人都确认使用appcompat v7.