How to track the Share button clicked on the ActionBar?

╄→гoц情女王★ 提交于 2020-01-06 08:43:10

问题


I have added Share option to my ActionBarSherlock, this way:

public boolean onCreateOptionsMenu(final Menu menu) {

menu.add("Share")
    .setIcon(R.drawable.ic_title_share_default)
    .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
}

and on clicking this Icon i want to do something. how can i track the click on this ShareIcon??


回答1:


You should create an XML file to define menu items. For example mymenu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/share"
          android:icon="@drawable/ic_title_share_default"
          android:title="@string/share"
          android:showAsAction="ifRoom"/>
</menu>

Then in the onCreateOptionsMenu you will do:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.mymenu, menu);
    return true;
}

To handle the item selection:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.share:
            //do something for share
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

You can see more information in here:

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

http://actionbarsherlock.com/usage.html



来源:https://stackoverflow.com/questions/12110366/how-to-track-the-share-button-clicked-on-the-actionbar

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