0

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

4

1 回答 1

2

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

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

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

于 2010-04-07T17:13:48.533 回答