Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android AlertDialog设置具有自定义背景颜色的MultiChoiceItems_Java_Android_Android Alertdialog - Fatal编程技术网

Java Android AlertDialog设置具有自定义背景颜色的MultiChoiceItems

Java Android AlertDialog设置具有自定义背景颜色的MultiChoiceItems,java,android,android-alertdialog,Java,Android,Android Alertdialog,我得到了一个带有多项选择项的alertDialog: builder.setTitle(R.string.layer_options_title) .setMultiChoiceItems(availableOptions, selectedLayerOptions, new DialogInterface.OnMultiChoiceClickListener() { ... }); 在哪里 来自 <string-array name="layer_opt

我得到了一个带有多项选择项的alertDialog:

builder.setTitle(R.string.layer_options_title)
    .setMultiChoiceItems(availableOptions, selectedLayerOptions, new DialogInterface.OnMultiChoiceClickListener() {
        ...
    });
在哪里

来自

<string-array name="layer_options">
    <item>Should have green background</item>
    <item>Should have yellow background</item>
    <item>Should have orange background</item>
    <item>Should have blue background</item>
</string-array>

应该有绿色背景
应该有黄色背景
应该有橙色的背景
应该有蓝色背景

有没有办法让这些多选项有背景色?

是的,有办法。将复选框放在版面中,并给版面背景颜色。我可以这样做:

AlertDialog.Builder alert = new AlertDialog.Builder(this);
        LinearLayout mainLayout       = new LinearLayout(this);
        mainLayout.setOrientation(LinearLayout.VERTICAL);

        LinearLayout layout1       = new LinearLayout(this);
        layout1.setOrientation(LinearLayout.HORIZONTAL);
        CheckBox cb1 = new CheckBox(getApplicationContext());
        cb1.setText("Easy");
        layout1.addView(cb1);
        layout1.setBackgroundColor(Color.BLUE);
        layout1.setMinimumHeight(50);

        LinearLayout layout2       = new LinearLayout(this);
        layout2.setOrientation(LinearLayout.HORIZONTAL);
        layout2.addView(new TextView(this));
        CheckBox cb2 = new CheckBox(getApplicationContext());
        cb2.setText("Normal");
        layout2.addView(cb2);
        layout2.setBackgroundColor(Color.CYAN);
        layout2.setMinimumHeight(50);

        LinearLayout layout3       = new LinearLayout(this);
        layout3.setOrientation(LinearLayout.HORIZONTAL);
        CheckBox cb3 = new CheckBox(getApplicationContext());
        cb3.setText("Hard");
        layout3.addView(cb3);
        layout3.setBackgroundColor(Color.GREEN);
        layout3.setMinimumHeight(50);

        mainLayout.addView(layout1);
        mainLayout.addView(layout2);
        mainLayout.addView(layout3);
        alert.setTitle("Custom alert demo");
        alert.setView(mainLayout);
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        alert.setPositiveButton("Done", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getBaseContext(), "done", Toast.LENGTH_SHORT).show();
            }
        });

        alert.show();
结果是:

首先,我创建了一个主布局(垂直),如代码中所示。然后,我为每个复选框创建了一个水平布局。在这种情况下,您可以使用元素(复选框、项目等)的颜色和字体

试试这个:

                AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
            builder1.setTitle("Some Titel?");
            builder1.setCancelable(true);

            List<Spanned> listItems = new ArrayList<Spanned>();
            listItems.add(Html.fromHtml("ipohne"));
            listItems.add(Html.fromHtml("windows"));
            listItems.add(Html.fromHtml("<span style='background-color: #FFFF00'>samsung</span>"));

            final CharSequence[] charSequenceItems = listItems.toArray(new CharSequence[listItems.size()]);
            builder1.setMultiChoiceItems(charSequenceItems, null, null);

            builder1.setPositiveButton("Weiter",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
            builder1.setNegativeButton("Abbrechen",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
            AlertDialog alert11 = builder1.create();
            alert11.show();
AlertDialog.builder1=新建AlertDialog.Builder(MainActivity.this);
builder1.setTitle(“一些滴度?”);
builder1.setCancelable(true);
List listItems=new ArrayList();
添加(Html.fromHtml(“ipohne”);
添加(Html.fromHtml(“windows”);
添加(Html.fromHtml(“三星”));
final CharSequence[]charSequenceItems=listItems.toArray(新的CharSequence[listItems.size());
builder1.setMultiChoiceItems(charSequenceItems,null,null);
builder1.setPositiveButton(“Weiter”,
新建DialogInterface.OnClickListener(){
public void onClick(DialogInterface对话框,int-id){
dialog.cancel();
}
});
builder1.setNegativeButton(“Abbrechen”,
新建DialogInterface.OnClickListener(){
public void onClick(DialogInterface对话框,int-id){
dialog.cancel();
}
});
AlertDialog alert11=builder1.create();
alert11.show();

您找到解决方案了吗?很遗憾,没有,noGuys,您检查过我的解决方案了吗?我可以更改AlertDialog中复选框的背景色。这不是你的问题吗??请看结果图片。太棒了!关于这个问题,我已经在网上浏览了很多文章,但在我看来,这是最有效、最优雅的解决方案。现在我关心的下一个问题是如何将android颜色代码转换为html。哈哈
                AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
            builder1.setTitle("Some Titel?");
            builder1.setCancelable(true);

            List<Spanned> listItems = new ArrayList<Spanned>();
            listItems.add(Html.fromHtml("ipohne"));
            listItems.add(Html.fromHtml("windows"));
            listItems.add(Html.fromHtml("<span style='background-color: #FFFF00'>samsung</span>"));

            final CharSequence[] charSequenceItems = listItems.toArray(new CharSequence[listItems.size()]);
            builder1.setMultiChoiceItems(charSequenceItems, null, null);

            builder1.setPositiveButton("Weiter",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
            builder1.setNegativeButton("Abbrechen",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
            AlertDialog alert11 = builder1.create();
            alert11.show();