2013-03-12 28 views
6

Tôi đang cố gắng thay đổi cỡ chữ của thông báo AlertDialog.Thay đổi kích thước phông chữ cho một thông báo 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(); 
    } 
}); 

Tôi nhận được thông báo lỗi cho biết rằng "findViewById()" không được xác định cho loại AlertDialog.Builder.

Trả lời

19

Sử dụng này:

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

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

trong trường hợp của bạn:

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

Nếu điều này không giúp bạn, gửi Lỗi LogCat của bạn trong câu hỏi của bạn.

+0

Vì lý do nào đó, người thân của tôi gặp sự cố khi tôi nhấn nút gửi để hiển thị thông báo hộp thoại cảnh báo – Alex

+1

Tôi đã chỉnh sửa câu trả lời của mình. Lỗi vì bạn sử dụng 'builder.show();'. – AwadKab

+0

Điều này làm việc hoàn hảo! cảm ơn bạn rất nhiều! Tôi cũng đang cố gắng thay đổi kích thước của nút "Thoát" nhưng vì tôi rất mới với Android nên tôi không thể tìm ra cách thực hiện đúng cách. – Alex

0

Kể từ khi bạn không sử dụng CustomDialog, tôi đề nghị bạn thêm TextView như sau,

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

này làm việc, cảm ơn bạn. Tuy nhiên, chương trình của tôi bị treo khi tôi nhấn nút Gửi. Tôi đã cập nhật mã của mình ở trên. – Alex

1

phương pháp findViewById thuộc loại 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(); 
+0

Đối với một số lý do, progam của tôi gặp sự cố khi tôi nhấn nút gửi để hiển thị thông báo thoại – Alex

+0

có thể nếu bạn hiển thị đầu ra logcat :) –

+0

Tôi đã sử dụng giải pháp của AwadKab và quản lý để thay đổi kích thước của thông báo Cảnh báo mà không gặp bất kỳ lỗi nào . Bạn có biết tôi có thể làm tương tự cho nút "Thoát" không? Tôi rất mới để phát triển Android và không thể tìm ra cách để làm điều này. cảm ơn bạn. – Alex

1

Hãy thử điều này

AlertDialog dialog = new AlertDialog.Builder(this).setMessage("Hello world").show(); 
TextView textView = (TextView) dialog.findViewById(android.R.id.message); 
textView.setTextSize(40); 
Các vấn đề liên quan