Android学习历程-使用菜单(menu)资源
学习备份笔记用:
菜单XML:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:title="File"> <menu> <group android:id="@+id/noncheckable_group" android:checkableBehavior="none" > <item android:id="@+id/newFile" android:alphabeticShortcut="n" android:title="New"/> <item android:id="@+id/openFile" android:alphabeticShortcut="o" android:title="Open"/> <item android:id="@+id/saveFile" android:alphabeticShortcut="s" android:title="Save"/> </group> </menu> </item> <item android:title="Edit"> <menu> <group android:id="@+id/edit_group" android:checkableBehavior="single" > <item android:id="@+id/cut" android:title="Cut"/> <item android:id="@+id/copy" android:title="Copy"/> <item android:id="@+id/past" android:title="Past"/> </group> </menu> </item> <item android:title="Help"> <menu> <group android:id="@+id/help_group"> <item android:id="@+id/about" android:title="About"/> <item android:id="@+id/exit" android:title="Exit"/> </group> </menu> </item> </menu>
布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:text="测试菜单资源" android:id="@+id/menuTextView01" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>
Activity:
package com.example.testmenu; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; public class TestMenuActivity extends Activity { private MenuInflater mi; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.test_menu); mi = new MenuInflater(TestMenuActivity.this); } @Override public boolean onCreateOptionsMenu(Menu menu) { mi.inflate(R.menu.test_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.about: aboutAlert("本实例演示的是如何使用XML菜单资源来定义菜单!"); break; case R.id.exit: exitAlert("真的要退出吗?"); break; } return true; } //显示对话框 private void exitAlert(String msg){ //实例化AlertDialog。Builder AlertDialog.Builder builder = new AlertDialog.Builder(TestMenuActivity.this); builder.setMessage(msg).setCancelable(false).setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { return; } }); //创建对话框 AlertDialog alert = builder.create(); alert.show(); } //显示对话框 private void aboutAlert(String msg){ //实例化AlertDialog。Builder AlertDialog.Builder builder = new AlertDialog.Builder(TestMenuActivity.this); builder.setMessage(msg).setCancelable(false).setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); //创建对话框 AlertDialog alert = builder.create(); alert.show(); } }