警报对话框按钮具有不同的颜色集

Alert dialog button has different colorset

你好,我有一个警告对话框。按钮是紫色的。是的,我可以使用以下方法更改文本的颜色:

          alertDialog.getButton(Dialog.BUTTON_NEGATIVE).
                    setTextColor(Color.parseColor("#000000"));

然而当它们被触摸时,会形成一种紫色色调。 我希望色调像我的应用程序的主色调一样是蓝色的,文本是黑色的。

这是我的警报风格:

<style name="MyCustomAlert2" parent="Theme.MaterialComponents.Dialog.Alert">
    <item name="colorAccent">@color/materialBlue</item>
    <item name="android:textColorPrimary">@android:color/black</item>
    <item name="android:windowBackground">@drawable/custom_alert2</item>
    <item name="colorControlNormal">@android:color/black</item>
    <item name="colorControlActivated">@color/materialBlue</item>
    <item name="textColorAlertDialogListItem">@android:color/black</item>
</style>

注意:none 以上链接的颜色是紫色。

感谢您的帮助

使用新的androidx.appcompat.app.AlertDialog and the Material components for android library.

只需使用类似的东西:

new MaterialAlertDialogBuilder(context)
            .setTitle("...")
            .setMessage("....")
            .setPositiveButton("Ok", /* listener = */ null)
            .setNegativeButton("Cancel", /* listener = */ null)
            .show();

然后您可以自定义样式,例如:

  <!-- Alert Dialog -->
  <style name="MyThemeOverlay.MaterialComponents.MaterialAlertDialog" parent="@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog">
    <item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
    <item name="buttonBarNegativeButtonStyle">@style/Widget.MaterialComponents.Button.TextButton.Dialog</item>
  </style>

在这里您可以更改每个按钮:

  <style name="PositiveButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
    <item name="android:textColor">#000000</item>
    <item name="backgroundTint">@color/selector_bt</item>
  </style>

您可以使用以下方法在您的应用主题中全局设置样式:

<item name="materialAlertDialogTheme">@style/MyThemeOverlay.MaterialComponents.MaterialAlertDialog</item>

或者你可以使用构造函数:

new MaterialAlertDialogBuilder(context,
          R.style.MyThemeOverlay_MaterialComponents_MaterialAlertDialog)