创建于preference NumberPicker对话框对话框、创建于、preference、NumberPicker

2023-09-06 09:02:53 作者:酷王

我想创建我的preference屏幕NumberPicker对话框。下面这个我已经做了一个: http://stackoverflow.com/a/5533295/2442638

I am trying to create a NumberPicker dialog in my preference screen. I have already made one following this:http://stackoverflow.com/a/5533295/2442638

不过,我的第二个对话框,我只想要一个微调,所以我已经适应了code如下:

However, for my second dialog, I only want one spinner, so I have adapted the code as follows:

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.NumberPicker;

public class SnoozeTPP extends DialogPreference { 

    private int Minute = 0;
    private NumberPicker np= null;

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

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

    public SnoozeTPP(Context context, AttributeSet attrs) {
        super(context, attrs);

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

    @Override
    protected View onCreateDialogView() {
        np = new NumberPicker(getContext());

        return (np);
    }

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

        np.setMaxValue(60);
        np.setValue(Minute);
    }

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

        if (positiveResult) {

            Minute = np.getValue();

            String time = 0 + ":" + String.valueOf(Minute);

            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("08:00");
            } else {
                time = getPersistedString(defaultValue.toString());
            }
        } else {
            time = defaultValue.toString();
        }

        Minute = getMinute(time);
    }

}

有没有错误,在对话框弹出正确的,但它的布局似乎是搞砸了:-)。在整个对话蓝线拉伸,而不是数字的只是宽度。

There are no errors and the dialog pops up correctly, but the layout of it seems to be "messed up" :-). The blue line stretch across the whole dialog instead of just the width of the numbers.

现在的问题是 - 如何正确设置布局? (我相信有很多其他错误的!)

The question is - how to set the layout correctly? (I am sure there are lots of other mistakes as well!)

感谢您

推荐答案

我使用的CyanogenMod数字选择器解决了这一点。 Java文件中:

I solved this by using the CyanogenMod number picker. Java file:

https://github.com/CyanogenMod/android_packages_apps_Trebuchet/blob/cm-10.2/src/com/cyanogenmod/trebuchet/$p$pference/NumberPicker$p$pference.java

XML文件: https://github.com/CyanogenMod/android_packages_apps_Trebuchet/blob/cm-10.2/res/layout/number_picker_dialog.xml