package com.wxz.menu;
import java.util.ArrayList;
import java.util.HashMap;
import com.wxz.menu.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
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.Button;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class CustomizeMenu extends Activity {
private Button btnCustomizeMenu;
private Button btnDefaultMenu;
private TextView tvTip;
private Context mContext;
private boolean isMore = true;// menu菜单翻页控制
AlertDialog menuDialog;// menu菜单Dialog
GridView menuGrid;
View menuView;
// 菜单图片
int[] menu_image_array = { R.drawable.menu_search,
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_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 = { "自动横屏", "检查更新", "检查网络", "定时刷新", "设置", "帮助",
"关于", "返回" };
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContext = CustomizeMenu.this;
this.setTitle("Android 自定义菜单");
btnCustomizeMenu = (Button) findViewById(R.id.btnCustomizeMenu);
btnCustomizeMenu.setVisibility(View.GONE);
btnDefaultMenu = (Button) findViewById(R.id.btnDefaultMenu);
btnDefaultMenu.setVisibility(View.VISIBLE);
btnDefaultMenu.setOnClickListener(new ClickEvent());
tvTip = (TextView) findViewById(R.id.tvTip);
tvTip.setText("请按Menu键(F2)显示自定义菜单");
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,
KeyEvent event) {
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 0:// 搜索,自动横屏
if (isMore)
Toast.makeText(mContext, "点击了搜索", Toast.LENGTH_SHORT).show();
else
Toast.makeText(mContext, "点击了自动横屏", Toast.LENGTH_SHORT).show();
break;
case 1:// 书签,检查更新
if (isMore)
Toast.makeText(mContext, "点击了书签", Toast.LENGTH_SHORT).show();
else
Toast.makeText(mContext, "点击了检查更新", Toast.LENGTH_SHORT).show();
break;
case 2:// 加入书签,检查网络
if (isMore)
Toast.makeText(mContext, "点击了加入书签", Toast.LENGTH_SHORT).show();
else
Toast.makeText(mContext, "点击了检查网络", Toast.LENGTH_SHORT).show();
break;
case 3:// 分享页面,定时刷新
if (isMore)
Toast.makeText(mContext, "点击了分享页面", Toast.LENGTH_SHORT).show();
else
Toast.makeText(mContext, "点击了定时刷新", Toast.LENGTH_SHORT).show();
break;
case 7:// 更多
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(7);
break;
default://退出, 夜间模式,刷新,设置,帮助,关于
Toast.makeText(mContext, "点击了其它菜单项", Toast.LENGTH_SHORT).show();
break;
}
}
});
}
class ClickEvent implements View.OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == btnDefaultMenu) {
Intent it = new Intent();
it.setClass(CustomizeMenu.this, DefaultMenu.class);
startActivity(it);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("menu");// 必须创建一项
return super.onCreateOptionsMenu(menu);
}
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;
}
@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
- 2
- 3
- 4
前往页