2010-04-07 27 views
0

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

回答

2

取决于你问的是什么,这些是不是“选项菜单”或“上下文菜单”,并创建他们是很容易的。这里有一个指向Developers' Website页面的链接,解释如何做菜单。

这里的供选菜单代码,改编自我的比赛一个基本的例子:

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

+0

的一点是一些细节的意见,我想另一个对话框/菜单在用户按下设置选项后弹出。这样做的标准方式是什么? – Yang 2010-04-07 21:57:47

+1

啊,那么你想在用户按下菜单中的某些东西后创建一个'AlertDialog'。 AlertDialogs可以有简单的文本和按钮,带复选框的列表或类似的东西。对于一个简单的是/否AlertDialog的例子,看看这个答案(http://stackoverflow.com/questions/2478517/how-to-display-a-yes-no-dialog-box-in-android/ 2478662#2478662)。如果您需要列表,请阅读Android开发人员的对话框资源(http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog)。 – 2010-04-07 22:49:44

相关问题