android - 覆盖菜单长按调出应用程序主屏幕和正常按调出菜单

发布于 2024-11-12 07:49:26 字数 1202 浏览 6 评论 0原文

我试图让菜单键调出以下内容:

  1. 长按调出我的应用程序的主屏幕(而不是默认的软键盘)
  2. 正常按下调出菜单。

我可以选择其中之一,但不能两者都做。我缺少什么?一些代码将不胜感激。

谢谢

这是我所拥有的:

  @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  
    {   
     if (keyCode == KeyEvent.KEYCODE_MENU) 
     {
      event.startTracking();
      return true;
     }

 return super.onKeyDown(keyCode, event);
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
   if (keyCode == KeyEvent.KEYCODE_MENU && event.isTracking() && !event.isCanceled())
   {
      this.openOptionsMenu();
   }
   return super.onKeyUp(keyCode,event);
}


 @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_MENU){

            if ( _activityId != WndId.Home)
            {
                navigateHome(NavDirection.Up);
                return (true);
            }
        }
        return super.onKeyLongPress(keyCode,event);
    }
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.muve_menu, menu);
        return true;
    }

I am trying to get the menu key to bring up the following:

  1. On long press to bring up my app's home screen (instead of default soft keyboard)
  2. On normal press to bring up menu.

I can do either or but not both. What am I missing? Some code would be much appreciated.

Thank you

Here is what I have:

  @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  
    {   
     if (keyCode == KeyEvent.KEYCODE_MENU) 
     {
      event.startTracking();
      return true;
     }

 return super.onKeyDown(keyCode, event);
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
   if (keyCode == KeyEvent.KEYCODE_MENU && event.isTracking() && !event.isCanceled())
   {
      this.openOptionsMenu();
   }
   return super.onKeyUp(keyCode,event);
}


 @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_MENU){

            if ( _activityId != WndId.Home)
            {
                navigateHome(NavDirection.Up);
                return (true);
            }
        }
        return super.onKeyLongPress(keyCode,event);
    }
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.muve_menu, menu);
        return true;
    }

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

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

发布评论

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

评论(2

不寐倦长更 2024-11-19 07:49:26

所以如果你调用 this.openOptionsMenu();在 onKeyUp() 中,您可以在常规屏幕上显示菜单,并且通过重定向到主屏幕来覆盖软键盘。

So if you call this.openOptionsMenu(); in the onKeyUp() you get the menu show up on the regular screen and soft keyboard is overriden by redirect to home screen.

清欢 2024-11-19 07:49:26

我怀疑当你说“可以执行其中一个或但不能同时执行两个”时,你的意思是如果你重写 onKeyUp 你看不到 onKeyLongPress ?如果是这样,那是因为,长按的按键事件也会经过 onKeyUp (可能在到达 onKeyLongPress 之前),并且您返回 true 表示您的代码已处理它 - 因此它永远不会到达 onKeyLongPress。

文档中说

“每次按键都由一系列按键事件来描述。一次按键以 ACTION_DOWN 的按键事件开始。如果按住该键足够长的时间以使其重复,则初始按下后会跟随 ACTION_DOWN 的其他按键事件getRepeatCount() 的值为非零,最后一个按键事件是按键按下的 ACTION_UP。”

因此,如果您想区分短按菜单键和长按菜单键,您还必须监视 onKeyDown 并区分多个 ACTION_DOWN。

I suspect that when you say you "can do either or but not both", you mean that if you override onKeyUp you do not see the onKeyLongPress? If so, that is because, the keyevents of the long press are also going through onKeyUp (probably before they get to onKeyLongPress) and you are returning true to say that your code has handled it - hence it never gets to onKeyLongPress.

The docs say that

"Each key press is described by a sequence of key events. A key press starts with a key event with ACTION_DOWN. If the key is held sufficiently long that it repeats, then the initial down is followed additional key events with ACTION_DOWN and a non-zero value for getRepeatCount(). The last key event is a ACTION_UP for the key up."

So if you want to distinguish between a short Menu press and a long menu press, you are also going to have to monitor onKeyDown and distinguish multiple ACTION_DOWNs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文