控制选项菜单的可用性
我有一个活动 (act2),可以从 act1 或 act3 启动。如果它是从 act1 启动的,我希望我的选项菜单可用,这意味着当您按下菜单按钮时,它就会显示。如果它是从 act3 启动的,我的选项菜单中没有任何内容,所以我希望当您按菜单时它不会出现。我该怎么办呢?
I have an activity (act2), which can be launched from act1 or act3. If it is launched from act1, I would like for my option menu to be available, meaning when you press the menu button, it shows up. If it is launched from act3, I have nothing that needs to be in the option menu so I would like for it not to appear when you press menu. How would I go about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您从 act1 调用 act2,则通过 Bundle 传递一个布尔变量作为
true
。如果您从 act3 调用 act2,请将其传递为false
或根本不传递它。在您的 act2 中,特别是在
onCreate()
方法中捕获该布尔变量。您必须小心,如果您没有从 act3 传递包,那么在执行此操作时,它将在 act2 中抛出空指针异常
。现在您可以获得一个布尔变量,帮助 act2 确定它来自哪里。 [如果这是真的,那么它来自第 1 幕或第 2 幕]。现在重写 Activity 中的
onCreateOptionsMenu()
方法,它返回布尔值。因此,如果您传递true
,那么当您按下菜单按钮时,菜单项将弹出,或者如果您返回false
,则不会弹出任何菜单项。使用从 onCreate() 方法获取的布尔变量,您可以简单地返回它,以便当其true
[from act1] 菜单将显示或在false
[from act3]根本不会显示。If you are invoking act2 from act1 pass a boolean variable as
true
through bundle. And if you are invoking act2 from act3 pass it asfalse
or don't pass it at all.In your act2 and specially in
onCreate()
method catch that boolean variable. You have to be careful that if you are not passing a bundle from act3 it will thrownull pointer exception
in act2 while doing this operation. Now you can get a boolean variable that helps act2 to determine where it came from. [if its true than it came from act1 or from act2].now override
onCreateOptionsMenu()
method in your activity, it returns boolean. So if you passtrue
than the menu items will pop up when you press menu button, or no menu items will pop up if you returnfalse
. Use that boolean variable you fetched from onCreate() method, you can simply return it so when itstrue
[from act1] menu will be shown or in case offalse
[from act3] it will not be shown at all.使用意图,这是可能的。例如:
在 act1 中,启动 act2 时,
在 act3 中,您可以将其设置为 false,或者不执行任何操作。
在act2中,检查并显示
Using Intents, it is possible. For example:
In act1, while launching act2,
In act3, you can either set it to false, or do nothing.
In act2, check and show
如果是从 act1 启动的:
如果是从 act3 启动的:
在 act2 中,您在 onCreate: 中读取“enableOptionMenu”:
最后检查 onCreateOptionsMenu 中的enableOptionMenu:
If it is launched from act1:
If it is launched from act3:
In act2 you read "enableOptionMenu" in onCreate:
and finally check that enableOptionMenu in onCreateOptionsMenu: