How to change color of Button in Alert Dialog

半腔热情 提交于 2019-12-13 09:37:17

问题


Here my code for create Dialog,

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

Is it possible to display dismiss in blue color rather than red?


回答1:


First, create AlertDialog from builder:

AlertDialog dialog = builder.create();

Then you can find your button and change color:

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

Here you use next 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.



回答2:


Yeah it is possible, change color from style and you can define custom style for your dialog like below:

<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/46397707/how-to-change-color-of-button-in-alert-dialog

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!