How to distinguish two menu item clicks in ActionBarSherlock?

痞子三分冷 提交于 2019-12-06 00:16:10

问题


I have been working with ActionBarSherlock recently, and follwing various tutorials, I wrote this code to add items to Action bar

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    menu.add("Refresh")
        .setIcon(R.drawable.ic_action_refresh)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);


    menu.add("Search")// Search
        .setIcon(R.drawable.ic_action_search)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        return true;
}

However, I dont know how to distinguish the two clicks.

Although I did find out that you have to Override onOptionsItemSelected to handle the clicks and also that a switch statement can be used to distinguish between clicks, but most tutorials use item ids from thier xml menus. Since I am not creating menus in xml how can i distinguish the clicks without ids.


回答1:


private static final int REFRESH = 1;
private static final int SEARCH = 2;

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    menu.add(0, REFRESH, 0, "Refresh")
        .setIcon(R.drawable.ic_action_refresh)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);


    menu.add(0, SEARCH, 0, "Search")
        .setIcon(R.drawable.ic_action_search)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case REFRESH:
            // Do refresh
            return true;
        case SEARCH:
            // Do search
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}



回答2:


Just check following

http://developer.android.com/guide/topics/ui/actionbar.html

which contains

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {   <--- here you can get it
        case android.R.id.home:
            // app icon in action bar clicked; go home
            Intent intent = new Intent(this, HomeActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}



回答3:


you can do ti by there Id in onOptionsItemSelected ................which can be set here also

http://thedevelopersinfo.wordpress.com/2009/10/29/handling-options-menu-item-selections-in-android/

http://developer.android.com/reference/android/view/Menu.html#add(int, int, int, java.lang.CharSequence)

use 
public abstract MenuItem add (int groupId, int itemId, int order, CharSequence title)

Since: API Level 1
Add a new item to the menu. This item displays the given title for its label.
Parameters

groupId The group identifier that this item should be part of. This can be used to define groups of items for batch state changes. Normally use NONE if an item should not be in a group.
itemId  Unique item ID. Use NONE if you do not need a unique ID.
order   The order for the item. Use NONE if you do not care about the order. See getOrder().
title   The text to display for the item.
Returns

The newly added menu item.


来源:https://stackoverflow.com/questions/10849964/how-to-distinguish-two-menu-item-clicks-in-actionbarsherlock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!