Android布局類型資源(三)---菜單

來源:互聯網
上載者:User


<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:icon="@drawable/ic_launcher" android:title="File"> <menu> <group android:id="@+id/noncheckable_group" android:checkableBehavior="none"> <item android:id="@+id/newFile" android:title="New" android:alphabeticShortcut="n"> </item> <item android:id="@+id/openFile" android:title="Open" android:alphabeticShortcut="o"> </item> <item android:id="@+id/saveFile" android:title="Save" android:alphabeticShortcut="s"> </item> </group> </menu> </item> <item android:title="Edit" android:icon="@drawable/ic_launcher"> <menu> <group android:id="@+id/edit_group" android:checkableBehavior="single"> <item android:id="@+id/cut" android:title="Cut"> </item> <item android:id="@+id/past" android:title="Past"> </item> </group> </menu></item><item android:title="Help" android:icon="@drawable/ic_launcher"> <menu> <group android:id="@+id/help_group"> <item android:id="@+id/about" android:title="About"> </item> <item android:id="@+id/exit" android:title="Exit"> </item> </group> </menu> </item></menu>

public class Test_Menu extends Activity{private MenuInflater mi;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.test_menu);mi = new MenuInflater(this);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {mi.inflate(R.menu.menu, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {switch(item.getItemId()){case R.id.about:aboutAlert("本執行個體示範的是如何使用Xml菜單資源來定義菜單!");break;case R.id.exit:exitAlert("真的要退出嗎?");break;}return super.onOptionsItemSelected(item);}private void exitAlert(String msg){//執行個體化AlertDialog.BuilderAlertDialog.Builder builder = new AlertDialog.Builder(this);//設定顯示資訊builder.setMessage(msg).setCancelable(false)//確定按鈕.setPositiveButton("確定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// 結束Activityfinish();}//取消按鈕}).setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {return;}});//建立對話方塊AlertDialog alert = builder.create();//顯示對話方塊alert.show();}private void aboutAlert(String msg){//執行個體化AlertDialog.BuilderAlertDialog.Builder builder = new AlertDialog.Builder(this);//設定顯示資訊builder.setMessage(msg).setCancelable(false)//確定按鈕.setPositiveButton("確定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {}});//建立對話方塊AlertDialog alert = builder.create();//顯示對話方塊alert.show();}}

public class Option_Menu extends Activity {//功能表項目ID常量private static final int Item_Start = Menu.FIRST;private static final int Item_Exit = Menu.FIRST+1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.option_menu);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {menu.add(0, Item_Start, 0, "開始");menu.add(0, Item_Exit, 0, "退出");return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {switch(item.getItemId()){//功能表項目1被選擇case Item_Start://這隻Activity標題setTitle("開始遊戲");break;//功能表項目1被選擇case Item_Exit:setTitle("退出!");break;}return true;}}

出處:http://blog.csdn.net/cl05300629/article/details/17717573 作者:佇望碧落

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.