简体   繁体   中英

Android Action Bar Search

I am a newcomer to android and am using Eclipse to learn to write action bars. Code for res/menu/main:

    <item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_search"
      android:title="@string/action_search"
      android:showAsAction="ifRoom" />

Code for MainActivity.java:

   public boolean OnOptionsItemSelected(MenuItem item){
    switch (item.getItemId()) {
    case R.id.action_search:
        openSearch();
        return true;
    case R.id.action_settings:
        openSettings();
        return true;
        default:
        return super.onOptionsItemSelected(item);
    }
}

However, there is a bug regarding R.id.action_search. It is said that it cannot be resolved or is not a field. How can I solve it?

One more question, am I going to define openSearch() and openSettings by myself or they are defined?

Thank you very much.

I believe that your project have to be built automatically to have the R.id working properly.

For your second question, if you want to open the system settings you should look here: How to open Settings of Android Phone on a button click in our Android App

For the search, I don't know if you're looking for a search bar or opening Google search, but for a search bar you should look at the section Add an action view on this page: http://developer.android.com/guide/topics/ui/actionbar.html#ActionView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM