optionMenu总结

一、系统默认optionMenu:

(1)在资源目录下,增加新目录menu,然后增加文件meetings_context_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:id="@+id/deleteTeam" android:title="打电话"
         />
   <item android:id="@+id/send" android:title="发短信"
         />
</menu>
(2)在需要调用的页面内,添加:

/**
  * 增加optionMenu形式的菜单,这个菜单在资源meetings_context_menu中定义
  */
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
           // Inflate the currently selected menu XML resource.
           MenuInflater inflater = getMenuInflater();
           inflater.inflate(R.menu.meetings_context_menu, menu);      
           // Hold on to this
        return true;
       } 

(3)处理菜单:

   /* optionMenu形式的菜单处理选择项 */
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.send:
     Intent DialIntent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:111111"));
     DialIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
    /** Finally start the Activity */
        startActivity(DialIntent);
            return true;
        case R.id.deleteTeam:
            //quit();
            return true;
        }
        return false;
    }  

 

二、自定制OptionMenu

(1)框架结构gridview_menu.xml:

<?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"
    android:background="@drawable/bgdialog"
    >
<GridView
         android:id="@+id/gridview"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:numColumns="3"
         android:verticalSpacing="8dip"
         android:horizontalSpacing="8dip"
         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:textColor="#FFFFFF"
        android:layout_centerHorizontal="true" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:textSize="16px" android:text="选项"></TextView>
</RelativeLayout>

(3)基本变量:

 private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
 String methodName = "getConnect";
 int SelectItem = 0;
 
    private boolean isMore = false;// menu菜单翻页控制
    AlertDialog menuDialog;// menu菜单Dialog
    GridView menuGrid;
    View menuView;
   
    private final int ITEM_SEARCH = 0;// 注册
    private final int ITEM_FILE_MANAGER = 1;// 登陆
    private final int ITEM_DOWN_MANAGER = 2;// 高级搜索
    private final int ITEM_FULLSCREEN = 3;// 淘宝团
    private final int ITEM_MORE = 4;// 退出

 (4)绑定操作:

 private void initCustomerMenu() {
  menuView = View.inflate(this, R.menu.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) {
             Intent intent = new Intent();
                switch (arg2) {
                case ITEM_SEARCH:
                
                 USTC.Const.urlhtml=USTC.Const.regurl;
                 intent.setClass(Main.this, WebMain.class);
           startActivity(intent);
                    break;
                case ITEM_FILE_MANAGER:
                 USTC.Const.urlhtml=USTC.Const.loginurl;
                 intent.setClass(Main.this, WebMain.class);
           startActivity(intent);
                    break;
                case ITEM_DOWN_MANAGER:
                 USTC.Const.urlhtml=USTC.Const.Searchurl;
                 intent.setClass(Main.this, WebMain.class);
           startActivity(intent);
                    break;
                case ITEM_FULLSCREEN:// 全屏
                 USTC.Const.urlhtml=USTC.Const.Tuanurl;
                 intent.setClass(Main.this, WebMain.class);
           startActivity(intent);
                    break;
                case ITEM_MORE:// 翻页
                 System.exit(0);
                    break;
                }
               
               
            }
        });
 }
 
    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
     }

   public boolean onCreateOptionsMenu(Menu menu) {
      menu.add("menu");// 必须创建一项
      return super.onCreateOptionsMenu(menu);
     
           // Inflate the currently selected menu XML resource.
          // MenuInflater inflater = getMenuInflater();
          // inflater.inflate(R.menu.meeting_context_menu, menu);      
           // Hold on to this
   
       }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值