1

对于自定义首选项,例如时间选择器首选项,我在 Android 中使用以下 XML:

<com.my.package.TimePreference android:key="notification_time" android:selectable="true" android:title="@string/notification_time" android:enabled="true" android:summary="@string/setTime" android:defaultValue="00:00" />

“TimePreference”类在哪里:

package com.my.package;

import android.content.Context;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TimePicker;

public class TimePreference extends DialogPreference {

    private int lastHour = 0;
    private int lastMinute = 0;
    private TimePicker picker = null;
    private boolean is_24_hours = true;

    public static int getHour(String time) {
        String[] pieces = time.split(":");
        return(Integer.parseInt(pieces[0]));
    }

    public static int getMinute(String time) {
        String[] pieces = time.split(":");
        return(Integer.parseInt(pieces[1]));
    }

    public TimePreference(Context ctxt) {
        this(ctxt, null);
    }

    public TimePreference(Context ctxt, AttributeSet attrs) {
        this(ctxt, attrs, 0);
    }

    public TimePreference(Context ctxt, AttributeSet attrs, int defStyle) {
        super(ctxt, attrs, defStyle);
        setPositiveButtonText(ctxt.getString(R.string.save));
        setNegativeButtonText(ctxt.getString(R.string.cancel));
        try {
            is_24_hours = DateFormat.is24HourFormat(ctxt);
        }
        catch (Exception e) {
            is_24_hours = true;
        }
    }

    @Override
    protected View onCreateDialogView() {
        picker = new TimePicker(getContext());
        if (is_24_hours) {
            picker.setIs24HourView(true);
        }
        else {
            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) {
            picker.clearFocus(); // important - otherwise manual input is not saved
            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 = null;
        if (restoreValue) {
            if (defaultValue == null) {
                time = getPersistedString("00:00");
            }
            else {
                time = getPersistedString(defaultValue.toString());
            }
        }
        else {
            time = defaultValue.toString();
        }
        lastHour = getHour(time);
        lastMinute = getMinute(time);
    }
}

这在 Android 2.2 和 2.3 上一直很有效——但现在我已经切换到 Android 4.0,我可以看到这个首选项不会适应首选项屏幕的布局:

在此处输入图像描述

我怎么解决这个问题?有没有比手动设置边距/填充更好的解决方案?

4

1 回答 1

4

终于发现问题了:

super()必须调用只接受两个参数(并且没有int defStyle参数)的构造函数。

在问题的代码中,super()使用预定义defStyle的值调用0whcih 会阻止 Android 选择任何好的布局。如果您在super()没有给出默认样式参数的情况下调用,超类DialogPreference的构造函数会自行选择最佳样式。

于 2012-12-30T14:17:47.023 回答