在Dialog中没有调用onContextItemSelected

时间:2013-02-23 05:16:16

标签: android

我的应用程序中有一个对话框,当单击对话框中的按钮时,将显示一个上下文菜单。但我的问题是没有调用onContextItemSelected。我的代码正在对话框外的其他按钮中工作。

这是我的代码:

dialog = new Dialog(SuperPizzaActivity.this, R.style.DialogSlideAnim);
dialog.setTitle("Select Size and Crust");
dialog.setContentView(R.layout.pizzadialog);
final Button select = (Button) dialog.findViewById(R.id.superpizzadselect);

select.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {

public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
menu.setHeaderTitle("Choose One");
menu.add(1, 1, 1, "Add Chicken");
menu.add(2, 2, 2, "Add Pasta");
menu.add(3, 3, 3, "Add Drinks");
menu.add(4, 4, 4, "Continue");
}
});

select.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
openContextMenu(v);
}
});



     public boolean onContextItemSelected(MenuItem item) {
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
            .getMenuInfo();

            switch (item.getItemId()) {
            case 1:
                dialog.dismiss();
                Intent i1 = new Intent(SuperPizzaActivity.this,SuperChickenActivity.class);
                i1.putExtra(checker, checker);
                startActivity(i1);
                overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);        
                break;

            case 2:
                dialog.dismiss();
                Intent i = new Intent(SuperPizzaActivity.this,SuperPastaActivity.class);
                i.putExtra(checker, checker);
                startActivity(i);
                overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                break;

            case 3:
                dialog.dismiss();
                Intent i2 = new Intent(SuperPizzaActivity.this,SuperDrinksActivity.class);
                i2.putExtra(checker, checker);
                startActivity(i2);
                overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                break;

            case 4:
                dialog.dismiss();
                Intent i3 = new Intent(SuperPizzaActivity.this,SuperSummaryList.class);
                startActivity(i3);
                overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                break;

            }
            return true;
            }

任何想法都将受到赞赏。

2 个答案:

答案 0 :(得分:0)

只需按照以下步骤添加和创建菜单。

@Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle("Choose One");
        menu.add(1, 1, 1, "Add Chicken");
        menu.add(2, 2, 2, "Add Pasta");
        menu.add(3, 3, 3, "Add Drinks");
        menu.add(4, 4, 4, "Continue");

        }

您必须注册菜单:

@Override
    public boolean onContextItemSelected(MenuItem item) {

        //add image from camera
        if(item.getItemId()==0)
        {
        Toast.makeText(MainActivity.this, "first", Toast.LENGTH_SHORT).show();  
        }
        //select image from list
        else if(item.getItemId()==1)
        {
            Toast.makeText(MainActivity.this, "second", Toast.LENGTH_SHORT).show();
        }
        else if(item.getItemId()==2)
        {
            Toast.makeText(MainActivity.this, "third", Toast.LENGTH_SHORT).show();
        }
        else if(item.getItemId()==3)
        {
            Toast.makeText(MainActivity.this, "fourth", Toast.LENGTH_SHORT).show();
        }
        else if(item.getItemId()==4)
        {
            Toast.makeText(MainActivity.this, "fifth", Toast.LENGTH_SHORT).show();
        }


        return super.onContextItemSelected(item);


    }

更新:

对话框:

private void dialog() {
        // custom dialog
        final Dialog dialog = new Dialog(MainActivity.this,android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
        dialog.setContentView(R.layout.temp);
        getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        // set the custom dialog components - text, image and button
        Button btn = (Button) dialog.findViewById(R.id.btn_login);
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            registerForContextMenu(v); //here opening menuitems
                 openContextMenu(v);
            }
        });

        dialog.show();
    }

在点击事件上调用菜单:

btn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog(); // here dialog will open.

                }
            });

我刚做了一个演示及其作品。

答案 1 :(得分:0)

请致电

registerForContextMenu method

在你的问题中我认为

 registerForContextMenu(select);

我希望这会有所帮助

修改

我在我的应用程序中使用了以下代码,它成功运行

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

    listview = (ListView)findViewById(R.id.lvHomeList);
    listview.setOnItemClickListener(this);
    registerForContextMenu(listview);
    if (null == adapter) {
        adapter = new HATAdapter(this);
        listview.setAdapter(adapter);
    }
    adapter.notifyDataSetChanged();

    LoadHomeList();
}


@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    AdapterView.AdapterContextMenuInfo info = (AdapterContextMenuInfo)menuInfo;
    if( 0 == info.position )
        return;
    menu.setHeaderTitle( adapter.getItem(info.position).getText() );  
    menu.add( Menu.NONE, HAT.HOME | HAT.ACTION_MODIFY, 0, "Modify");  
    menu.add( Menu.NONE, HAT.HOME | HAT.ACTION_ERASE, 0, "Erase");  
}

@Override  
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
    if( 0 == info.position )
        return false;
    int nResponse = item.getItemId();
    String sStr = adapter.getItem(info.position).getText();
    doResponse(nResponse, sStr);
    return true;  
}