Show Long Message in AlertDialog

瘦欲@ 提交于 2019-12-07 17:12:16

问题


I am trying to display an lengthy message in Default AlertDialog with following code.

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

}

But when I pass long message Alert Dialog does not display full message. Can I align text of Dialog by center. I dont want to use Custom Dialog.


回答1:


Change

dialog.setTitle(message);

to

dialog.setMessage(message);

and if you want to center align the text, you'll have to create a textview like in the following example:

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

taken from: Center message in android dialog box




回答2:


try following code

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

}



回答3:


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


来源:https://stackoverflow.com/questions/10699644/show-long-message-in-alertdialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!