我正在尝试使用2件物品。 “删除”项目可以正常工作。即使它们在屏幕上都正确显示,共享按钮也不会对触摸作出反应。这有什么不对?
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/delete"
android:title="Delete all records"/>
<item
android:id="@+id/menu_item_share"
android:actionProviderClass="android.widget.ShareActionProvider"
android:showAsAction="ifRoom"
android:title="Share"/>
</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.next_activity, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.delete:
// code here
break;
case R.id.menu_item_share:
ShareActionProvider myShareActionProvider = (ShareActionProvider) item.getActionProvider();
String shareSubject = "test Subject";
String shareText = "test 123";
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_SEND);
myIntent.setType("text/plain");
myIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, shareSubject);
myIntent.putExtra(Intent.EXTRA_TEXT, shareText);
myShareActionProvider.setShareIntent(myIntent);
break;
}
return true;
}
答案 0 :(得分:0)
以下是我在其中一个项目中使用共享操作的方法。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
MenuItem shareItem = menu.findItem(R.id.action_share);
mShareActionProvider = (ShareActionProvider) MenuItemCompat
.getActionProvider(shareItem);
mShareActionProvider.setShareIntent(getDefaultIntent());
return super.onCreateOptionsMenu(menu);
}
private Intent getDefaultIntent() {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, curTaskTv
.getText().toString());
intent.putExtra(android.content.Intent.EXTRA_TEXT, curTaskTv.getText()
.toString());
return intent;
}
您似乎必须在onCreateOptionsMenu()
方法