質問

I'm having a Searchview that is immediately focused ( the client wants this ). The text in the search balk is always an icon glass with text Search hint!.

How can I change the text?

I tried with

SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setQueryHint("TEST");

Then the text appears real quick and then changes back to the default of android.

<item
    android:id="@+id/action_search"
    android:icon="@drawable/ic_action_search"
    android:title="@string/action_search"
    android:visible="false"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="collapseActionView|ifRoom" />

EDIT:

This is my creation:

    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);

    menu.findItem(R.id.action_beneficiaries).setVisible(false);
    menu.findItem(R.id.action_search).setVisible(query == null);


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {

        if (query == null) {
            // Associate searchable configuration with the SearchView
            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView()

            searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
            searchView.setIconifiedByDefault(false);
        }
    }

    return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

    // called from menu
    if (getCallingActivity() == null) {
        menu.findItem(R.id.action_accounts).setVisible(true);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            menu.findItem(R.id.action_transaction).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
        } else {

            menu.findItem(R.id.action_transaction).setVisible(false);

        }
    } else {
        // called from New Transaction
        menu.findItem(R.id.action_transaction).setVisible(false);
    }

    return super.onPrepareOptionsMenu(menu);
}
役に立ちましたか?

解決

According to your xml, you are using the Support Library. Therefore, you need to retrieve your item with this following:

MenuItem searchItem = (MenuItem) menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);  

Then, you will be able to change the text of the hint with searchView.setQueryHint() and even its color with setHintTextColor.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top