2013-03-12 151 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);