2013-03-12 57 views
6

我试图更改AlertDialog消息的字体大小。更改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

回答

19

使用此:

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的错误在你的问题。

+0

由于某种原因,当我按下提交按钮以显示警告对话框消息时,我的程序崩溃了 – Alex 2013-03-12 13:25:08

+1

我编辑了我的答案。因为你使用'builder.show();'错误。 – AwadKab 2013-03-12 14:54:15

+0

这工作完美!非常感谢!我也试图改变“退出”按钮的大小,但因为我对Android非常陌生,所以无法弄清楚如何正确执行。 – Alex 2013-03-12 15:31:44

0

既然你不使用CustomDialog,我建议你的TextView添加如下,

TextView textView = (TextView) findViewById(android.R.id.message); // remove builder object 
+0

这个工作,谢谢。但是,当我按提交按钮时,我的程序崩溃。我已经更新了我的代码。 – Alex 2013-03-12 13:16:46

1

findViewById属于类型视图的方法。

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

由于某种原因,当我按下提交按钮来显示警告对话框消息时,我的程序崩溃了 – Alex 2013-03-12 13:21:10

+0

也许如果您向我们显示logcat输出:) – 2013-03-12 15:52:09

+0

我已经使用AwadKab的解决方案并设法更改警报消息的大小而不会出现任何错误。你知道我是否可以为“退出”按钮做同样的事情?我对Android开发非常陌生,无法弄清楚如何做到这一点。谢谢。 – Alex 2013-03-12 16:00:50

1

试试这个

AlertDialog dialog = new AlertDialog.Builder(this).setMessage("Hello world").show(); 
TextView textView = (TextView) dialog.findViewById(android.R.id.message); 
textView.setTextSize(40);