在对话框首选项的时间选择器中更改数字颜色

changing number color in the timepicker of dialogprefernce

我浏览了几篇 Whosebug 帖子,但遗憾的是还没有找到适合我的东西:我有一个使用时间选择器的 TimePreference。我相信由于我的配色方案,数字没有显示(似乎是白色的白色):Screenshot of how it looks at the moment

我试图找出如何通过样式影响数字颜色。运气不好,但是对于一些指向 android:TimePickerStyle 和类似的 Whosebug 答案,它需要比我设置的更高的 API (API 15)。 我也尝试过使用自定义布局,但我不太确定在哪里最好调用 TimePreference.class 中的布局(或者我碰巧在正确的地方,但也没有运气)。 我也研究过改变钟面颜色的可能性,但也没有进一步研究。

TimePreference 当前看起来像这样(未实现自定义布局):

public class TimePreference extends DialogPreference {

    private int lastHour = 0;
    private int lastMinute = 0;
    private boolean isAm;
    private TimePicker picker = null;


    private static int getHour(String time) {
        String[] pieces = time.split(":");

        return (Integer.parseInt(pieces[0]));
    }

    private static int getMinute(String time) {
        String[] pieces = time.split(":");

        return (Integer.parseInt(pieces[1]));
    }

    public TimePreference(Context ctxt, AttributeSet attrs) {
        super(ctxt, attrs);

        setPositiveButtonText("Set");
        setNegativeButtonText("Cancel");
    }

    @Override
    protected View onCreateDialogView() {
        picker = new TimePicker(getContext());

        //check locale settings whether to use am/pm or 24 hour display
        picker.setIs24HourView(false);

        return (picker);
    }

    @Override
    protected void onBindDialogView(View v) {
        super.onBindDialogView(v);

        picker.setCurrentHour(lastHour);
        picker.setCurrentMinute(lastMinute);
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);

        if (positiveResult) {
            lastHour = picker.getCurrentHour();
            lastMinute = picker.getCurrentMinute();

            String time = String.valueOf(lastHour) + ":" + String.valueOf(lastMinute);

            if (callChangeListener(time)) {
                persistString(time);
            }
        }
    }

    @Override
    protected Object onGetDefaultValue(TypedArray a, int index) {
        return (a.getString(index));
    }

    @Override
    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
        String time;
        if (restoreValue) {
            if (defaultValue == null) {
                time = getPersistedString("12:00 pm");
            } else {
                time = getPersistedString(defaultValue.toString());
            }
        } else {
            time = defaultValue.toString();
        }

        lastHour = getHour(time);
        lastMinute = getMinute(time);

        new TimePickerDialog.OnTimeSetListener() {

            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                isAm = hourOfDay < 12;
            }
        };
    }

    public String getTime() {
        String meridianId;
        if (isAm) {
            if (lastHour > 12) {
                meridianId = " pm";
            } else {
                meridianId = " am";
            }
        } else {
            meridianId = "";
        }
        return lastHour + ":" + lastMinute + meridianId;
    }
}

而我的 style.xml 目前看起来像这样:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        (…)
        <item name="android:dialogTheme">@style/DialogTheme</item>
        <item name="android:alertDialogTheme">@style/DialogTheme</item>
    </style>

<style name="AppAlertDialogContent" parent="Theme.AppCompat.Dialog">
        <!-- To tint EditText and TimePicker -->
        <item name="colorAccent">@color/colorAccent</item>
        <!-- Used for the title and text -->
        <item name="android:textColorPrimary">@color/colorPrimaryDark</item>
        <item name="android:windowBackground">@color/colorPrimary</item>
    </style>

    <style name="DialogTheme" parent="Theme.AppCompat.Dialog">
        <!-- Used for the title and text -->
        <item name="android:textColorPrimary">@color/colorLightAccent</item>
        <!-- Used for the background -->
        <item name="android:background">@color/colorPrimary</item>
        <!-- Used for the buttons -->
        <item name="colorAccent">@color/colorLightAccent</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    </style>

非常感谢您帮助确保这些数字可以被实际读取。无论是通过自定义布局、样式还是其他巧妙的方法。谢谢你的帮助。

深入研究 Timepicker 小部件后,我不得不承认这在 API 21 下是不可能的。所以我最终构建了自己的自定义 UI,使用两个 Numberpickers 和一个 ToggleButton。这给了我足够多的可达视图,我可以在其中充分控制颜色以确保它可读。因为这个android:theme 是救星,因为这是我可以(终于!)影响颜色的地方。

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/timePickerLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<!-- hour -->
<NumberPicker
    android:id="@+id/dialog_hour_np"
    android:layout_width="@dimen/picker_width"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/half_space"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:solidColor="@color/colorPrimaryDark"
    android:theme="@style/AppTheme.Picker"
    app:layout_constraintEnd_toStartOf="@+id/dialog_minute_np"
    app:layout_constraintStart_toStartOf="parent" />
<!-- minute -->
<NumberPicker
    android:id="@+id/dialog_minute_np"
    android:layout_width="@dimen/picker_width"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/half_space"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:solidColor="@color/colorPrimaryDark"
    android:theme="@style/AppTheme.Picker"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/dialog_amPm_tb"
    app:layout_constraintStart_toEndOf="@+id/dialog_hour_np"
    app:layout_constraintTop_toTopOf="parent" />
<!-- AM / PM -->
<ToggleButton
    android:id="@+id/dialog_amPm_tb"
    style="?android:attr/textAppearanceLargeInverse"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="@dimen/half_space"
    android:layout_marginStart="@dimen/std_space"
    android:background="@color/colorPrimaryDark"
    android:textColor="@color/colorLightAccent"
    android:textOff="@string/time_string_pm"
    android:textOn="@string/time_string_am"
    app:layout_constraintBottom_toBottomOf="@+id/dialog_minute_np"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/dialog_minute_np"
    app:layout_constraintTop_toTopOf="@+id/dialog_minute_np" />

和主题:

<style name="AppTheme.Picker" parent="Theme.AppCompat.Light.NoActionBar" >
        <item name="android:textColorPrimary">@color/colorAccent</item>
        <item name="android:textColorSecondary">@color/colorLightAccent</item>
    </style>