更改 DatePickerDialog 中某个按钮的文本颜色?

2022-09-02 11:11:38

我有一个日期选取器对话框,其主题是

<style name="DatePickerTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">@color/that_blue_color</item>
</style>

我制作了这个自定义主题,因为我想更改datePickerDialog的背景颜色。我设法更改了背景的颜色,选择器圆圈和按钮的文本颜色。但是我现在想更改按钮的文本颜色,并保持按钮的文本颜色不变。我该怎么做?CANCELOK


答案 1

您可以使用 获取 from 并修改其属性。请参阅下面的示例。调用其他明智的按钮后,它将给出一个.ButtonDialoggetButton().show()null

 final Calendar c = Calendar.getInstance();
            int mYear = c.get(Calendar.YEAR);
            int  mMonth = c.get(Calendar.MONTH);
            int mDay = c.get(Calendar.DAY_OF_MONTH);
            DatePickerDialog datePickerDialog = new DatePickerDialog(ConstarintsActivity.this,
                    (view, year, monthOfYear, dayOfMonth) -> {


                    }, mYear, mMonth, mDay);
            datePickerDialog.show();
            datePickerDialog.getButton(DatePickerDialog.BUTTON_NEGATIVE).setTextColor(Color.GREEN);

答案 2

您可以为按钮制作不同的样式,如下所示。CANCEL

<style name="DatePickerTheme" parent="Theme.MaterialComponents.Light.Dialog.Alert">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:buttonBarNegativeButtonStyle">@style/DatePickerTheme.ButtonBarNegativeButtonStyle</item>
</style>

<style name="DatePickerTheme.ButtonBarNegativeButtonStyle" parent="Widget.MaterialComponents.Button.TextButton.Dialog">
    <item name="android:textColor">@android:color/holo_red_light</item>
</style>

推荐