在AlertDialog中显示长消息

时间:2012-05-22 09:51:59

标签: android android-alertdialog android-dialog

我正在尝试使用以下代码在Default AlertDialog中显示一条冗长的消息。

public static void showAlert(String message, Context con) {
    AlertDialog.Builder dialog = new AlertDialog.Builder(con);
    dialog.setTitle(message);
    dialog.setPositiveButton(" OK ", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();

        }
    });
    dialog.show();

}

但是,当我通过长消息时,警报对话框不显示完整消息。我可以按对话框对齐Dialog的文本。我不想使用自定义对话框。

3 个答案:

答案 0 :(得分:9)

更改

dialog.setTitle(message);

dialog.setMessage(message);

如果你想对齐文本,你必须创建 像以下示例中的textview:

    AlertDialog.Builder dialog = new AlertDialog.Builder(con);
    dialog.setTitle(message);
    dialog.setPositiveButton(" OK ", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();

        }
    });
    dialog.show();

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

取自:Center message in android dialog box

答案 1 :(得分:1)

尝试以下代码

public static void showAlert(String message, Context con) {
    AlertDialog.Builder dialog = new AlertDialog.Builder(con);
    dialog.setMessage(message);
    dialog.setPositiveButton(" OK ", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();

        }
    });
    dialog.show();

}

答案 2 :(得分:1)

AlertDialog.Builder builder1 = new AlertDialog.Builder(getParent());

builder1.setTitle("Login Alert");

TextView myMsg = new TextView(getParent());

myMsg.setText("You have not logged in to save the product.Do you want to login?");

myMsg.setTextSize(20);

myMsg.setGravity(Gravity.CENTER_HORIZONTAL);

builder1.setView(myMsg);