4

我有问题。我遵循了很多指南,但仍然无法让我的分享按钮工作。我在 ActionBar 中显示了图标,但是当我按下 android:showAsAction="always" 时什么也没有发生。但是当 android:showAsAction="never" 它工作。我只希望共享图标始终显示在 ActionBar 中。我做错了什么?请帮忙

这是我的代码:

public class Main extends Activity {

    private ShareActionProvider mShareActionProvider;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        MenuItem item = menu.findItem(R.id.shareButton);
        mShareActionProvider = (ShareActionProvider) item.getActionProvider();
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.shareButton:
            Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 
            sharingIntent.setType("text/plain");
            String shareBody = "Check it out";
            sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Subject");
            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
            startActivity(Intent.createChooser(sharingIntent, "Share via"));
            return true;
        case R.id.aboutButton:
            Intent intent = new Intent(this, About.class);
            startActivity(intent);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
}

这是我的 menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/shareButton"
        android:actionProviderClass="android.widget.ShareActionProvider"
        android:title="@string/share"
        android:showAsAction="always"/>  **---> when I put here "NEVER" then it works! But I want Share to be always as icon**

    <item
        android:id="@+id/aboutButton"
        android:actionProviderClass="android.widget.ShareActionProvider"
        android:showAsAction="never"
        android:title="@string/about_dev"/>
</menu>
4

3 回答 3

8

如果您没有找到解决方案,请尝试获取 ShareActionProvider

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share)
            .getActionProvider();
    mShareActionProvider.setShareIntent(doShare());
    return true;
}

并且doShare()会是:

    public Intent doShare() {
    // populate the share intent with data
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_TEXT, "Put whatever you want");
    return intent;
}
于 2014-04-30T21:50:34.790 回答
1

尝试“Intent share = new Intent (Intent.ACTION_SEND)”。你似乎做对了一切!

于 2014-03-05T20:29:50.483 回答
0

case R.id.shareButton:

而不是使用

            Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 

利用:

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
mShareActionProvider.setShareIntent(sharingIntent);

有关更多信息,请参阅以下链接:

https://developer.android.com/reference/android/widget/ShareActionProvider.html

https://developer.android.com/training/sharing/shareaction.html#set-share-intent

于 2018-01-08T15:50:44.143 回答