如何在ActionBarSherlock中区分两个菜单项的点击?

6

最近我一直在使用ActionBarSherlock,根据各种教程,我编写了以下代码以添加项目到操作栏中

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

不过,我不知道如何区分这两次点击。

虽然我已经发现你必须重写onOptionsItemSelected来处理点击事件,并且可以使用switch语句来区分点击事件,但是大多数教程都使用他们的xml菜单中的item id。由于我不是在xml中创建菜单,所以没有id,那么我该如何区分这两次点击呢?


你有没有特别的原因不在XML文件中定义菜单?这样会更容易。 - MRD
3个回答

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

谢谢,我遇到了同样的问题,现在setIcon()无法工作。你能帮我吗? - sherin

1

请检查以下内容

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

其中包含

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

0

你可以通过 onOptionsItemSelected 中的 ID 来实现它................这个 ID 也可以在此设置

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.

是的,但是什么ID呢?我没有设置任何ID,而且当我添加菜单项时,也没有像.setid(string)这样的方法。 - bakshi_s
哦,谢谢,我一拿到电脑就试试,那就忽略上一个评论吧。 - bakshi_s

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接