Android 暂停后退键和菜单键上的活动
在我的应用程序中,我只有一项活动,并且我使用 ViewAnimator 来更改屏幕。对于菜单和后退键,我尝试暂停活动(就像主页键一样)并显示菜单或返回到上一个View
。
愤怒的小鸟也有同样的想法(愤怒的小鸟里约热内卢做了与我想要的完全相同的事情)。
我还检查了 OnKeyDown 事件以了解 Home 键如何工作,但我认为它是由系统处理的。
谢谢。
In my application I have only one activity and I am using ViewAnimator
for changing the screen. For the Menu and Back Keys I am trying to pause the activity (like home key does) and show a menu or go back to previous View
.
Angry Birds do the same think (Angry Birds Rio do exactly the same thing what I want).
I have also checked the OnKeyDown
event to find out how the Home key work but I think it is handled by the system.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了捕获菜单键:
请在此处阅读更多信息: http://developer.android.com/reference/android/app/Activity.html#onCreateOptionsMenu(android.view.Menu)
为了捕获后退键事件:
在此处阅读更多信息:http://developer.android.com/reference/android/app/Activity .html#onKeyUp(int, android.view.KeyEvent)
编辑:
暂停应用程序的逻辑取决于您。但是您可以显示一个
AlertDialog
,最多包含三个按钮和/或任何您想要的视图:In order to catch the menu key:
Read more here: http://developer.android.com/reference/android/app/Activity.html#onCreateOptionsMenu(android.view.Menu)
Ir order to catch the back key event:
Read more here: http://developer.android.com/reference/android/app/Activity.html#onKeyUp(int, android.view.KeyEvent)
EDIT:
Pausing your application's logic is up to you. But you can show an
AlertDialog
with up to three buttons and/or any View you want: http://developer.android.com/reference/android/app/AlertDialog.htmlHome 键永远不会直接被应用程序事件处理程序捕获,有一种方法可以通过一些工作来检测它是否发生,请参阅 onUserLeaveHint 获取主要思想,然后使用 onUserInteraction 尝试确定它是否是 home 键。
不确定您的意图是什么,我有点不清楚这是否就是您想要的答案。
无论如何,根据您的应用程序,您可能需要将其与其他一些状态管理结合起来,但对于非常简单的应用程序,它在大多数情况下都有效。我只用它来进行分析,但它可能对你有用,我会仔细研究一下,以防万一
The home key will never be trapped by an applications event handlers directly, there is way to detect that it happened with a little work see onUserLeaveHint for the main idea and then onUserInteraction to try and determine if it's the home key.
Not sure exactly what your intent is and I am a little unclear if that's all you want answered.
Anyway dependent on your application you might need to combine this with some other state management, but for a very simple applications it works most of the time. I only use it for analytic's but it might work for you, I'll just through it out in case it does
当我正确地回答你的问题时,你不想改变主页按钮的行为,对吧?
要处理 back 和 menuKey,您可以使用 onKeyDown 回调,如下所示:
对于菜单按钮,您也可以使用 onKeyDown 回调,但我认为这不是最好的方法,因为您无论如何都想显示菜单。
您可以使用 onMenuOpend 回调来代替(尽管您应该在准确调用此方法时检查文档):
希望这会有所帮助...
摩根大通
When I get your question correctly you don't want to change the behavior of the home button, right?
To handle the back and menuKey you can use the onKeyDown callback like this:
For the menu button you could also use the onKeyDown callback but I think it's not the best way, since you want to show a menu anyway.
You can use the onMenuOpend callback for that instead (although you should check the docs when this method is called exactly):
Hope this helps...
jpm
如果应用程序冻结或发生其他情况,用户想要退出应用程序,则无法覆盖主页键。这将是他们离开的唯一途径。但是主页键会暂停活动,例如在动画中间按主页键并再次切换到应用程序,动画将在您按主页键的位置继续。
you can't override the home key in case the user wants to exit the app if it freezes or something. it would be the only way for them to leave. But home key pauses the activity e.g. in the middle of the an animation press home key and switch to app again the animation continue where you pressed the home key.