如何在android中的alertdialog中创建第二个标题

时间:2016-07-28 21:29:58

标签: android alertdialog

我想有这个alertdialog,但有第二个标题将以下列表分成两部分。

This is how the alertdialog current looks

以下是代码:

final CharSequence[] items = {" Cereal ", " Chocolate chips ", " Crunchy peanut butter ", " Vanilla ", " Espresso powder ",
            " Kosher salt ", " Powdered sugar ", " Marshmallows "};
    // arraylist to keep the selected items
    final ArrayList seletedItems = new ArrayList();

    builder = new AlertDialog.Builder(this);
    builder.setTitle("Ingredients List");
    builder.setMultiChoiceItems(items, null,
            new DialogInterface.OnMultiChoiceClickListener() {
                // indexSelected contains the index of item (of which checkbox checked)
                @Override
                public void onClick(DialogInterface dialog, int indexSelected,
                                    boolean isChecked) {
                    if (isChecked) {
                        // If the user checked the item, add it to the selected items
                        // write your code when user checked the checkbox
                        seletedItems.add(indexSelected);
                    } else if (seletedItems.contains(indexSelected)) {
                        // Else, if the item is already in the array, remove it
                        // write your code when user Uchecked the checkbox
                        seletedItems.remove(Integer.valueOf(indexSelected));
                    }
                }
            })
            // Set the action buttons
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    //  Your code when user clicked on OK
                    //  You can write the code  to save the selected item here

                }
            });
    dialog = builder.create();

1 个答案:

答案 0 :(得分:0)

AlertDialog不能有第二个标题,但您可以在内容视图中创建它,以设置内容查看使用:

 dialog.setContentView(R.layout.your_content_view);

在布局 R.layout.your_content_view 您可以拥有任何结构,因此可以添加看起来像标题的TextView。

只设置标题文字:

 TextView text = (TextView) dialog.findViewById(R.id.header);
 text.setText("Header text");

当然 R.id.header 必须存在于 R.layout.your_content_view