因此,我尝试添加一个简单的共享操作,但点击共享操作并不会显示应该出现的流行音乐。
它应该看起来像这样。
我使用appcompatv7和工具栏而不是操作栏
这是我的代码
菜单-XML
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_item_share"
android:orderInCategory="100"
app:showAsAction="always"
android:icon="@drawable/ic_social_share"
android:title="Share"
android:actionProviderClass="android.widget.ShareActionProvider" />
<item
android:title="Add"
android:id="@+id/action_add_new_alarm"
android:orderInCategory="101"
android:icon="@android:drawable/ic_menu_add"
app:showAsAction="always"/>
ONCREATE,ONCREATEOPTIONS和OPTIONS SELECTED-JAVA
//all about the toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.view);
// Set an OnMenuItemClickListener to handle menu item clicks
toolbar.setOnMenuItemClickListener(
new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
// Handle the menu item
switch (item.getItemId()) {
case R.id.action_add_new_alarm: {
startAlarmDetailsActivity(-1);
break;
}
}
return true;
}
});
// Inflate a menu to be displayed in the toolbar
toolbar.inflateMenu(R.menu.alarm_list);
toolbar.setTitle("MY APP");
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate menu resource file.
getMenuInflater().inflate(R.menu.alarm_list, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add_new_alarm: {
startAlarmDetailsActivity(-1);
Log.i(TAG, "add button clicked");
return true;
}
case R.id.menu_item_share: {
Log.i(TAG, "share button clicked");
actionShare();
return true;
}
default:
return super.onOptionsItemSelected(item);
}
}
private void actionShare(){
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_SUBJECT, "my string");
i.putExtra(Intent.EXTRA_TEXT, "another string");
startActivity(i);
//Or like above will always display the chooser
//startActivity(Intent.createChooser(i, getResources().getText(R.string.share)));
}
即使Logcat也没有显示我按下按钮
答案 0 :(得分:3)
onCreateOptionsMenu
/ onOptionsItemSelected
,与ActionBar
一起使用,但正如您所述,您使用的是ToolBar
,并且onMenuItemClick
的实施,并没有t包括启动共享意图的选项