问题

我想在我的Android应用程序中添加"共享"按钮。

像那样

:

我添加了"分享"按钮,但按钮未激活。我点击,但没有任何反应。

我在MainActivity.java中的代码:

private ShareActionProvider mShareActionProvider;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.share_menu, menu);
    getMenuInflater().inflate(R.menu.main, menu);
    MenuItem item = menu.findItem(R.id.share_menu);
    mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share_menu).getActionProvider();
    mShareActionProvider.setShareIntent(getDefaultShareIntent());

    return true;
}

{
    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Text");
    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject");
    startActivity(Intent.createChooser(sharingIntent, "Share using"));
}

我想在第一个选项卡(first_tab.xml)或第二个选项卡(second_tab.xml)中共享文本。

选项卡中的代码(xml)(如果需要):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_color"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity$DummySectionFragment" >

<TextView
    android:id="@+id/section_label1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/text"
    android:textColor="@color/text_color" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:src="@drawable/sprite" />

对不起我的英文


#1 热门回答(256 赞)

添加aButton并点击Button添加此代码:

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 
sharingIntent.setType("text/plain");
String shareBody = "Here is the share content body";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));

有用的链接:
For basic sharingFor customization


#2 热门回答(5 赞)

创建一个具有id共享的按钮,并添加以下代码段。

share.setOnClickListener(new View.OnClickListener() {             
    @Override
    public void onClick(View v) {

        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");
        String shareBody = "Your body here";
        String shareSub = "Your subject here";
        sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, shareSub);
        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
        startActivity(Intent.createChooser(sharingIntent, "Share using"));
    }
});

上面的代码片段将在共享按钮单击操作上打开共享选择器。但是,请注意...共享代码段可能无法使用模拟器输出非常好的结果。为了获得实际结果,请在Android设备上运行代码片段以获得真实结果。


原文链接