How to add multiple buttons on a single AlertDialog

前端 未结 5 485
暗喜
暗喜 2020-12-14 06:12

I have a butoon, on clicking of this button i want to open multiple buttons on a single AlertDialog like this :\"enter

5条回答
  •  盖世英雄少女心
    2020-12-14 06:28

    A simple solution without xml:

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle("Title");
    builder.setItems(new CharSequence[]
            {"button 1", "button 2", "button 3", "button 4"},
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // The 'which' argument contains the index position
                    // of the selected item
                    switch (which) {
                        case 0:
                            Toast.makeText(context, "clicked 1", Toast.LENGTH_SHORT).show();
                            break;
                        case 1:
                            Toast.makeText(context, "clicked 2", Toast.LENGTH_SHORT).show();
                            break;
                        case 2:
                            Toast.makeText(context, "clicked 3", Toast.LENGTH_SHORT).show();
                            break;
                        case 3:
                            Toast.makeText(context, "clicked 4", Toast.LENGTH_SHORT).show();
                            break;
                    }
                }
            });
    builder.create().show();
    

提交回复
热议问题