设置搜索提示动态 [英] Set search hint dynamically

查看:95
本文介绍了设置搜索提示动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何动态设置的Andr​​oid搜索对话框提示? 吨有尝试做一些这样的:

Does anybody knows how to set android search dialog hint dynamically? T have try to do something like:

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
 android:label="@string/search_label"
 android:hint="@string/search_hint"
 android:id="@+id/search_dialog_text">
</searchable>

某处:

@Override
public boolean onSearchRequested() {
  Bundle appSearchData = new Bundle();
  appSearchData.putString("SomeSpecificParam", SomeClass.class.getName());
  startSearch("", false, appSearchData, false);
  EditText  text = (EditText )findViewById(R.id.search_dialog_text);
  text.setHint("Search something else");
  return true;
}

但文本等于空。

but text is equal null.

所以,期待您的建议。谢谢你。

So looking forward you suggestions. Thanks.

推荐答案

这感觉pretty的哈克,但为我工作 - 不是真正的动态,但工作了两个搜索提示我需要之间的交替:

This feels pretty hacky but worked for me - not truly dynamic but worked for alternating between the two search hints I needed:

  1. 在我创建了一个第二searchable.xml(如 Android的搜索文档描述),被称为searchable2.xml,具有确定我的第二个搜索提示。
  2. 在我创建从我原来的活动和压倒一切没有一个虚拟的活动延长。
  3. 在清单中的虚拟活动与新searchable2.xml相关的:

  1. I created a second searchable.xml (as described in the Android search docs), called searchable2.xml, with my second search hint defined.
  2. I created a dummy activity extended from my original activity and overriding nothing.
  3. In the manifest the dummy activity was associated with the new searchable2.xml:

<activity android:name=".MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <action android:name="android.intent.action.SEARCH" />
    </intent-filter>
    <meta-data android:name="android.app.searchable"
        android:resource="@xml/searchable"/>
</activity>


<activity android:name=".DummyActivity">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <action android:name="android.intent.action.SEARCH" />
    </intent-filter>
    <meta-data android:name="android.app.searchable"
        android:resource="@xml/searchable2"/>
</activity>

  • 在MainActivity我推翻onSearchRequested()来引用相应的搜索活动:

  • In 'MainActivity' I overrode 'onSearchRequested()' to reference the appropriate searchable activity:

    
    public boolean onSearchRequested()
     {
           SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
    if(searchManager!=null) { // start the search with the appropriate searchable activity // so we get the correct search hint in the search dialog if(/* your condition here */) searchManager.startSearch(null, false,new ComponentName(this, MainActivity.class), null, false); else searchManager.startSearch(null, false,new ComponentName(this, DummyActivity.class), null, false);

  • 讨厌。但绝望的时代呼唤绝望的措施......

    Nasty. But desperate times call for desperate measures...

    AFAIK,从着眼于Android源类只用于查找元数据,但如果有人知道不同的,那么请让我知道。

    AFAIK, and from looking at the Android source the class is only used to look up the metadata, but if anybody knows differently then please let me know.

    这篇关于设置搜索提示动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆