android:有关于为应用程序创建菜单的教程吗?

发布于 2024-08-28 04:42:50 字数 123 浏览 10 评论 0原文

我需要一个非常简单的菜单,它可能只包含一个或两个项目:设置/选项,按下其中之一应该显示一些客户定义的参数(称为对话框),例如显示的结果数。有没有关于创建此类菜单的好的教程?我看过android中的“记事本”示例,它并没有真正的帮助。

I need a very simple menu which probably contains only one or two items: settings/options, where pressing one of them should show some customer defined parameters (is it called dialog), e.g., number of results shown. Is there any good tutorial on creating such kind of menus? I've looked at the "notepad" example in android, it doesn't really help.

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

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

发布评论

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

评论(1

对风讲故事 2024-09-04 04:42:50

根据您的要求,这些是“选项菜单”或“上下文菜单”,创建它们非常容易。以下是开发人员网站上页面的链接,解释了如何做菜单。

以下是选项菜单代码的基本示例,改编自我的游戏:

public boolean onCreateOptionsMenu(Menu menu){
    // Define your menu, giving each button a unique identifier numbers
    // (MENU_PAUSE, etc)
    // This is called only once, the first time the menu button is clicked
    menu.add(0, MENU_PAUSE, 0, "Pause").setIcon(android.R.drawable.ic_media_pause);         
    menu.add(0, MENU_RESUME, 0, "Resume").setIcon(android.R.drawable.ic_media_play);
    return true;
}


public boolean onPrepareOptionsMenu(Menu menu){
    // This is called every time the menu button is pressed. In my game, I
    // use this to show or hide the pause/resume buttons depending on the
    // current state
}


public boolean onOptionsItemSelected(MenuItem item){
    // and this is self explanatory
    boolean handled = false;

    switch (item.getItemId()){
    case MENU_PAUSE:
        pauseGame();
        handled = true;
        break;

    case MENU_RESUME:
        resumeGame();
        handled = true;
        break;
    }
    return handled;
}

编辑:有关 AlertDialogs 的一些详细信息,请参阅注释

Depending on what you're asking for, these are either "Options Menus" or "Context Menus", and creating them is very easy. Here's a link to the page on the Developers' Website explaining how to do menus.

Here's a basic example of code for options menus, adapted from my game:

public boolean onCreateOptionsMenu(Menu menu){
    // Define your menu, giving each button a unique identifier numbers
    // (MENU_PAUSE, etc)
    // This is called only once, the first time the menu button is clicked
    menu.add(0, MENU_PAUSE, 0, "Pause").setIcon(android.R.drawable.ic_media_pause);         
    menu.add(0, MENU_RESUME, 0, "Resume").setIcon(android.R.drawable.ic_media_play);
    return true;
}


public boolean onPrepareOptionsMenu(Menu menu){
    // This is called every time the menu button is pressed. In my game, I
    // use this to show or hide the pause/resume buttons depending on the
    // current state
}


public boolean onOptionsItemSelected(MenuItem item){
    // and this is self explanatory
    boolean handled = false;

    switch (item.getItemId()){
    case MENU_PAUSE:
        pauseGame();
        handled = true;
        break;

    case MENU_RESUME:
        resumeGame();
        handled = true;
        break;
    }
    return handled;
}

Edit: See the comments for some details on AlertDialogs

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