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

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

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

一、系统的

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

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

OptionMenuDemo.java

package com.option;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class OptionsMenuDemo extends Activity {
	/** Called when the activity is first created. */
/*	final static int ONE = Menu.FIRST;
	final static int TWO = Menu.FIRST + 1;*/

	// final static int THREE = Menu.FIRST+2;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.main);
		setContentView(R.layout.main1);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub

		menu.add(Menu.NONE, Menu.FIRST + 1, 1, "首页").setIcon(R.drawable.home);
		menu.add(Menu.NONE, Menu.FIRST + 2, 2, "新建").setIcon(R.drawable.sys_begin);
		menu.add(Menu.NONE, Menu.FIRST + 3, 3, "查询").setIcon(R.drawable.sys_query);
		menu.add(Menu.NONE, Menu.FIRST + 4, 4, "新闻").setIcon(R.drawable.sys_news);
		menu.add(Menu.NONE, Menu.FIRST + 5, 5, "退出").setIcon(R.drawable.menu_exit);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		super.onOptionsItemSelected(item);
		Intent intent = new Intent();
		
		switch (item.getItemId()) {
		case Menu.FIRST + 1:
			/*Toast.makeText(this, "首页", Toast.LENGTH_LONG).show();
			break;*/
			intent.setClass(OptionsMenuDemo.this, testActivity1.class);
			startActivity(intent);
			break;
		case Menu.FIRST + 2:
			Toast.makeText(this, "新建", Toast.LENGTH_LONG).show();
			break;

		case Menu.FIRST + 3:
			Toast.makeText(this, "查询", Toast.LENGTH_LONG).show();
			break;

		case Menu.FIRST + 4:
			Toast.makeText(this, "新闻", Toast.LENGTH_LONG).show();
			break;

		case Menu.FIRST + 5:
			AlertDialog alertDialog = new AlertDialog.Builder(this)
					.setTitle("退出系统")
					.setMessage("是否退出本系统?")
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub
									System.exit(1);
								}
							})
					.setNegativeButton("取消",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub
									return;
								}
							}).create();
			alertDialog.show();
			break;
		}
		return false;

	}

	/*public void onOptionsMenuClosed(Menu menu) {
		Toast.makeText(this, "你关闭了选项菜单", Toast.LENGTH_SHORT).show();
	}*/

	public boolean onPrepareOptionMenu(Menu menu) {
		Toast.makeText(this, "选项菜单显示之前onPrepareOptionsMenu方法会被调用",
				Toast.LENGTH_LONG).show();
		return true;
	}

}


效果:

单击退出出现

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

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


 

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

二、别人的自定义的

1.gridview_menu.xml (首先自定义菜单界面,我是GridView来包含菜单项,4列3行)
<?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>
2.item_menu.xml菜单项的现实样式,一个图标和一个文字。
<?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>

3、定义,主类

private boolean isMore= false;// menu菜单翻页控制  AlertDialog menuDialog;// menu菜单Dialog  GridView menuGrid;  View menuView; private finalint ITEM_SEARCH= 0;// 搜索  private finalint ITEM_FILE_MANAGER= 1;// 文件管理  private finalint ITEM_DOWN_MANAGER= 2;// 下载管理  private finalint ITEM_FULLSCREEN= 3;// 全屏  private finalint ITEM_MORE= 11;// 菜单 /** 菜单图片 **/ int[] menu_image_array= { R.drawable.menu_search,  R.drawable.menu_filemanager, R.drawable.menu_downmanager,  R.drawable.menu_fullscreen, R.drawable.menu_inputurl,  R.drawable.menu_bookmark, R.drawable.menu_bookmark_sync_import,  R.drawable.menu_sharepage, R.drawable.menu_quit,  R.drawable.menu_nightmode, R.drawable.menu_refresh,  R.drawable.menu_more }; /** 菜单文字 **/  String[] menu_name_array= {"搜索","文件管理","下载管理","全屏","网址","书签", "加入书签","分享页面","退出","夜间模式","刷新","更多" }; /** 菜单图片2 **/ int[] menu_image_array2= { R.drawable.menu_auto_landscape,  R.drawable.menu_penselectmodel, R.drawable.menu_page_attr,  R.drawable.menu_novel_mode, R.drawable.menu_page_updown,  R.drawable.menu_checkupdate, R.drawable.menu_checknet,  R.drawable.menu_refreshtimer, R.drawable.menu_syssettings,  R.drawable.menu_help, R.drawable.menu_about, R.drawable.menu_return }; /** 菜单文字2 **/  String[] menu_name_array2= {"自动横屏","笔选模式","阅读模式","浏览模式","快捷翻页", "检查更新","检查网络","定时刷新","设置","帮助","关于","返回" };

4如果第一次打开则设置视图,否则直接显示menuDialog视图。

@Override public boolean onMenuOpened(int featureId, Menu menu) { if (menuDialog== null) {  menuDialog= new AlertDialog.Builder(this).setView(menuView).show();  }else {  menuDialog.show();  } return false;// 返回为true 则显示系统menu  }

1、getMenuAdapter方法 为菜单添加菜单项

private SimpleAdapter getMenuAdapter(String[] menuNameArray, int[] imageResourceArray) {  ArrayList<HashMap<String, Object>> data= new ArrayList<HashMap<String, Object>>(); for (int i= 0; i< menuNameArray.length; i++) {  HashMap<String, Object> map= new HashMap<String, Object>();  map.put("itemImage", imageResourceArray[i]);  map.put("itemText", menuNameArray[i]);  data.add(map);  }  SimpleAdapter simperAdapter= new SimpleAdapter(this, data,  R.layout.item_menu,new String[] {"itemImage","itemText" }, new int[] { R.id.item_image, R.id.item_text }); return simperAdapter;  }

6

Override public boolean onCreateOptionsMenu(Menu menu) {  menu.add("menu");// 必须创建一项  return super.onCreateOptionsMenu(menu);

7、onCreate方法重写

@Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub  super.onCreate(savedInstanceState);  setContentView(R.layout.main);  menuView= View.inflate(this, R.layout.gridview_menu,null); // 创建AlertDialog  menuDialog= new AlertDialog.Builder(this).create();  menuDialog.setView(menuView);  menuDialog.setOnKeyListener(new OnKeyListener() { public boolean onKey(DialogInterface dialog,int keyCode,  KeyEventevent) { if (keyCode== KeyEvent.KEYCODE_MENU)// 监听按键  dialog.dismiss(); return false;  }  });  menuGrid= (GridView) menuView.findViewById(R.id.gridview);  menuGrid.setAdapter(getMenuAdapter(menu_name_array, menu_image_array)); /** 监听menu选项 **/  menuGrid.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) { switch (arg2) { case ITEM_SEARCH:// 搜索 break; case ITEM_FILE_MANAGER:// 文件管理 break; case ITEM_DOWN_MANAGER:// 下载管理 break; case ITEM_FULLSCREEN:// 全屏 break; case ITEM_MORE:// 翻页  if (isMore) {  menuGrid.setAdapter(getMenuAdapter(menu_name_array2,  menu_image_array2));  isMore= false;  }else {// 首页  menuGrid.setAdapter(getMenuAdapter(menu_name_array,  menu_image_array));  isMore= true;  }  menuGrid.invalidate();// 更新menu  menuGrid.setSelection(ITEM_MORE); break;  }  }  });  }

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值