Android,为什么当我使用 onOptionsItemSelected() 时,选项菜单不起作用?

发布于 2025-01-07 22:43:43 字数 1427 浏览 4 评论 0原文

今天我很惊讶为什么当我在代码中添加 onKeyDown() 时我的菜单没有显示!

在我的项目中,我有菜单,并以常规方式使用 onCreateOptionsMenu()onOptionsItemSelected() 设计它。该项目没有问题并且运行良好。

问题在于使用onKeyDown()。当我将其放入我的活动并运行项目时,当我单击菜单按钮时,它不显示任何内容。当我注释掉 onKeyDown() 时,应用程序工作正常。

原因是什么?谢谢

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);

    return (super.onCreateOptionsMenu(menu));
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.home:
            Log.i("Menu", "Home clicked.");
            return true;
        case R.id.social:
            Log.i("Menu", "Social Networks clicked.");
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    super.onKeyDown(keyCode, event);

    if(keyCode == KeyEvent.KEYCODE_HOME){
        Log.i("onKeyDown", "Home presed");
        int pid = android.os.Process.myPid(); 
        android.os.Process.killProcess(pid); 
    }

    if(keyCode == KeyEvent.KEYCODE_BACK){
        Log.i("onKeyDown", "back presed");
        int pid = android.os.Process.myPid(); 
        android.os.Process.killProcess(pid);
    }
    return true;
}

Today I amazed that why my menu doesn't show when i put onKeyDown() in my code!!!

in my project i have menu and i designed it with onCreateOptionsMenu() and onOptionsItemSelected() in regular way. The project doesn't have problem and works fine.

The problem is using onKeyDown(). when i put it into my activity and run the project, when i click on menu button, it doesn't show anything. while when i comment out onKeyDown() the app works fine.

What is the reason? Thank you

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);

    return (super.onCreateOptionsMenu(menu));
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.home:
            Log.i("Menu", "Home clicked.");
            return true;
        case R.id.social:
            Log.i("Menu", "Social Networks clicked.");
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    super.onKeyDown(keyCode, event);

    if(keyCode == KeyEvent.KEYCODE_HOME){
        Log.i("onKeyDown", "Home presed");
        int pid = android.os.Process.myPid(); 
        android.os.Process.killProcess(pid); 
    }

    if(keyCode == KeyEvent.KEYCODE_BACK){
        Log.i("onKeyDown", "back presed");
        int pid = android.os.Process.myPid(); 
        android.os.Process.killProcess(pid);
    }
    return true;
}

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

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

发布评论

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

评论(1

盛夏已如深秋| 2025-01-14 22:43:43

当您从 onKeyDown() 方法返回一个值时,它指示该事件是否已被处理。在您的情况下,无论您是否真正响应了该事件,总是返回 true 。该方法的正确版本应该是:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
        case KeyEvent.KEYCODE_HOME:
            //Handle the event
            return true;
        case KeyEvent.KEYCODE_BACK:
            //Handle the event
            return true;
    }

    return super.onKeyDown(keyCode, event);
}

另外,我应该说,在单击按钮时终止进程并不是 Android 中处理此类事情的方式,您应该管理您的活动。返回按钮默认结束 Activity,您应该保持原样,启动时重新启动 Activity 在 AndroidManifest.xml 中指定。

When you return a value from the onKeyDown() method, it indicates whether the event has been handled or not. In your case true is always returned regardless of whether you have really responded to the event or not. The correct version of the method should be:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
        case KeyEvent.KEYCODE_HOME:
            //Handle the event
            return true;
        case KeyEvent.KEYCODE_BACK:
            //Handle the event
            return true;
    }

    return super.onKeyDown(keyCode, event);
}

Also, I should say that killing the process on the button clicks is not how such things are handled in Android, you should manage your activity instead. Back button finishes activity by default, and you should leave this as that, restarting activity on launch is specified in AndroidManifest.xml.

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