Android分享按钮

时间:2015-04-17 11:59:04

标签: android share-button

在我根据您的回复更新了我的问题之后,这是我的.Class:

public class ShareScreen extends Activity{

         private ShareScreenAdapter adapter;
         private ViewPager viewPager;
         private String[] mThumbIds = Constants.IMAGES;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.share_screen);

             viewPager = (ViewPager) findViewById(R.id.pager);

             ImageButton sharingButton = new ImageButton(this);   
             sharingButton.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
             sharingButton.setImageResource(R.drawable.share);

             Intent i = getIntent();
             int position = i.getIntExtra("position", 0);

             adapter = new ShareScreenAdapter(ShareScreen.this, mThumbIds);

             viewPager.setAdapter(adapter);

             // displaying selected image first
             viewPager.setCurrentItem(position);   

     }

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


        @Override
        public boolean onOptionsItemSelected(MenuItem item) {

             switch (item.getItemId()) {
                case R.id.menu_item_share:
                    shareIt();
                    break;
                default:
                    break;
            }
             return true;

        }


        private void shareIt()
        {
            //sharing implementation here
            String shareBody = "Here is the share content body";

            Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
            sharingIntent.setType("text/plain");

            sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);

            startActivity(Intent.createChooser(sharingIntent, "Share via"));
        }


}

在menu / main_share.xml中,我有分享按钮:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
     <item
            android:id="@+id/menu_item_share"
            android:title="Share"
            android:showAsAction="always"
            android:actionProviderClass=
                "android.widget.ShareActionProvider" />

</menu>

还是不行!问题是我的动作栏中的shareButton无效。我点击它但没有任何反应!

2 个答案:

答案 0 :(得分:3)

我想你想要做的是以下

首先,创建onCreateOptionsMenuonOptionsItemSelected,它适用于ActionBar中点击的按钮。然后,您填写代码以符合您的目标,如下所示:

MainActivity.java

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


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.shareButton:
            shareit();
            break;
        default:
            break;
    }
return true;
} 

menu.xml文件

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/shareButton"
        android:orderInCategory="100"
        android:showAsAction="always"/> 
</menu>

答案 1 :(得分:-1)

很难在没有堆栈跟踪的情况下进行调试,但是到目前为止您从代码中实际上并没有将sharingButton添加到视图中。

[view you want to add the button to].addView(sharingButton)