Onelong

分享知识,与你一起进步......
RSS icon Home icon
  • android 实现上下文菜单的方法

    post by onelong / 2010-5-29 19:59 Saturday [android]

    1。调用registerForContextMenu()方法注册下上文菜单
    2。重新实现public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo)方法创建上下文菜单

    @Override
        public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
            super.onCreateContextMenu(menu, v, menuInfo);
            menu.add(0, C_MENU_NEW, 0, "新建");
            menu.add(0, C_MENU_OPEN, 0, "打开");
        }
    3。重新实现public boolean onContextItemSelected(MenuItem item)处理回调
      @Override
        public boolean onContextItemSelected(MenuItem item) {
          super.onContextItemSelected(item);     
          switch (item.getItemId()) {
          case C_MENU_NEW:
              //做新建的那些事。
              break;
          case C_MENU_OPEN:
              //做打开的那些事。
              break;
          }
          return false;

    阅读全文>>

  • android 创建子菜单的方法

    post by onelong / 2010-5-29 19:44 Saturday [android]

     @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                            //重写onCreateOptionsMenu(Menu menu)方法实现创建选项菜单
            SubMenu sub=menu.addSubMenu("搜索");//添加子菜单
            sub.setIcon(android.R.drawable.ic_menu_search);
            sub.add(0, MENU_LOCAL, 0, "本地");
            sub.add(0, MENU_INTERNET, 0, "网络");
            return true;
        }
     public boolean onOptionsItemSelected(MenuItem item) {
        //重写onOptionsItemSelected(MenuItem item)方法实现事件监听
            super.onOptionsItemSelected(item);
            switch (item.getItemId()) {
             case MENU_LOCAL:
                //处理点击事件
              break;
            }
    实现子菜单的功能的步骤:
    1。重新实现public boolean onCreateOptionsMenu(Menu menu)
    2。重新实现public boolean onOptionsItemSelected(MenuItem item)

    阅读全文>>