设置AlertDialog正向button文本为粗体

这是我的代码。

AlertDialog.Builder builder = new AlertDialog.Builder(this); LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); final View vi = inflater.inflate(R.layout.group_dialog_layout,null); builder.setView(vi); TextView txtNewGroupEntry = (TextView) vi.findViewById(R.id.txtGroupRename); if(isNew==true){ builder.setTitle("New Group"); txtNewGroupEntry.setText(R.string.new_group_instruction); } builder.setPositiveButton(R.string.ok_button, null); builder.setNegativeButton(R.string.cancel_button, null); AlertDialog dialog = builder.create(); dialog.show(); Button okButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE); 

我有一个警告对话框,添加button和取消button。 我希望两个button的文字都是粗体和斜体。 我该怎么做?

Solutions Collecting From Web of "设置AlertDialog正向button文本为粗体"

将其设置为Html.fromHtml("<b><i>add</i></b>")而不是将button文本设置为"add"

所以对于你的代码:

更改这些行:

 builder.setPositiveButton(R.string.ok_button, null); // and builder.setNegativeButton(R.string.cancel_button, null); 

为了这些路线:

 builder.setPositiveButton(Html.fromHtml("<b><i>" + getString(R.string.ok_button) + "</i><b>"), null); // and builder.setNegativeButton(Html.fromHtml("<b><i>" + getString(R.string.cancel_button) + "</i><b>"), null); 

要么

你可以修改strings.xml文件中的strings.xml

例如,如果你的string是这样的:

 <string name="ok_button">add</string> <string name="cancel_button">cancel</string> 

你可以改变他们到这个:

 <string name="ok_button"><b><i>add</i></b></string> <string name="cancel_button"><b><i>cancel</i></b></string> 

您仍然错误地引用您的string资源。 而不是R.string.ok_button ,因为它返回一个int,你将不得不使用getString(R.string.ok_button)

所以你将不得不改变这些行:

 builder.setPositiveButton(R.string.ok_button, null); // and builder.setNegativeButton(R.string.cancel_button, null); 

为了这些路线:

 builder.setPositiveButton(getString(R.string.ok_button), null); // and builder.setNegativeButton(getString(R.string.cancel_button), null); 

这个示例代码将帮助你…

  AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("message").setTitle("title"); AlertDialog alertDialog = builder.create(); DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case DialogInterface.BUTTON_POSITIVE: // do work break; case DialogInterface.BUTTON_NEUTRAL: // do work break; case DialogInterface.BUTTON_NEGATIVE: // do work break; default: break; } } }; alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Yes", listener); alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "No", listener); alertDialog.setButton(DialogInterface.BUTTON_NEUTRAL, "Cancel", listener); alertDialog.setOnShowListener(new DialogInterface.OnShowListener() { @Override public void onShow(DialogInterface dialog) { AlertDialog alertDialog = (AlertDialog) dialog; Button button = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE); button.setTypeface(Typeface.DEFAULT, Typeface.BOLD | Typeface.ITALIC); button = alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE); button.setTypeface(Typeface.DEFAULT, Typeface.BOLD | Typeface.ITALIC); button = alertDialog.getButton(DialogInterface.BUTTON_NEUTRAL); button.setTypeface(Typeface.DEFAULT, Typeface.BOLD | Typeface.ITALIC); } }); alertDialog.show(); 

你可以访问onShow() Dialog's buttons ,就像我在下面提到的那样。 在onShow()触发时,您可以访问YOIR对话框的UI,试试这个,

 private void showAlertDialog(){ AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setMessage("Dailog Demo!"); builder .setCancelable(false) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); final AlertDialog alertdialog = builder.create(); alertdialog.setOnShowListener(new OnShowListener() { @Override public void onShow(DialogInterface dialog) { ((Button)alertdialog.getButton(Dialog.BUTTON_POSITIVE)).setTypeface(null, Typeface.BOLD_ITALIC); ((Button)alertdialog.getButton(Dialog.BUTTON_NEGATIVE)).setTypeface(null, Typeface.BOLD_ITALIC); } }); alertdialog.show(); }