2012-09-01 43 views
1

目前我正在開發我的第一個應用程序之一。在這個應用程序中,我有一個TimePicker和一個DatePicker。我目前的Activity有一個黑暗的背景。現在我想在我的TimePicker/DatePicker中使用白色文本顏色。如何更改我的timepicker和datepicker的textcolor?

在我的佈局,我已經定義了我的採摘:

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

該解決方案應該在2.3的工作 - 4.1

回答

-1

我認爲你可以使用編碼爲此,請嘗試以下操作:

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); 
+0

[曾見此](http://grokbase.com/t/gg/android-developers/1287p4zckr/changing-text-color-font-for-datepicker-widget) –

+5

此解決方案對我無效。 edittext對象始終爲空。 getIdentifier函數中是否有錯誤? – user1640506

1

使用:

<style name="MyHolo" parent="android:Theme.Holo.NoActionBar"> 

     ... 

     <item name="android:editTextColor">#000000</item> 
</style> 

設置TimePicker文本顏色爲API> = 11

1

對於我的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; 
} 

雖然,它只是在棒棒糖測試。