【问题标题】:How can i change the textcolor of my timepicker and datepicker?如何更改时间选择器和日期选择器的文本颜色?
【发布时间】:2012-09-01 12:49:48
【问题描述】:

目前我正在开发我的第一个应用程序。在这个应用程序中,我有一个TimePicker 和一个DatePicker。我当前的Activity 有深色背景。现在我想在我的TimePicker/DatePicker 中添加一个白色的文本颜色。

在我的布局中,我定义了我的选择器:

<DatePicker android:id="@+id/dpDateOfValue" android:calendarViewShown="false" />
<TimePicker android:id="@+id/tpTimeOfValue" />

该解决方案应该适用于 2.3 - 4.1

【问题讨论】:

    标签: android datepicker android-4.0-ice-cream-sandwich timepicker


    【解决方案1】:

    用途:

    <style name="MyHolo" parent="android:Theme.Holo.NoActionBar">
    
            ...
    
            <item name="android:editTextColor">#000000</item>
    </style>
    

    为 API >= 11 设置 TimePicker 文本颜色

    【讨论】:

      【解决方案2】:

      对于 DatePicker,我使用以下代码:

      public static void setDatePickerTextColor(DatePicker dp, int color) {
          LinearLayout l = (LinearLayout) dp.getChildAt(0);
          if (l != null) {
              l = (LinearLayout) l.getChildAt(0);
              if (l != null) {
                  for (int i = 0; i < 3; i++) {
                      NumberPicker np = (NumberPicker) l.getChildAt(i);
                      if (np != null) {
                          setNumberPickerTextColor(np, color);
                      }
                  }
              }
          }
      }
      
      public static boolean setNumberPickerTextColor(NumberPicker numberPicker, int color) {
          final int count = numberPicker.getChildCount();
          for (int i = 0; i < count; i++) {
              View child = numberPicker.getChildAt(i);
              if (child instanceof EditText) {
                  try {
                      Field selectorWheelPaintField = numberPicker.getClass()
                              .getDeclaredField("mSelectorWheelPaint");
                      selectorWheelPaintField.setAccessible(true);
                      ((Paint) selectorWheelPaintField.get(numberPicker)).setColor(color);
                      ((EditText) child).setTextColor(color);
                      numberPicker.invalidate();
                      return true;
                  } catch (NoSuchFieldException e) {
                      Log.w("NumberPickerTextColor", e);
                  } catch (IllegalAccessException e) {
                      Log.w("NumberPickerTextColor", e);
                  } catch (IllegalArgumentException e) {
                      Log.w("NumberPickerTextColor", e);
                  }
              }
          }
          return false;
      }
      

      虽然,它只在 Lollipop 上进行了测试。

      【讨论】:

        【解决方案3】:

        我认为您可以使用 Coding 来做到这一点,请尝试以下操作:

        DatePicker your_picker = (DatePicker) findViewById(R.id.dpDateOfValue);
        EditText edittext = (EditText) your_picker.findViewById(Resources.getSystem().getIdentifier("datepicker_input", "id",  "android"));
        
        edittext.setTextColor(Color.BLUE);
        

        【讨论】:

        • 这个解决方案对我不起作用。 edittext 对象始终为空。 getIdentifier 函数是否有错误?
        猜你喜欢
        • 2015-05-27
        • 2021-06-27
        • 1970-01-01
        • 2021-08-07
        • 2015-10-22
        • 2016-10-10
        • 1970-01-01
        • 2012-10-26
        • 1970-01-01
        相关资源
        最近更新 更多