变量搜索提示

发布于 2024-08-23 06:19:20 字数 915 浏览 6 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

香草可樂 2024-08-30 06:19:20

AFAIK,没有任何方法可以访问为搜索提供的 EditText。

AFAIK, There isn't any way you can access the EditText provided for search.

酒浓于脸红 2024-08-30 06:19:20

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 use setOnClickListener(); for a spinner setOnItemSelectedListener() would be more appropriate.

百善笑为先 2024-08-30 06:19:20

我不确定你的 startSearch 是什么样的,但你正在寻找这样的东西:

private void startSearch(CharSequence hint) {
    EditText searchBox = (EditText)findViewById(R.id.my_edit_text);
    searchBox.setHint(hint);

I'm not sure what your startSearch looks like, but something like this is what you are looking for:

private void startSearch(CharSequence hint) {
    EditText searchBox = (EditText)findViewById(R.id.my_edit_text);
    searchBox.setHint(hint);
情丝乱 2024-08-30 06:19:20

您可以在运行时向活动添加元数据吗?如果可以的话,您可以为搜索分配不同的 xml 文件。

Can you add meta-data to an activity during runtime? If you could, then you can assign different xml file for the search.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文