将摘要/附加文本添加到alertDialog单选项

时间:2014-07-07 15:27:06

标签: android android-alertdialog

有没有办法使用单选项向alertDialog添加摘要或其他文本?我希望有一个粗体标题和一些带有一些额外信息的小文本。

4 个答案:

答案 0 :(得分:2)

谷歌说:

  

"由于列表出现在对话框的内容区域中,因此对话框无法显示消息和列表":   http://developer.android.com/guide/topics/ui/dialogs.html

似乎按钮,消息和多项选择项是独占的。 如果您想同时拥有消息和多项选项,则必须使用自己的DialogFragment自定义内容

答案 1 :(得分:-1)

设置标题,然后设置消息:

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context);

            // set title
            alertDialogBuilder.setTitle("Your Title");

            // set dialog message
            alertDialogBuilder
                .setMessage("Click yes to exit!")
                .setCancelable(false)
                .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        // if this button is clicked, close
                        // current activity
                        MainActivity.this.finish();
                    }
                  })
                .setNegativeButton("No",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        // if this button is clicked, just close
                        // the dialog box and do nothing
                        dialog.cancel();
                    }
                });

                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();
            }

答案 2 :(得分:-1)

您可以在AlertDialog的构建方法setSingleChoiceItems中使用 -

final CharSequence[] items = {"Red", "Green", "Blue"};

AlertDialog dialog = new AlertDialog.Builder(this).setMessage("Hello world").show();
TextView textView = (TextView) dialog.findViewById(android.R.id.message);
textView.setTextSize(10); //small text font
dialog.setTitle("Colors");
dialog.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int item) {
                Toast.makeText(getApplicationContext(), 
items[item], Toast.LENGTH_SHORT).show();
     }
 });

答案 3 :(得分:-1)

您可以这样做:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title");
builder.setMessage("Message");
builder.setPositiveButton("OK", null);
AlertDialog dialog = builder.show();

// Must call show() prior to fetching text view
TextView messageView = (TextView)dialog.findViewById(android.R.id.message);
messageView.setGravity(Gravity.CENTER);

来自:https://stackoverflow.com/a/5948851/1239966