10

我正在尝试更改AlertDialog消息的字体大小。

Button submit = (Button) findViewById(R.id.submitButton);
submit.setOnClickListener(new View.OnClickListener() {

    public void onClick(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(
                Application1GoodExample.this);
        builder.setMessage("Your form has been successfully submitted");
        TextView textView = (TextView) findViewById(android.R.id.message);
        textView.setTextSize(40);
        builder.setNegativeButton("Exit", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();             
            }
        });

        builder.show();
    }
});

我收到一条错误消息,指出 " findViewById()" 类型未定义AlertDialog.Builder

4

4 回答 4

28

用这个 :

AlertDialog alert = builder.create();
alert.show();

TextView msgTxt = (TextView) alert.findViewById(android.R.id.message);   
msgTxt.setTextSize(16.0);

在你的情况下:

Button submit = (Button) findViewById(R.id.submitButton);

submit.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(Application1GoodExample.this);

        builder.setMessage("Your form has been successfully submitted");
        builder.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();             
            }
        });

        // this will solve your error
        AlertDialog alert = builder.create();
        alert.show();
        alert.getWindow().getAttributes();

        TextView textView = (TextView) alert.findViewById(android.R.id.message);
        textView.setTextSize(40);
        Button btn1 = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
        btn1.setTextSize(16);
    }
});

如果这对您没有帮助,请在您的问题中发布您的 LogCat 错误。

于 2013-03-12T13:16:56.403 回答
4

试试这个

 AlertDialog dialog = new AlertDialog.Builder(this).setMessage("Hello world").show();
 TextView textView = (TextView) dialog.findViewById(android.R.id.message);
 textView.setTextSize(40);
于 2013-03-12T13:54:06.313 回答
1

findViewById 方法属于 View 类型。

AlertDialog.Builder builder = new AlertDialog.Builder(Application1GoodExample.this);
builder.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
    dialog.cancel();           
    } 
});
builder.setMessage("Your form has been successfully submitted");
AlertDialog theDialog=builder.create();
TextView textView = (TextView) theDialog.findViewById(android.R.id.message);
textView.setTextSize(40);

theDialog.show();
于 2013-03-12T13:11:22.337 回答
0

由于您没有使用 CustomDialog,我建议您添加 TextView 如下,

TextView textView = (TextView) findViewById(android.R.id.message); // remove builder object
于 2013-03-12T13:09:16.173 回答