简体   繁体   中英

How to set actions for actionbar buttons?

I know java basics but, I don't know what I'm suppose to do here:

  @Override
    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);
        }
    }

I want to display strings when I press on the actionbar buttons, so what am I going to have to do? Do I have to create the methods openSearch and openSettings? If so, what am I suppose to put inside them??

You can use Toast for example.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.action_search:
            openSearch();
            Toast.makeText(getActivity(), "search!", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.action_settings:
            openSettings();
            Toast.makeText(getActivity(), "settings!", Toast.LENGTH_SHORT).show();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

Your ActionBar buttons come from a Menu inflated in the method onCreateOptionsMenu of your Activity.

This method should look like something like that :

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions, menu);
    return super.onCreateOptionsMenu(menu);
}

R.menu.main_activity_actions is a reference to an XML file that defines your menus. For example, this XML could be something like that :

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="Search Button"/>
    <item android:id="@+id/action_compose"
          android:icon="@drawable/ic_action_compose"
          android:title="Compose Button" />
</menu>

Note that every <item> has an id.

Now, when the user clicks a button in your ActionBar, the method ``onOptionsItemSelected(MenuItem item)` is called in your activity.

In this method, you have to know on which button the user pressed. You can do so with the ID of the provided MenuItem. In the code you provided, every R.id.* are actually references to the same ID you have in the XML.

Finally, following the above examples, if the app should show a Toast when the user click on the Search button; I would have this code in my Activity :

  @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.action_search:
                Toast.makeText(this, "Search Button Pressed !", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.action_compose:
                // do something when the user press the button Compose.
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

Be sure to read the following article to understand the ActionBar : ActionBar

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