簡體   English   中英

操作欄中的搜索窗口小部件中的提示未顯示

[英]Hint in Search Widget within Action Bar is not showing up

我在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;
  }   
}

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>

我的嘗試:

我的動作條有黑色背景所以我假設我最初看不到:提示值,因為提示文字顏色是黑色也是如此。 但是即使在更改了hintTextColor之后,我仍然看不到提示值。

有任何想法嗎?

謝謝。

出於某種原因,您需要在標記內放置<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>

否則您的活動將不會被識別為發射器

還沒有人能夠回答我的問題......

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

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

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

我相信我仍然可以像在簡單的EditText一樣在XML中預設SearchWidget的提示值。

您必須為提示屬性提供字符串資源。 硬編碼文本無效。 代替

android:hint="search"

使用字符串資源

android:hint="@string/search_hint"

該文檔很明確: http//developer.android.com/guide/topics/search/searchable-config.html

android:hint="string resource"

如果您想跳到解決方案,只需向下滾動即可

說明

看來這將是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();
}

稍后,當您設置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呢?

兩種選擇:

  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。

您可以使用

android:queryHint="Hint text"

在您的XML搜索窗口小部件中。

接受的答案確實有效,但僅適用於MAIN活動。 如果你想在另一個活動中使用它,它將無法工作。

以下代碼取自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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM