Android:onCreateOptionsMenu() 项目操作
我通过以下方式创建了一个菜单:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Email");
return super.onCreateOptionsMenu(menu);
}
但我不记得如何设置 onclicklistener,因此当选择它时我可以运行我的电子邮件功能。
I have a menu created through:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Email");
return super.onCreateOptionsMenu(menu);
}
But I can't remember how to set a onclicklistener so when its selected I can run my email function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
覆盖
onOptionsItemSelected(MenuItem item)
。所以这就像编辑:
既然这已经得到了这么多点,我应该指出,将 ID 添加到菜单选项非常好。确保它们始终唯一的一个好方法是在放置在
res/values
文件夹中的ids.xml
资源中定义它们。ids.xml
然后,当您重写
onCreateOptionsMenu(Menu menu)
方法时,您可以像这样使用 ID:重写
onOptionsItemSelected(MenuItem item)
。这样做的原因是
Activity
会使用菜单选项覆盖它,但Fragments
也可以添加自己的菜单项。使用ids.xml
可确保 ID 是唯一的,无论它们的放置顺序如何。Override
onOptionsItemSelected(MenuItem item)
. So it would be likeEDIT:
Since this has gotten so many points, I should note that it is very good to add ID's to the menu options. A good way to ensure they are always unique is to define them in an
ids.xml
resource that is put in theres/values
folder.ids.xml
Then when you override the
onCreateOptionsMenu(Menu menu)
method, you can use the IDs like so:Override
onOptionsItemSelected(MenuItem item)
.The reason you do this is the
Activity
would override this with menu options, butFragments
can also add their own menu items. Using theids.xml
ensures the IDs are unique no matter which order they are placed.那是行不通的。您应该为菜单项定义 ID:
That won't work. You should define IDs for your menu items:
来自 Android 开发者指南
From Android developer guide