如何更改警报对话框中按钮的颜色

How to change color of Button in Alert Dialog

这是我创建对话框的代码,

builder.setMessage(msg).setNeutralButton("Dismiss",dialogClickListener)
                       .setPositiveButton("Edit", dialogClickListener)
                       .setNegativeButton("Delete", dialogClickListener).show();

是否可以用蓝色而不是红色显示关闭?

是的,可以从样式更改颜色,您可以为对话框定义自定义样式,如下所示:

<style name="AlertDialogCustom" parent="android:Theme.Material.Light.Dialog.Alert">
  <item name="android:colorPrimary">yourColorCode</item>
  <item name="android:colorAccent">yourColorCode</item>
</style>

首先,从构建器创建 AlertDialog:

AlertDialog dialog = builder.create();

然后您可以找到您的按钮并更改颜色:

dialog.show(); //Only after .show() was called
dialog.getButton(dialog.BUTTON_NEGATIVE).setTextColor(your_color);
dialog.getButton(dialog.BUTTON_POSITIVE).setTextColor(your_color);
dialog.getButton(dialog.BUTTON_NEUTRAL).setTextColor(your_color);

这里使用下一个method:

void setTextColor (int color)
     Sets the text color for all the states (normal, selected, focused) to be this color.
Parameters
     color  int: A color value in the form 0xAARRGGBB. Do not pass a resource ID. To get a color value from a resource ID, call getColor.