【问题标题】:Change the text color of one of the buttons in DatePickerDialog?更改 DatePickerDialog 中某个按钮的文本颜色?
【发布时间】:2018-06-09 07:12:06
【问题描述】:

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

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

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

【问题讨论】:

    标签: java android android-theme


    【解决方案1】:

    您可以从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);
    

    【讨论】:

      【解决方案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>
      

      【讨论】:

      • Shukriya :) bhai
      【解决方案3】:
              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 中设置了按钮颜色。

      【讨论】:

        【解决方案4】:

        我设法做到了这样

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

        【讨论】:

        • 为负和正按钮添加项目对我有用。这个答案有效。感谢分享。
        【解决方案5】:

        只需为CANCEL 按钮制作不同的样式,然后将该样式放入您的警报对话框的父主题中,如下所示:

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

        【讨论】:

          【解决方案6】:

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

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

          【讨论】:

          【解决方案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
                  }
          

          希望对你有帮助..!

          【讨论】:

            【解决方案8】:
             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);
                                }
                            });
                        }
                    });
            

            【讨论】:

              【解决方案9】:

              这是我的解决方案。

                  <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:

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2017-04-10
                • 2011-03-14
                • 1970-01-01
                • 2015-02-02
                • 1970-01-01
                相关资源
                最近更新 更多