简体   繁体   English

Android-SetBackgroundColor持续变为蓝色?

[英]Android - SetBackgroundColor keeps going to blue?

I was just programming away a new app on Android Studio, experimenting with code (I'm new to Java). 我刚刚在Android Studio上编程了一个新应用,并尝试了代码(我是Java新手)。 I was experimenting with Dialogs, and had a Dialog to set the Color of the layout. 我正在尝试使用“对话框”,并使用“对话框”来设置布局的颜色。 I had a list of colors and I was using setBackgroundColor(Color.BLACK) For example, and even when i set it to use Black or a RGB value, it always just sets the layout color to blue, even when using Color.GREEN or Color.BLACK. 我有一个颜色列表,并且正在使用setBackgroundColor(Color.BLACK)例如,即使我将其设置为使用Black或RGB值,即使使用Color.GREEN或颜色黑色 Here is the code I am using. 这是我正在使用的代码。

final Dialog dialog = new Dialog(MainActivity.this);

                //Create Alert Dialog
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

                builder.setTitle("Choose background type");

                builder.setItems(_options, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which)
                        {
                            case 0:
                                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                                builder.setTitle("Make your selection");
                                builder.setItems(colors, new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        //Change colors
                                        switch (which)
                                        {
                                            //Black
                                            case 0:
                                                middleLayout.setBackgroundColor(Color.BLACK);
                                            case 1:
                                            //Blue
                                                middleLayout.setBackgroundColor(Color.rgb(0,0,255));
                                            case 2:
                                            //Brown
                                                middleLayout.setBackgroundColor(Color.rgb(102, 51, 0));
                                            case 3:
                                            //Cyan
                                                middleLayout.setBackgroundColor(Color.CYAN);
                                            case 4:
                                            //Yellow
                                                middleLayout.setBackgroundColor(Color.YELLOW);
                                            case 5:
                                            //Orange
                                                middleLayout.setBackgroundColor(Color.rgb(255, 128, 0));
                                            case 6:
                                            //Red
                                                middleLayout.setBackgroundColor(Color.RED);
                                            case 7:
                                            //Grey
                                                middleLayout.setBackgroundColor(Color.GRAY);
                                            case 8:
                                            //White
                                                middleLayout.setBackgroundColor(Color.WHITE);
                                            case 9:
                                            //Green
                                                middleLayout.setBackgroundColor(Color.GREEN);
                                            case 10:
                                            //Pink
                                                middleLayout.setBackgroundColor(Color.rgb(255,51,153));
                                            case 11:
                                            //Cream
                                                middleLayout.setBackgroundColor(Color.rgb(255,204,153));
                                            case 12:
                                            //Purple
                                                middleLayout.setBackgroundColor(Color.rgb(153,0,153));
                                            case 13:
                                            //Sky Blue
                                                middleLayout.setBackgroundColor(Color.rgb(0,128,255));
                                            case 14:
                                            //Dark Blue
                                                middleLayout.setBackgroundColor(Color.rgb(0,0,153));
                                        }

                                    }
                                });
                                AlertDialog alert = builder.create();
                                alert.show();

                            case 1:
                        }
                    }
                });

I was also using a CharSequence[] for the color list. 我还将CharSequence []用于颜色列表。

Any Help? 有帮助吗?

Thanks. 谢谢。

Add a break after each case . 在每种case添加一个break

Without this, all your cases are executed, with the last one setting the color to blue. 没有这种情况,将执行所有案例,最后一个案例将颜色设置为蓝色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM