如何更改按钮的颜色在警报对话框
问题描述:
这里我为创建对话框代码,如何更改按钮的颜色在警报对话框
builder.setMessage(msg).setNeutralButton("Dismiss",dialogClickListener).setPositiveButton("Edit", dialogClickListener)
.setNegativeButton("Delete", dialogClickListener).show();
是否有可能显示驳回蓝色,而不是红色的?
答
首先,从生成器来创建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.
+0
谢谢你,一个有点神秘的答案,但它指出我正确的方向,现在它的作品。 – alberto
答
啊这是可能的,颜色从风格改变,你可以自定义样式您的对话框如下图所示:
<style name="AlertDialogCustom" parent="android:Theme.Material.Light.Dialog.Alert">
<item name="android:colorPrimary">yourColorCode</item>
<item name="android:colorAccent">yourColorCode</item>
</style>
试试这个https://stackoverflow.com/questions/39930505/change-color-of-button-in-alertdialog-builder –
的[在AlertDialog.Builder按钮改变颜色]可能的复制( https://stackoverflow.com/questions/39930505/change-color-of-button-in-alertdialog-builder) –
Sheikh给出的答案告诉我AlertDialog dialog = builder.create(); Brahmy adigopula在原始问题中的回答仅仅是指AlertDialog.show出于蓝色 – alberto