2013-03-12 86 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

+1

私の回答を編集しました。 'builder.show();'を使用しているためにエラーが発生しました。 – AwadKab

+0

これは完全に機能しました!どうもありがとうございます!私はまた、 "終了"ボタンのサイズを変更しようとしていますが、私はAndroidにはとても新しいので、正しく行う方法を理解することはできません。 – Alex

0

あなたがCustomDialogを使用していないので、私はあなたが、以下のよう

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

試してみてください、ありがとうございました。しかし、Submitボタンを押すとプログラムがクラッシュします。上記のコードを更新しました。 – Alex

1

findViewByIdがタイプビューに属している方法をのTextViewを追加することをお勧めします。

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

+0

と表示される可能性があります。 –

+0

私はAwadKabのソリューションを使用しましたが、エラーを表示せずに警告メッセージのサイズを変更できました。 「終了」ボタンでも同じことができるかどうか分かりますか?私はAndroid開発には非常に新しいので、これを行う方法を理解できません。ありがとうございました。 – Alex

1

これは働いていたこの

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