码迷,mamicode.com
首页 > 移动开发 > 详细

android 之 菜单

时间:2017-05-31 12:06:54      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:title   ons   字符   上下   head   ext   ble   rate   cli   

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();
    }

技术分享技术分享

技术分享技术分享

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);
}

技术分享技术分享

android 之 菜单

标签:title   ons   字符   上下   head   ext   ble   rate   cli   

原文地址:http://www.cnblogs.com/yechanglv/p/6923047.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!