我正在开发一个需要在Onclick方法上打开ShareAction菜单的项目,我似乎无法让它工作。首先,我有ShareAction菜单工作,但只在设置菜单中,这是;是我,当我试图将其应用到的ImageButton的onclick方法Android我怎样才能让ShareActionProvider工作在ImageButton上点击
// This is inside the Oncreate Method
share= (ImageButton)findViewById(R.id.share);
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ShareActionProvider nn= new ShareActionProvider(Login.this);
Intent ShareIntent=new Intent(Intent.ACTION_SEND);
ShareIntent.setAction(Intent.ACTION_SEND);
ShareIntent.setType("text/plain");
ShareIntent.putExtra(Intent.EXTRA_TEXT, "Text To share");
nn.setShareIntent(ShareIntent);
}
});
上面的代码不起作用它不给任何错误或没有。我有相同的代码使用设置菜单的工作,做了以下 菜单的活动
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".Login">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="200" android:icon="@drawable/overflow" app:showAsAction="never" />
<item android:id="@+id/action_share" android:title="@string/action_share" android:orderInCategory="100" app:showAsAction="ifRoom"
android:actionProviderClass="android.widget.ShareActionProvider"
/>
</menu>
然后简单地把它添加到菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_login, menu);
MenuItem shareItem= menu.findItem(R.id.action_share);
ShareActionProvider mShare= (ShareActionProvider)shareItem.getActionProvider();
Intent ShareIntent=new Intent(Intent.ACTION_SEND);
ShareIntent.setAction(Intent.ACTION_SEND);
ShareIntent.setType("text/plain");
ShareIntent.putExtra(Intent.EXTRA_TEXT, "Text To share");
mShare.setShareIntent(ShareIntent);
return true;
}
我缺少什么或者做错了是不是让该菜单在Oncreate中打开?
噢好吧完美谢谢让我知道,我是新来的。由于我需要用户共享单个帖子,因此我将在第二个选项上工作。 –