android 之 菜单

Posted 叶常绿

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android 之 菜单相关的知识,希望对你有一定的参考价值。

android的菜单主要分三类:选项菜单(Options Menu)、上下文菜单(Context Menu)、子菜单(Submenu)。

1 选项菜单和子菜单

一个Menu对象代表一个菜单,Menu中可添加菜单项MenuItem,也可以添加子菜单SubMenu。

  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
   public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        //添加性别子菜单
        SubMenu subMenuGender=menu.addSubMenu(MAIN_GROUP, MENU_GENDER, 0,"性别");
        subMenuGender.setIcon(R.drawable.gender);
        subMenuGender.setHeaderIcon(R.drawable.gender);
        male=subMenuGender.add(GENDER_GROUP,MENU_GENDER_MALE, 0, "男");//为子菜单添加选项
        male.setChecked(true);//设置默认选项
        subMenuGender.add(GENDER_GROUP,MENU_GENDER_FEMALE, 0, "女");//为子菜单添加选项
        subMenuGender.setGroupCheckable(GENDER_GROUP, true, true);//设置GENDER_GROUP组是可选的、互斥的
        
        //添加兴趣子菜单
       SubMenu subMenuHobby=menu.addSubMenu(MAIN_GROUP, MENU_HOBBY, 1, "爱好");
        subMenuHobby.setIcon(R.drawable.hobby);
        miaHobby[0]=subMenuHobby.add(HOBBY_GROUP, MENU_HOBBY1, 0, "游泳");
        miaHobby[1]=subMenuHobby.add(HOBBY_GROUP, MENU_HOBBY2, 1, "篮球");
        miaHobby[2]=subMenuHobby.add(HOBBY_GROUP, MENU_HOBBY3, 2, "跑步");
        miaHobby[0].setCheckable(true);
        miaHobby[1].setCheckable(true);
        miaHobby[2].setCheckable(true);//设置为可多选
        
        MenuItem ok=menu.add(MAIN_GROUP, MENU_OK, 3, "确定");//为主菜单添加选项菜单
        OnMenuItemClickListener lsn=new OnMenuItemClickListener() {
            
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                // TODO Auto-generated method stub
                appendStateStr();
                return true;
            }
        };
        ok.setOnMenuItemClickListener(lsn);
        ok.setAlphabeticShortcut(\'o\');
//给确定菜单设置字符快捷键
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch(item.getItemId()){
        case MENU_GENDER_MALE:
        case MENU_GENDER_FEMALE:
            item.setChecked(true);
            appendStateStr();
            break;
        case MENU_HOBBY1:
        case MENU_HOBBY2:
        case MENU_HOBBY3:
            item.setChecked(!item.isChecked());
            appendStateStr();
            break;
        }
        return true;
    }

   private void appendStateStr() {
        // TODO Auto-generated method stub
        String result="您选择的性别是:";
        if(male.isChecked()){
            result+="男";
        }else{
            result+="女";
        }
        String hobbyStr="";
        for(MenuItem mi:miaHobby){
            if(mi.isChecked()){
                hobbyStr+=mi.getTitle()+"、";
            }
        }
        if(hobbyStr.length()>0){
            result+=",您的爱好为:"+hobbyStr.substring(0,hobbyStr.length()-1)+".\\n";
        }else{
            result=result+".\\n";
        }
        Toast.makeText(mainActivity.this, result, Toast.LENGTH_LONG).show();
    }

image_thumbimage_thumb[1]

image_thumb[3]image_thumb[2]

2 上下文菜单

上下文菜单是注册到某个View对象上的,如果一个View对象注册了上下文菜单,用户可通过长按该View对象弹出上下文菜单.

首先在onCreateContextMenu中为不同空间配置不同的上下文菜单:

@Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        // TODO Auto-generated method stub
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderIcon(R.drawable.bird);
        if(v==findViewById(R.id.editview01)){
            menu.add(0,1,0,"菜单项1");
            menu.add(0,2,0,"菜单项2");
            menu.add(0,3,0,"菜单项3");
        }else if(v==findViewById(R.id.editview02)){
            menu.add(0,4,0,"菜单项4");
            menu.add(0,5,0,"菜单项5");
        }
    }

在onCreate方法中为两个文本框注册上下文菜单:

@Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       this.registerForContextMenu(findViewById(R.id.editview01));
       this.registerForContextMenu(findViewById(R.id.editview02));
       
   }

添加上下文菜单监听事件:

@Override
public boolean onContextItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch(item.getItemId()){
    case 1:
    case 2:
    case 3:
        Toast.makeText(mainActivity.this, "这是第一个EditView的上下文菜单"+item.getTitle(), Toast.LENGTH_LONG).show();
    case 4:
    case 5:
        Toast.makeText(mainActivity.this, "这是第二个EditView的上下文菜单"+item.getTitle(), Toast.LENGTH_LONG).show();
            
    }
    return super.onContextItemSelected(item);
}

image_thumb[6]image_thumb[5]

以上是关于android 之 菜单的主要内容,如果未能解决你的问题,请参考以下文章

Android之卫星菜单的实现

Android之菜单总结

Android高仿网易新闻客户端之侧滑菜单

Android基本控件之Menus

一起学Android之Menu

Android之Button 和下拉菜单的结合