-->

Android HoneyComb DatePicker Text Color

2020-01-29 08:31发布

问题:

I'm searching for a possibilitie to adjust the text color of the datepicker widget in an android honeycomb app. I knew that the widget inherent the global text-color which is white in my case, but i need a black text-color for the datepicker as the background here is light grey.

Anyone know how to fix this?

回答1:

DONE IT

Did it in a Theme in the application styles.xml (basically set a style on all EditText fields)

I have this in /values-v11/ so it only affects >HC

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="Theme.SelectDate" parent="@android:style/Theme.Holo.NoActionBar">
        <item name="android:editTextStyle">@style/Widget.EditText.Black</item>
    </style>

    <style name="Widget.EditText.Black" parent="@android:style/Widget.EditText">
        <item name="android:textColor">@color/black</item>
    </style>

</resources>

Then in my AndroidManifest, for the Activity that uses the DatePicker:

      <activity
            android:name=".ui.phone.SelectDateActivity"
            android:label="Date Selection"
            android:screenOrientation="portrait"
            android:theme="@style/Theme.SelectDate" />

That's it!


My Working Out:

I came to this conclusion by checking the DatePicker source:

https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/date_picker.xml

That showed me the DatePicker used NumberPicker

https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/number_picker.xml https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/number_picker_with_selector_wheel.xml

The NumberPicker uses an EditText

You can therefore style an EditText

android : how to change the style of edit text?

And if you search in this file for "editText" you will see you can set a style on all edittext fields in one Activity!

https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/themes.xml

You override this item:

 <item name="editTextStyle">@android:style/Widget.EditText</item>


回答2:

I have found this solution: debugging DatePicker object, I get the object jerarqy. Maybe it's not an elegant solution but it works:

    private void setNumberPickerProperties(DatePicker dp)
{
        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)
                                {
                                        EditText et = (EditText)np.getChildAt(1);
                                        et.setTextColor(Color.BLACK);
                                }
                        }
                }
        }
}


回答3:

Hi there :) There is an EditText widget somewhere within the datepicker widget. You just have to find it. You can do this by using some creative coding and start searching through the childrens of the datepicker widget using methods like getChildAt(index) and getChildCount() and then loop through it.

You can also do something like this, but i'm not sure that it will work on all devices, better loop through the datepickers children:

DatePicker picker;
ViewGroup childpicker;

childpicker = (ViewGroup) findViewById(Resources.getSystem().getIdentifier("month" /*rest is: day, year*/,    "id", "android"));

EditText textview = (EditText)  picker.findViewById(Resources.getSystem().getIdentifier("timepicker_input", "id",  "android"));

textview.setTextColor(Color.GREEN);

I hope this helps :)



回答4:

Hmm I did it like this:

private void hackDatePickerTextColorToBlack(){
    setTextColorBlack(datePicker);
}

private static void setTextColorBlack(ViewGroup v) {
    int count = v.getChildCount();
    for (int i = 0; i < count; i++) {
        View c = v.getChildAt(i);
        if(c instanceof ViewGroup){
            setTextColorBlack((ViewGroup) c);
        } else
        if(c instanceof TextView){
            ((TextView) c).setTextColor(Color.BLACK);
        }
    }
}

This changes the text color to black but careful with recursion this could take some time.

Also when the date picker is used the text goes back to white so that sucks!

FYI here's the source for DatePicker: https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/date_picker.xml

The EditTexts are NumberPickers



回答5:

I had a similar issue, although I was looking to change the text size, but that's a minor detail. I used the same process to pick apart the View hierarchy and change the font size. However, once a month (or day or year) was changed, the font changed back to the original value. Great for viewing, bad for editing. I took the next step and added a change listener. Now when the value gets changed, it pops back to the preferred font size:

public void setFontSize(final int size) {

    LinearLayout l = (LinearLayout) mPicker.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);
                for (int x = 0; x < np.getChildCount(); x++) {
                    View view = np.getChildAt(x);
                    if ((view != null) && (view instanceof TextView)) {
                        final TextView tv = (TextView) view;
                        tv.setTextSize(size);
                        tv.setOnEditorActionListener(new OnEditorActionListener() {

                            public boolean onEditorAction(TextView v,
                                    int actionId, KeyEvent event) {
                                tv.setTextSize(size);
                                return false;
                            }
                        });
                    }
                }
            }
        }
    }

}


回答6:

The function setTextColorBlack() only works once. If I scroll the datepicker then all the textviews is become the default color, until I tab on a particular date or month or year field.