我正在嘗試更改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(); } });
我收到一條錯誤消息,指出AlertDialog.Buildertypes的“ findViewById() ”未定義。
用這個 :
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錯誤。
方法findViewById屬于Viewtypes。
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();
嘗試這個
AlertDialog dialog = new AlertDialog.Builder(this).setMessage("Hello world").show(); TextView textView = (TextView) dialog.findViewById(android.R.id.message); textView.setTextSize(40);
由于您沒有使用CustomDialog,我建議您添加TextView如下,
TextView textView = (TextView) findViewById(android.R.id.message); // remove builder object