首页 > 解决方案 > 更改日期选择器对话框主题

问题描述

我已经实现了 datepickerdialog 并希望删除 Cancel 和 Ok Button 的背景颜色并更改这些按钮的文本颜色。这是在 androidx 项目中。

在此处输入图像描述

public void displayCalendar(final TextView tv, final String myFormat) {
    DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            c.set(Calendar.YEAR, year);
            c.set(Calendar.MONTH, monthOfYear);
            c.set(Calendar.DAY_OF_MONTH, dayOfMonth);

            SimpleDateFormat sdf1 = new SimpleDateFormat(myFormat, Locale.US);
            str_date = sdf1.format(c.getTime());
            tv.setText(str_date);
            mListener.onDateSelected();
        }

    };
    DatePickerDialog datePickerDialog = new DatePickerDialog(con, R.style.DatePicker, date, c
            .get(Calendar.YEAR), c.get(Calendar.MONTH),
            c.get(Calendar.DAY_OF_MONTH));

    datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
    datePickerDialog.setTitle("");
    datePickerDialog.show();
}

在风格上——

<style name="DatePicker" parent="Theme.MaterialComponents.Light.Dialog">
    <item name="colorPrimary">#377dff</item>
    <item name="colorPrimaryDark">#1C5CD3</item>
    <item name="colorAccent">#377dff</item>
</style>

标签: androiddatepickerstyles

解决方案


尝试如下

 Button btn_ok = alert.getButton(DialogInterface.BUTTON_POSITIVE);
 btn_ok .setBackgroundColor(Color.YELLOW); //add your color

推荐阅读