Java里optionsMenu.add_OptionsMenu

沈高峻
2023-12-01

完整代码

package com.wjq.menu;

import java.util.ArrayList;

import java.util.HashMap;

import android.app.Activity;

import android.app.AlertDialog;

import android.content.DialogInterface;

import android.content.DialogInterface.OnKeyListener;

import android.os.Bundle;

import android.view.KeyEvent;

import android.view.Menu;

import android.view.View;

import android.widget.AdapterView;

import android.widget.GridView;

import android.widget.SimpleAdapter;

import android.widget.AdapterView.OnItemClickListener;publicclassCustomizeMenu extends Activity {privateboolean isMore=false;//menu菜单翻页控制AlertDialog menuDialog;//menu菜单DialogGridView menuGrid;

View menuView;privatefinalintITEM_SEARCH=0;//搜索privatefinalintITEM_FILE_MANAGER=1;//文件管理privatefinalintITEM_DOWN_MANAGER=2;//下载管理privatefinalintITEM_FULLSCREEN=3;//全屏privatefinalintITEM_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={"自动横屏","笔选模式","阅读模式","浏览模式","快捷翻页","检查更新","检查网络","定时刷新","设置","帮助","关于","返回"};

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {//TODO Auto-generated method stubsuper.onCreate(savedInstanceState);

setContentView(R.layout.main);

menuView=View.inflate(this, R.layout.gridview_menu,null);//创建AlertDialogmenuDialog=newAlertDialog.Builder(this).create();

menuDialog.setView(menuView);

menuDialog.setOnKeyListener(newOnKeyListener() {publicboolean onKey(DialogInterface dialog,intkeyCode,

KeyEventevent) {if(keyCode==KeyEvent.KEYCODE_MENU)//监听按键dialog.dismiss();returnfalse;

}

});

menuGrid=(GridView) menuView.findViewById(R.id.gridview);

menuGrid.setAdapter(getMenuAdapter(menu_name_array, menu_image_array));/** 监听menu选项 **/menuGrid.setOnItemClickListener(newOnItemClickListener() {publicvoidonItemClick(AdapterView>arg0, View arg1,intarg2,longarg3) {switch(arg2) {caseITEM_SEARCH://搜索break;caseITEM_FILE_MANAGER://文件管理break;caseITEM_DOWN_MANAGER://下载管理break;caseITEM_FULLSCREEN://全屏break;caseITEM_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();//更新menumenuGrid.setSelection(ITEM_MORE);break;

}

}

});

}

@Overridepublicboolean onCreateOptionsMenu(Menu menu) {

menu.add("menu");//必须创建一项returnsuper.onCreateOptionsMenu(menu);

}privateSimpleAdapter getMenuAdapter(String[] menuNameArray,int[] imageResourceArray) {

ArrayList>data=newArrayList>();for(inti=0; i

HashMapmap=newHashMap();

map.put("itemImage", imageResourceArray);

map.put("itemText", menuNameArray);

data.add(map);

}

SimpleAdapter simperAdapter=newSimpleAdapter(this, data,

R.layout.item_menu,newString[] {"itemImage","itemText"},newint[] { R.id.item_image, R.id.item_text });returnsimperAdapter;

}

@Overridepublicboolean onMenuOpened(intfeatureId, Menu menu) {if(menuDialog==null) {

menuDialog=newAlertDialog.Builder(this).setView(menuView).show();

}else{

menuDialog.show();

}returnfalse;//返回为true 则显示系统menu}

}

 类似资料: