项目笔记2:系统菜单OptionsMenu的用法

今天老大分配的任务是完成系统菜单,刚开始我木呀,什么都不懂,还好提醒了我,是OptionsMenu,然后就上网找找,有点点小小的成就感

今天就两个内容,一个是系统自己自带的(也是我采用的) 二是自定义的,我也照着写了,但是出了问题,为啥我目前没明白,还是先贴来吧

一、系统的

    item:每一个item对应一项菜单。

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="wrap_content"  
  5.   android:layout_height="wrap_content"  
  6.   android:orientation ="vertical">  
  7.     
  8.   <TextView android:layout_width="wrap_content"  
  9.             android:layout_height="wrap_content"  
  10.             android:text="请点击Menu键显示选项菜单"  
  11.             android:id="@+id/TextView02"/>  
  12. </LinearLayout>  

OptionMenuDemo.java

  1. package com.option;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.content.DialogInterface;  
  6. import android.content.Intent;  
  7. import android.os.Bundle;  
  8. import android.view.Menu;  
  9. import android.view.MenuItem;  
  10. import android.widget.Toast;  
  11.   
  12. public class OptionsMenuDemo extends Activity {  
  13.     /** Called when the activity is first created. */  
  14. /*  final static int ONE = Menu.FIRST; 
  15.     final static int TWO = Menu.FIRST + 1;*/  
  16.   
  17.     // final static int THREE = Menu.FIRST+2;  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         // setContentView(R.layout.main);  
  22.         setContentView(R.layout.main1);  
  23.   
  24.     }  
  25.   
  26.     @Override  
  27.     public boolean onCreateOptionsMenu(Menu menu) {  
  28.         // TODO Auto-generated method stub  
  29.   
  30.         menu.add(Menu.NONE, Menu.FIRST + 11"首页").setIcon(R.drawable.home);  
  31.         menu.add(Menu.NONE, Menu.FIRST + 22"新建").setIcon(R.drawable.sys_begin);  
  32.         menu.add(Menu.NONE, Menu.FIRST + 33"查询").setIcon(R.drawable.sys_query);  
  33.         menu.add(Menu.NONE, Menu.FIRST + 44"新闻").setIcon(R.drawable.sys_news);  
  34.         menu.add(Menu.NONE, Menu.FIRST + 55"退出").setIcon(R.drawable.menu_exit);  
  35.         return true;  
  36.     }  
  37.   
  38.     @Override  
  39.     public boolean onOptionsItemSelected(MenuItem item) {  
  40.           
  41.         super.onOptionsItemSelected(item);  
  42.         Intent intent = new Intent();  
  43.           
  44.         switch (item.getItemId()) {  
  45.         case Menu.FIRST + 1:  
  46.             /*Toast.makeText(this, "首页", Toast.LENGTH_LONG).show(); 
  47.             break;*/  
  48.             intent.setClass(OptionsMenuDemo.this, testActivity1.class);  
  49.             startActivity(intent);  
  50.             break;  
  51.         case Menu.FIRST + 2:  
  52.             Toast.makeText(this"新建", Toast.LENGTH_LONG).show();  
  53.             break;  
  54.   
  55.         case Menu.FIRST + 3:  
  56.             Toast.makeText(this"查询", Toast.LENGTH_LONG).show();  
  57.             break;  
  58.   
  59.         case Menu.FIRST + 4:  
  60.             Toast.makeText(this"新闻", Toast.LENGTH_LONG).show();  
  61.             break;  
  62.   
  63.         case Menu.FIRST + 5:  
  64.             AlertDialog alertDialog = new AlertDialog.Builder(this)  
  65.                     .setTitle("退出系统")  
  66.                     .setMessage("是否退出本系统?")  
  67.                     .setPositiveButton("确定",  
  68.                             new DialogInterface.OnClickListener() {  
  69.                                 @Override  
  70.                                 public void onClick(DialogInterface dialog,  
  71.                                         int which) {  
  72.                                     // TODO Auto-generated method stub  
  73.                                     System.exit(1);  
  74.                                 }  
  75.                             })  
  76.                     .setNegativeButton("取消",  
  77.                             new DialogInterface.OnClickListener() {  
  78.   
  79.                                 @Override  
  80.                                 public void onClick(DialogInterface dialog,  
  81.                                         int which) {  
  82.                                     // TODO Auto-generated method stub  
  83.                                     return;  
  84.                                 }  
  85.                             }).create();  
  86.             alertDialog.show();  
  87.             break;  
  88.         }  
  89.         return false;  
  90.   
  91.     }  
  92.   
  93.     /*public void onOptionsMenuClosed(Menu menu) { 
  94.         Toast.makeText(this, "你关闭了选项菜单", Toast.LENGTH_SHORT).show(); 
  95.     }*/  
  96.   
  97.     public boolean onPrepareOptionMenu(Menu menu) {  
  98.         Toast.makeText(this"选项菜单显示之前onPrepareOptionsMenu方法会被调用",  
  99.                 Toast.LENGTH_LONG).show();  
  100.         return true;  
  101.     }  
  102.   
  103. }  


效果:

单击退出出现

PS:关于项目,有很多的基类,所以,将这里的两个方法复制到基类中去即onCreateOptionsMenu

onOptionsItemSelected,如果有些方法在别的页面都一样,就写在基类中,否则,就写一个抽象方法,然后到具体的页面再去实现抽象方法,子类继承抽象类的方法,要实现抽象类中所有的抽象方法,这个明天尽量争取实现了~~后期也会有笔记~


 

 一些解释:
   android:icon:用于指定菜单显示的图标。
  android:title:菜单的标题,显示在图标下面。
  android:alphabeticShortcut:菜单选择的快捷键。
  关于Menu的更多属性请查看SDK上的 Menu Resource   可以查看另一种方式:http://www.cnblogs.com/keyindex/articles/1813764.html

二、别人的自定义的

  1. 1.gridview_menu.xml (首先自定义菜单界面,我是GridView来包含菜单项,4列3行)  
  2. <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"><GridView android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:numColumns="4" android:verticalSpacing="10dip" android:horizontalSpacing="10dip" android:stretchMode="columnWidth" android:gravity="center"/></LinearLayout>  
  3. 2.item_menu.xml菜单项的现实样式,一个图标和一个文字。  
  4. <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/RelativeLayout_Item" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingBottom="5dip"><ImageView android:id="@+id/item_image" android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView><TextView android:layout_below="@id/item_image" android:id="@+id/item_text" android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选项"></TextView></RelativeLayout>  
  1. <p>3<span style="font-family:宋体;">、定义,主类</span></p><p>private boolean isMore= false;// menu<span style="font-family:宋体;">菜单翻页控制</span>  
  2.  AlertDialog menuDialog;// menu<span style="font-family:宋体;">菜单</span><span style="font-family:Courier New;">Dialog</span>  
  3.  GridView menuGrid;  
  4.  View menuView;  
  5.   
  6. private finalint ITEM_SEARCH= 0;// 搜索  
  7.  private finalint ITEM_FILE_MANAGER= 1;// 文件管理  
  8.  private finalint ITEM_DOWN_MANAGER= 2;// 下载管理  
  9.  private finalint ITEM_FULLSCREEN= 3;// 全屏  
  10.  private finalint ITEM_MORE= 11;// 菜单  
  11.   
  12.   
  13. /** <span style="font-family:宋体;">菜单图片 </span><span style="font-family:Courier New;">**/</span>  
  14. int[] menu_image_array= { R.drawable.menu_search,  
  15.  R.drawable.menu_filemanager, R.drawable.menu_downmanager,  
  16.  R.drawable.menu_fullscreen, R.drawable.menu_inputurl,  
  17.  R.drawable.menu_bookmark, R.drawable.menu_bookmark_sync_import,  
  18.  R.drawable.menu_sharepage, R.drawable.menu_quit,  
  19.  R.drawable.menu_nightmode, R.drawable.menu_refresh,  
  20.  R.drawable.menu_more };  
  21. /** <span style="font-family:宋体;">菜单文字 </span><span style="font-family:Courier New;">**/</span>  
  22.  String[] menu_name_array= {"<span style="font-family:宋体;">搜索</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">文件管理</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">下载管理</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">全屏</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">网址</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">书签</span><span style="font-family:Courier New;">"</span>,  
  23. "<span style="font-family:宋体;">加入书签</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">分享页面</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">退出</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">夜间模式</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">刷新</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">更多</span><span style="font-family:Courier New;">"</span> };  
  24. /** <span style="font-family:宋体;">菜单图片</span><span style="font-family:Courier New;">2 **/</span>  
  25. int[] menu_image_array2= { R.drawable.menu_auto_landscape,  
  26.  R.drawable.menu_penselectmodel, R.drawable.menu_page_attr,  
  27.  R.drawable.menu_novel_mode, R.drawable.menu_page_updown,  
  28.  R.drawable.menu_checkupdate, R.drawable.menu_checknet,  
  29.  R.drawable.menu_refreshtimer, R.drawable.menu_syssettings,  
  30.  R.drawable.menu_help, R.drawable.menu_about, R.drawable.menu_return };  
  31. /** <span style="font-family:宋体;">菜单文字</span><span style="font-family:Courier New;">2 **/</span>  
  32.  String[] menu_name_array2= {"<span style="font-family:宋体;">自动横屏</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">笔选模式</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">阅读模式</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">浏览模式</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">快捷翻页</span><span style="font-family:Courier New;">"</span>,  
  33. "<span style="font-family:宋体;">检查更新</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">检查网络</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">定时刷新</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">设置</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">帮助</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">关于</span><span style="font-family:Courier New;">"</span>,"<span style="font-family:宋体;">返回</span><span style="font-family:Courier New;">"</span> };</p><p>4<span style="font-family:宋体;">、</span>如果第一次打开则设置视图,否则直接显示<span style="font-family:Times New Roman;">menuDialog</span><span style="font-family:宋体;">视图。</span></p><p>@Override  
  34. public boolean onMenuOpened(int featureId, Menu menu) {  
  35. if (menuDialog== null) {  
  36.  menuDialog= new AlertDialog.Builder(this).setView(menuView).show();  
  37.  }else {  
  38.  menuDialog.show();  
  39.  }  
  40. return false;// 返 回为<span style="font-family:Courier New;">true </span>< span style="font-family:宋体;">则显示系统</span><span style="font- family:Courier New;">menu</span>  
  41.  }</p><p>1、getMenuAdapter方法 为菜单添加菜单项</p><p>private SimpleAdapter getMenuAdapter(String[] menuNameArray,  
  42. int[] imageResourceArray) {  
  43.  ArrayList<HashMap<String, Object>> data= new ArrayList<HashMap<String, Object>>();  
  44. for (int i= 0; i< menuNameArray.length; i++) {  
  45.  HashMap<String, Object> map= new HashMap<String, Object>();  
  46.  map.put("itemImage", imageResourceArray[i]);  
  47.  map.put("itemText", menuNameArray[i]);  
  48.  data.add(map);  
  49.  }  
  50.  SimpleAdapter simperAdapter= new SimpleAdapter(this, data,  
  51.  R.layout.item_menu,new String[] {"itemImage","itemText" },  
  52. new int[] { R.id.item_image, R.id.item_text });  
  53. return simperAdapter;  
  54.  }</p><p>6<span style="font-family:宋体;">、</span></p><p>Override  
  55. public boolean onCreateOptionsMenu(Menu menu) {  
  56.  menu.add("menu");// 必须创建一项  
  57.  return super.onCreateOptionsMenu(menu);  
  58. </p><p>7、onCreate<span style="font-family:宋体;">方法重写</span></p><p>@Override  
  59. protected void onCreate(Bundle savedInstanceState) {  
  60. // TODO Auto-generated method stub  
  61.  super.onCreate(savedInstanceState);  
  62.   
  63.  setContentView(R.layout.main);  
  64.   
  65.  menuView= View.inflate(this, R.layout.gridview_menu,null);  
  66. // 创建<span style="font-family:Courier New;">AlertDialog</span>  
  67.  menuDialog= new AlertDialog.Builder(this).create();  
  68.  menuDialog.setView(menuView);  
  69.  menuDialog.setOnKeyListener(new OnKeyListener() {  
  70. public boolean onKey(DialogInterface dialog,int keyCode,  
  71.  KeyEventevent) {  
  72. if (keyCode== KeyEvent.KEYCODE_MENU)// 监听按键  
  73.  dialog.dismiss();  
  74. return false;  
  75.  }  
  76.  });  
  77.   
  78.  menuGrid= (GridView) menuView.findViewById(R.id.gridview);  
  79.  menuGrid.setAdapter(getMenuAdapter(menu_name_array, menu_image_array));  
  80. /** <span style="font- family:宋体;">监听</span><span style="font-family:Courier New;"& gt;menu</span><span style="font-family:宋体;">选项 </span& gt;<span style="font-family:Courier New;">**/</span>  
  81.  menuGrid.setOnItemClickListener(new OnItemClickListener() {  
  82. public void onItemClick(AdapterView<?> arg0, View arg1,int arg2,  
  83. long arg3) {  
  84. switch (arg2) {  
  85. case ITEM_SEARCH:// 搜索  
  86.   
  87. break;  
  88. case ITEM_FILE_MANAGER:// 文件管理  
  89.   
  90. break;  
  91. case ITEM_DOWN_MANAGER:// 下载管理  
  92.   
  93. break;  
  94. case ITEM_FULLSCREEN:// 全屏  
  95.   
  96. break;  
  97. case ITEM_MORE:// 翻页  
  98.  if (isMore) {  
  99.  menuGrid.setAdapter(getMenuAdapter(menu_name_array2,  
  100.  menu_image_array2));  
  101.  isMore= false;  
  102.  }else {// 首页  
  103.  menuGrid.setAdapter(getMenuAdapter(menu_name_array,  
  104.  menu_image_array));  
  105.  isMore= true;  
  106.  }  
  107.  menuGrid.invalidate();// 更新<span style="font-family:Courier New;">menu</span>  
  108.  menuGrid.setSelection(ITEM_MORE);  
  109. break;  
  110.  }  
  111.   
  112.   
  113.  }  
  114.  });  
  115.  }</p>  

参考的是http://www.cnblogs.com/keyindex/articles/1813764.html 是这个网站的

原文地址:https://www.cnblogs.com/LiaoHao/p/3334208.html