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 ..." b开发者_开发知识库ut I can't find any reference to how it can be controlled from the SearchManager either.
AFAIK, There isn't any way you can access the EditText provided for search.
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.
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);
Can you add meta-data to an activity during runtime? If you could, then you can assign different xml file for the search.
精彩评论