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

标签 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.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,我仍然看不到提示值。

有什么想法吗?

谢谢。

最佳答案

由于某些原因,您需要输入 <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>

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

关于java - 操作栏中的搜索小部件中的提示未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20082535/

相关文章:

java - RGB、ARGB 和 RGBA 颜色 channel 位数

java - 在开关按钮中进行套接字编程

Android:在 Eclipse 布局查看器中显示自定义 UI View

r - 使用 R/ggplot2 控制中间值颜色

android - 如何使 View 50% 透明?

android - 使用 Android volley 进行单元测试

java - 空指针异常 : Attempt to invoke virtual method 'long java.util.Date.getTime()' on a null object reference

java - 如何在 Google 应用引擎中设置 Unicode?

java - spring-boot 中存在多个 WebSecurityConfigurerAdapter 的问题

android - 如何在 Jetpack Compose 中实现这种布局