(已弃用)片段 onOptionsItemSelected 未被调用 [英] (Deprecated) Fragment onOptionsItemSelected not being called
本文介绍了(已弃用)片段 onOptionsItemSelected 未被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这个问题是针对已弃用的夏洛克动作条.现在应该改用 Android 支持库
This question was for the deprecated sherlock action bar. Android support library should be used instead now
我为我的 fragment
添加了一个名为 share 的操作栏菜单选项,该选项出现但未捕获选择事件
I have added an action bar menu option called share for my fragment
which appears but the selection event is not being caught
我是这样添加的
@Override
public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {
MenuItem item = menu.add(0, 7,0, R.string.share);
item.setIcon(R.drawable.social_share).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
尝试在 fragment
和 fragment 活动
中捕获它,例如
Trying to capture it in both the fragment
and the fragment activity
like
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 7:
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT, "I'm being sent!!");
startActivity(Intent.createChooser(share, "Share Text"));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
并且我在 onCreate()
中有 setHasOptionsMenu(true);
.
推荐答案
为 actionbar sherlock 使用而编辑
Edit for actionbar sherlock use
我不得不使用
public boolean onMenuItemSelected(int featureId, MenuItem item) {
在主activity中捕获菜单项
in the main activity to capture the menu item
这篇关于(已弃用)片段 onOptionsItemSelected 未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文