我正在尝试让 Android 的菜单正常工作

发布于 2024-12-13 06:07:25 字数 668 浏览 2 评论 0原文

主要活动

public boolean onCreateOptionsMenu(Menu menu)
{
    getMenuInflater().inflate(R.menu.menumenu, menu);
    return true;
}

public void myClickHandler(View v) 
{ 
    switch(v.getId()) 
    { 
         case R.id.resetscoreboard:
             scoreboardreset();
             topText.setText("Scoreboard Has Been Reset!!");

    }
}

menumenu.xml

<item
    android:id="@+id/resetscoreboard"
    android:title="Reset Scoreboard"
    android:orderInCategory="1"
    android:onClick="myClickHandler">

</item>

你能告诉我为什么当我拉出菜单并单击它时它没有执行任何操作吗?

非常感谢专家@堆栈溢出!

Main Activity

public boolean onCreateOptionsMenu(Menu menu)
{
    getMenuInflater().inflate(R.menu.menumenu, menu);
    return true;
}

public void myClickHandler(View v) 
{ 
    switch(v.getId()) 
    { 
         case R.id.resetscoreboard:
             scoreboardreset();
             topText.setText("Scoreboard Has Been Reset!!");

    }
}

menumenu.xml

<item
    android:id="@+id/resetscoreboard"
    android:title="Reset Scoreboard"
    android:orderInCategory="1"
    android:onClick="myClickHandler">

</item>

Could you tell me why it does not do anything when I pull up the menu and click on it?

Thanks a lot, experts @ stack overflow!!

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

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

发布评论

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

评论(2

缘字诀 2024-12-20 06:07:25

您不需要定义点击处理程序,而是需要实现:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {

    case R.id.resetscoreboard:
       scoreboardreset();
       topText.setText("Scoreboard Has Been Reset!!");
       break;

    default:
        break;
    }

    return true;
}

通过 this 示例来了解 Android 中菜单的具体实现。

Instead of defining click Handler, you need to implement:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {

    case R.id.resetscoreboard:
       scoreboardreset();
       topText.setText("Scoreboard Has Been Reset!!");
       break;

    default:
        break;
    }

    return true;
}

Go through this example to get exact idea for the implementation of Menu in Android.

假面具 2024-12-20 06:07:25

您不应在此处使用 android:onClick 属性。

相反,请重写方法 onOptionsItemSelected

You should not use android:onClick attribute here.

Instead, override the method onOptionsItemSelected.

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