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

Change the text color of one of the buttons in DatePickerDialog?

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

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

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

只需为 CANCEL 按钮制作不同的样式并将该样式放入您的 alertdialog 父主题中,如下所示:

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

<style name="NegativeButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
        <item name="android:textColor">#f00</item>
</style>

我做到了

 datePickerDialog.show();
 datePickerDialog.
 getButton(DatePickerDialog.BUTTON_NEGATIVE).setTextColor(Color.G
 RAY);

您可以从 Dialog 获取 Button 并使用 getButton() 修改它的属性。请参见下面的示例。调用 .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);
<style name="DatePickerTheme" parent="Theme.AppCompat.Light">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/that_blue_color</item>
</style>

在 AndroidManifest.xml 文件中添加这一行 activity android:theme="@style/DatePickerTheme"。

在 XML 文件中试试这个:

<Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Set Date"
    />

现在在 Java 文件中:

btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Initialize a new date picker dialog fragment
                DialogFragment dFragment = new DatePickerFragment();

                // Show the date picker dialog fragment
                dFragment.show(getFragmentManager(), "Date Picker");
            }
        });

public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState){
            final Calendar calendar = Calendar.getInstance();
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH);
            int day = calendar.get(Calendar.DAY_OF_MONTH);

            /*
                Create a DatePickerDialog using Theme.

                    DatePickerDialog(Context context, int theme, DatePickerDialog.OnDateSetListener listener,
                        int year, int monthOfYear, int dayOfMonth)
             */

            // DatePickerDialog THEME_DEVICE_DEFAULT_LIGHT
            DatePickerDialog dpd = new DatePickerDialog(getActivity(),
                    AlertDialog.THEME_DEVICE_DEFAULT_LIGHT,this,year,month,day);
       // Return the DatePickerDialog
            return  dpd;
        }

        public void onDateSet(DatePicker view, int year, int month, int day){
            // Do something with the chosen date
        }

希望对你有所帮助..!

您可以为 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>

只需在您的styles.xml

中添加以下内容
<style name="mdtp_ActionButton.Text" parent="Widget.MaterialComponents.Button.TextButton.Dialog"/>
        dialog.setOnShowListener(dialog -> {
            int positiveColor = ContextCompat.getColor(view.getContext(), R.color.default_blue_color);
            int negativeColor = ContextCompat.getColor(view.getContext(), R.color.font_hint);
            dialog.getButton(DatePickerDialog.BUTTON_POSITIVE).setTextColor(positiveColor);
            dialog.getButton(DatePickerDialog.BUTTON_NEGATIVE).setTextColor(negativeColor);
        });

        dialog.show();

我在对话框的 showListener 中设置了按钮颜色。

 edt_dob.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Birthday_click = false;
                Calendar now = Calendar.getInstance();
                DatePickerDialog dpd = DatePickerDialog.newInstance((DatePickerDialog.OnDateSetListener) context,
                        now.get(Calendar.YEAR),
                        now.get(Calendar.MONTH),
                        now.get(Calendar.DAY_OF_MONTH)
                );
                dpd.setMaxDate(Calendar.getInstance());
                dpd.show(getFragmentManager(), "Datepickerdialog");
                dpd.dismissOnPause(true);
                dpd.setAccentColor(000000);
                dpd.setOnDateSetListener(new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
                        edt_dob.setText(null);
                        edt_dob.setInputType(InputType.TYPE_NULL);

                        int day = dayOfMonth;
                        int yy = year;
                        int month = ++monthOfYear;
                        String str_day = day < 10 ? "0" + day : "" + day;
                        String str_month = month < 10 ? "0" + month : "" + month;
                        String date_select = yy + "-" + str_month + "-" + str_day;

                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                        Date date1 = null;
                        try {
                            date1 = format.parse(date_select);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        String date = format.format(date1);
                        if (date.endsWith("01") && !date.endsWith("11"))
                            format = new SimpleDateFormat("MMMM d'st', yyyy");
                        else if (date.endsWith("02") && !date.endsWith("12"))
                            format = new SimpleDateFormat("MMMM d'nd', yyyy");
                        else if (date.endsWith("03") && !date.endsWith("13"))
                            format = new SimpleDateFormat("MMMM d'rd', yyyy");
                        else
                            format = new SimpleDateFormat("MMMM d'th', yyyy");
                        String yourDate = format.format(date1);
                        edt_dob.setText(yourDate);
                    }
                });
            }
        });

这是我的解决方案。

    <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
        <!-- ... -->
        <item name="android:datePickerDialogTheme">@style/SpinnerDatePickerStyle</item>
    </style>

    <style name="SpinnerDatePickerStyle" parent="android:Theme.Material.Light.Dialog">
        <item name="android:datePickerStyle">@style/myDatePickerStyle</item>
        <item name="android:buttonBarPositiveButtonStyle">@style/DatePickerButtonStyle</item>
        <item name="android:buttonBarNegativeButtonStyle">@style/DatePickerButtonStyle</item>
    </style>

    <style name="myDatePickerStyle" parent="android:Widget.Material.DatePicker">
        <item name="android:datePickerMode">spinner</item>
    </style>

    <style name="DatePickerButtonStyle" parent="android:Widget.Material.Button.Borderless">
        <item name="android:textColor">@color/black</item>
    </style>

不是buttonBarPositiveButtonStyle而是android:buttonBarPositiveButtonStyleandroid: