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

时间:2018-06-09 07:12:06

标签: java android android-theme

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

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

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

8 个答案:

答案 0 :(得分:4)

您可以从Button获取Dialog并使用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);

答案 1 :(得分:1)

我设法像这样做了

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

答案 2 :(得分:1)

只需在您的styles.xml中添加以下内容

<style name="mdtp_ActionButton.Text" parent="Widget.MaterialComponents.Button.TextButton.Dialog"/>

答案 3 :(得分:0)

只需为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>

答案 4 :(得分:0)

<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文件中,在粒子活动中添加此行android:theme =&#34; @ style / DatePickerTheme&#34;。

在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
        }

我希望它能帮到你..!

答案 5 :(得分:0)

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

答案 6 :(得分:0)

        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中设置按钮的颜色。

答案 7 :(得分:0)

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