变量搜索提示
在我的 android 项目的 searchable.xml
中,我可以指定在未写入任何文本时出现在 edittext 中的提示。有什么办法可以动态设置这个吗?
在我的操作中,我有两个搜索按钮,它们正在调用 startSearch
并传递它们自己的参数。我想根据单击的按钮设置反映所选操作的提示 - 例如“搜索电影标题”、“搜索演员”。
在我看来,这可能可以通过将参数传递给 startSearch
或使用本地化方法来实现,就像我可以在 values-fr\strings.xml,当单击另一个按钮时,可能会有一个替代搜索资源文件作为目标? 或者如果
searchable.xml
可以被制作成一个选择器,这样我就可以让它在不同的状态下有不同的行为,不知何故,这也很好......问题我还没有找到实现这些目标的方法。
我想要这样做的真正原因是因为在我看来,这是最好的沟通方式,当按下设备搜索按钮时,默认操作是第一个选项,标题搜索。
更新
为了避免混淆,我对任何声明性或更改EditText mSearchTextField
in SearchDialog
。我不清楚的是如何引用该 EditText。链接到的代码中的注释说“这仍然由 SearchManager 控制......”,但我也找不到任何关于如何从 SearchManager 控制它的参考。
In my searchable.xml
in my android project, I get to specify a hint that appears in the edittext, when no text is written. Is there any way to dynamically set this?
In my action, I have two search buttons, that are calling startSearch
passing their own parameters. I would like to set hints that reflect the selected action, based on which button was clicked - say "search movie titles", "search actors".
The way I see it, this could potentially be achievable by passing parameters to startSearch
, or using a localization approach, just as I could place one hint in values-fr\strings.xml
, there might be an alternative search resource file to target for when another button has been clicked? Or if the searchable.xml
could be made into a selector, so that I could have it act differently in different states, somehow, that would also be fine... Problem is I haven't been able to find a means of achieving any of these.
The real reason I want to be doing this, is because the way I see it, it's the best way of communicating that the default action, when the device search button is pressed, is the first option, title search.
UPDATE
To avoid confusion, I'm happy with any declarative or programmatic approach of changing the hint in the EditText mSearchTextField
in SearchDialog
. What I'm unclear about is how to reference that EditText. The comments in the code linked to says "This is still controlled by the SearchManager ..." but I can't find any reference to how it can be controlled from the SearchManager either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
AFAIK,没有任何方法可以访问为搜索提供的 EditText。
AFAIK, There isn't any way you can access the EditText provided for search.
Android 的 XML 不太适合动态;您可能必须在活动代码中解决此问题。
您可以通过使用
setHint()
。然后你只需要按钮上的某种监听器(或者可能是 Spinner)。对于按钮,您只需使用setOnClickListener()
;对于微调器setOnItemSelectedListener ()
会更合适。Android's XML doesn't lend itself too well to dynamism; you'll probably have to fix this problem in the Activity's code.
You could do this in code by changing the hint in the EditText by using
setHint()
. Then you'd just need a listener of some kind on the buttons (or possibly a Spinner). For a Button, you'd just usesetOnClickListener()
; for a spinnersetOnItemSelectedListener()
would be more appropriate.我不确定你的 startSearch 是什么样的,但你正在寻找这样的东西:
I'm not sure what your startSearch looks like, but something like this is what you are looking for:
您可以在运行时向活动添加元数据吗?如果可以的话,您可以为搜索分配不同的 xml 文件。
Can you add meta-data to an activity during runtime? If you could, then you can assign different xml file for the search.