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

时间:2017-09-25 04:39:05

标签: android message

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

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

是否可以用蓝色而不是红色显示消除?

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

首先,从构建器创建AlertDialog:

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

然后你可以找到你的按钮并改变颜色:

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.

在这里使用下一个method

git pull