为什么它在Alert Dialog中显示一个按钮?

时间:2014-04-21 10:11:39

标签: android

我想在点击框中显示大量按钮,其中背景设置为该颜色。但它显示了一个蓝色按钮,我必须做什么,以便它显示所有颜色按钮

AlertDialog.Builder builder = new AlertDialog.Builder(
                    this);

            builder.setTitle("Set Background Color").setCancelable(false);
            builder.setNeutralButton("Red",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int id) {
                            // User clicked OK button
                            ll.setBackgroundColor(Color.RED);
                        }
                    });
            builder.setNeutralButton("White",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int id) {
                            // User cancelled the dialog
                            ll.setBackgroundColor(Color.WHITE);
                        }
                    });

            builder.setNeutralButton("Yellow", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int id) {
                            // User cancelled the dialog
                            ll.setBackgroundColor(Color.YELLOW);
                        }
                    });

            builder.setNeutralButton("Green", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,
                        int id) {
                    // User cancelled the dialog
                    ll.setBackgroundColor(Color.GREEN);
                }
            });

            builder.setNeutralButton("Black", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,
                        int id) {
                    // User cancelled the dialog
                    ll.setBackgroundColor(Color.BLACK);
                }
            });

            builder.setNeutralButton("Blue", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,
                        int id) {
                    // User cancelled the dialog
                    ll.setBackgroundColor(Color.BLUE);
                }
            });

            AlertDialog dialog = builder.create();
            dialog.show();

2 个答案:

答案 0 :(得分:2)

尝试以下代码

  AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle("Set Background Color");
    builder.setItems(new CharSequence[]
            {"Red", "White", "Yellow", "Green", "Black", "Blue"},
            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:
                            ll.setBackgroundColor(Color.RED);
                            break;
                        case 1:
                           ll.setBackgroundColor(Color.WHITE);
                            break;
                        case 2:
                            ll.setBackgroundColor(Color.YELLOW);
                            break;
                        case 3:
                            ll.setBackgroundColor(Color.GREEN);
                            break;
                        case 4:
                              ll.setBackgroundColor(Color.BLACK);
                            break;
                        case 5:
                              ll.setBackgroundColor(Color.BLUE);
                            break;
                    }
                }
            });
    builder.create().show();

答案 1 :(得分:2)

显示蓝色,因为在您的代码中,您不断覆盖按钮上的其他颜色。据我所知,你只能有一个中性按钮。而你正在做的是每次创建一个新的中性按钮并替换旧的按钮。

您需要使用所有必需的按钮创建自定义XML布局并对其进行充气。

或者,如果您不需要太多自定义,您可以使用以下代码段:

AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle("Set Background Color");
    builder.setItems(new CharSequence[]
            {"Red", "Green", "Black", "Blue"},
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int position) {
                    switch (position) {
                        case 0:
                            ll.setBackgroundColor(Color.RED);
                            break;
                        case 1:
                           ll.setBackgroundColor(Color.GREEN);
                            break;
                        case 2:
                            ll.setBackgroundColor(Color.BLACK);
                            break;
                        case 3:
                            ll.setBackgroundColor(Color.BLUE);
                            break;
                    }
                }
            });
    builder.create().show();