6

嗨,我正在尝试在一项活动中制作多个上下文菜单。有两个按钮,我想为每个按钮创建不同的上下文菜单。任何人都可以建议我或向我推荐一些工作示例吗?

这是我当前的代码的样子。

registerForContextMenu(btn_1);
registerForContextMenu(btn_2);


btn_1.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

        openContextMenu(v);
    }   
});

btn_2.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

        openContextMenu(v);
    }   

});


    }

@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        getMenuInflater().inflate(R.menu.media_menu, menu);
        menu.setHeaderTitle("Context menu for btn_1");
        menu.setHeaderIcon(R.drawable.icon_media_up); 
}

@Override
public boolean onContextItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case R.id.photo:
                Toast.makeText(this, "One", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.video:
                Toast.makeText(this, "Two", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.audio:
                Toast.makeText(this, "Three", Toast.LENGTH_SHORT).show();
                return true;
            }
        return super.onContextItemSelected(item);
    }
4

2 回答 2

13

你必须在你的onCreateContextMenu方法中处理这个。有一个参数View是您单击的视图。因此,通过使用该参数,您可以扩展不同的菜单。例如

@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        if(v.getId() == R.id.first_button)
             inflate one menu
        else if(v.getId() == R.id.second_button)
             inflate another menu        
}
于 2013-04-24T15:19:11.617 回答
3

这是工作解决方案

 @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        // TODO Auto-generated method stub
        super.onCreateContextMenu(menu, v, menuInfo);

     MenuInflater menuiflatr=getMenuInflater();
         if(v.getId() == R.id.ChooseSession){


        menuiflatr.inflate(R.menu.session_menu, menu);
         menu.setHeaderTitle("Choose Session Type");
         menu.setHeaderIcon(R.drawable.ic_launcher);
         MenuItem item_individual=menu.findItem(R.id.individual);
         MenuItem item_group=menu.findItem(R.id.group);
         if(itemselection_for_sessiontype==1)item_individual.setChecked(true);
         else if(itemselection_for_sessiontype==2)item_group.setChecked(true);
    }
        else if(v.getId() == R.id.ChooseTrajectory){
             //inflate another menu     
             menuiflatr.inflate(R.menu.trajectory_menu, menu);
             menu.setHeaderTitle("Enable Trajectory");
             menu.setHeaderIcon(R.drawable.ic_launcher);
             MenuItem item_On=menu.findItem(R.id.on);
             MenuItem item_Off=menu.findItem(R.id.off);
             if(itemselection_for_trajectory==1)item_On.setChecked(true);
             else if(itemselection_for_trajectory==2)item_Off.setChecked(true);
    }
        else if(v.getId() == R.id.ChooseMapeType){
            //inflate another menu     
            menuiflatr.inflate(R.menu.maptype_menu, menu);
            menu.setHeaderTitle("Choose Map Type");
         menu.setHeaderIcon(R.drawable.ic_launcher);
       }
   }`
  `
     @Override
     public boolean onContextItemSelected(MenuItem item) {
        switch(item.getItemId()){
        case R.id.individual:
            Toast.makeText(this, "Session Type Individual selected!",     `   `Toast.LENGTH_SHORT).show();
            item.setChecked(true);
            itemselection_for_sessiontype=1;
            return true;

    case R.id.group:
        Toast.makeText(this, "Session Type Group selected!", Toast.LENGTH_SHORT).show();
        item.setChecked(true);
        itemselection_for_sessiontype=2;
        return true;

    case R.id.on:
        Toast.makeText(this, "Trajectory Mode Enabled", Toast.LENGTH_SHORT).show();
        item.setChecked(true);
        itemselection_for_trajectory=1;
        return true;

    case R.id.off:
        Toast.makeText(this, "Trajectory Mode Disabled!",               Toast.LENGTH_SHORT).show();
        item.setChecked(true);
        itemselection_for_trajectory=2;
        return true;
    }





    return super.onContextItemSelected(item);
    }

    public void selectSession(View view){
        registerForContextMenu(view);

        openContextMenu(view);
    }
于 2015-08-15T08:30:06.910 回答