我在操作项上添加了共享按钮,我试图处理操作按钮(操作栏中的按钮)上的单击事件。我尝试使用以下代码,但我知道我必须为此添加clicklistener。以下是我的活动代码:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.d("", "Inside the menu selected");
System.out.println("Inside the menu selected");
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_share:
shareImage();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
这是我的菜单布局的代码:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/menu_settings"/>
<item android:id="@+id/action_share"
android:icon="@drawable/share2"
android:title="search"
android:showAsAction="always" />
</menu>
我还尝试引用:单击具有actionLayout的菜单项时未调用onOptionsItemSelected()
但是我没有得到答案中的itemchooser。
请帮我。
谢谢阿曼
这里的问题是我编写代码的活动是在主要活动下托管的选项卡活动之一。我在tabhost活动下编写了相同的代码,并且有效。
谢谢您的回答。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句