Android 在菜单可见时处理按键事件

发布于 2024-11-18 02:25:45 字数 244 浏览 5 评论 0原文

这是我的问题。

我覆盖了我的应用程序的音量减小键,以在长时间按下该键时执行某些特定任务,它正常工作。我可以为此拦截按键按下和按键按下事件。

关键侦听器事件是在特定的自定义组件中编写的,而不是在使用该组件的活动中编写的。

当我在按下音量减小键的同时按下菜单按钮,并且在显示菜单时松开菜单按钮时,会出现问题,我无法拦截其按键事件。

您能解释一下其背后的原因是什么吗?而这个问题能不能解决呢?

谢谢。

Here is my problem.

I override volume down key for my application to do some specific task while it's pressed for a long time, it works fine normally. I can intercept key down and key up events for that.

The key listener events are written in particular custom component and not in activity where this component is used.

Problem arises when I press menu button while I have pressed the volume down key, and when I release it while menu is shown, I can not intercept its key up event.

Can you explain what is the reason behind it? And if this problem can be solved or not?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼角的笑意。 2024-11-25 02:25:45

单击菜单时,您的窗口焦点会发生变化。焦点转到菜单视图。因此 onKeyDown() 不起作用,因为如果屏幕焦点位于顶部的视图或活动上,onKeyDown 就会起作用。

使用下面的代码来了解屏幕的焦点

@Override
    public void onWindowFocusChanged(boolean hasFocus) {
        // TODO Auto-generated method stub
        System.out.println("....window focus changed..");
        super.onWindowFocusChanged(hasFocus);
    }

When Menu is clicked your window focus change. Focus come to Menu view. Because of this onKeyDown() does not work because onKeyDown wil work if the focus of the screen will be on the view or activity which is on top.

Use the following code to know focus of the screen

@Override
    public void onWindowFocusChanged(boolean hasFocus) {
        // TODO Auto-generated method stub
        System.out.println("....window focus changed..");
        super.onWindowFocusChanged(hasFocus);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文