簡體   English   中英

具有自定義背景顏色的Android AlertDialog setMultiChoiceItems

[英]Android AlertDialog setMultiChoiceItems with custom background color

我得到了這個alertDialog,有多項選擇:

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

哪里

availableOptions = getApplicationContext().getResources().getStringArray(R.array.layer_options);

來自

<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();

截圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM