How can i change the textcolor of my timepicker and datepicker?

你离开我真会死。 提交于 2019-12-21 22:41:12

问题


Currently I am working on one of my first applications. In this application I have a TimePicker and a DatePicker. My current Activity has a dark background. Now I want a white textcolor in my TimePicker/DatePicker.

In my layout I have defined my pickers:

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

The solution should work on 2.3 - 4.1


回答1:


Use:

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

        ...

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

to set TimePicker text color for API >= 11




回答2:


For DatePicker I use this code:

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

Although, it was tested only on Lollipop.




回答3:


I think you can do this using Coding, try following:

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


来源:https://stackoverflow.com/questions/12227884/how-can-i-change-the-textcolor-of-my-timepicker-and-datepicker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!