代码之家  ›  专栏  ›  技术社区  ›  Dishonered

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

  •  0
  • Dishonered  · 技术社区  · 7 年前

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

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

    我做这个自定义主题是因为我想更改日期选择对话框的背景色。我设法改变了背景色,选取圆和按钮的文本颜色。但我现在想更改 CANCEL 按钮并保留文本颜色 OK 按钮不变。我该怎么做?

    5 回复  |  直到 6 年前
        1
  •  5
  •   ADM    7 年前

    你可以得到 Button Dialog 并使用修改IT属性 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);
    
        2
  •  1
  •   Dishonered    7 年前

    我是这样做的

     datePickerDialog.show();
     datePickerDialog.
     getButton(DatePickerDialog.BUTTON_NEGATIVE).setTextColor(Color.G
     RAY);
    
        3
  •  1
  •   Manish Karena    7 年前

    只需改变风格 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
  •  1
  •   Shehan Rangana    6 年前

    您可以为 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>
    
        5
  •  0
  •   Viral Patel    7 年前
    <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=“@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
            }
    

    我希望它能帮助你….