在偏好设置中创建数字选择对话框

8

我正在尝试在我的首选项屏幕中创建一个NumberPicker对话框。 我已经按照以下方式创建了一个:https://dev59.com/c2035IYBdhLWcg3wPdkS#5533295

但是,对于我的第二个对话框,我只想要一个下拉菜单,因此我已将代码修改如下:

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

}

没有错误,对话框也可以正确弹出,但是其布局似乎有些混乱 :-). 蓝线跨越整个对话框而不仅仅是数字的宽度。enter image description here

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

谢谢


这里有一个链接可能对你有帮助:https://gist.github.com/tomstrummer/959884 - Ryan S
@RSenApps 谢谢,我确实尝试了那个方法,但是我没能让它工作 :-( - user2442638
4个回答

5

这是一个很棒的答案。 - Etienne Lawlor
2
Styleable在这里:https://github.com/CyanogenMod/android_packages_apps_Trebuchet/blob/cm-10.2/res/values/attrs.xml - Gh61
5
这个类似乎引用了com.android.internal包。我需要像这里中所描述的那样构建一个original-android.jar来使用它,还是我在过于复杂化问题? - Code Commander

3
以下是一个简单但可工作的NumberPickerPreference示例,保存介于1和100之间的整数值: 应用程序截图 NumberPickerPreference.java: 链接
public class NumberPickerPreference extends DialogPreference {
    private NumberPicker mPicker;
    private Integer mNumber = 0;

    public NumberPickerPreference(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NumberPickerPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setPositiveButtonText(android.R.string.ok);
        setNegativeButtonText(android.R.string.cancel);
    }

    @Override
    protected View onCreateDialogView() {
        mPicker = new NumberPicker(getContext());
        mPicker.setMinValue(1);
        mPicker.setMaxValue(100);
        mPicker.setValue(mNumber);
        return mPicker;
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        if (positiveResult) {
            // needed when user edits the text field and clicks OK
            mPicker.clearFocus();
            setValue(mPicker.getValue());
        }
    }   

    @Override
    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
        setValue(restoreValue ? getPersistedInt(mNumber) : (Integer) defaultValue);
    }

    public void setValue(int value) {
        if (shouldPersist()) {
            persistInt(value);
        }

        if (value != mNumber) {
            mNumber = value;
            notifyChanged();
        }
    }

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

1
这更像是一个解决方法而不是解决方案,但我希望它能有所帮助。添加一个虚拟的textView可以解决这个问题。我遇到了完全相同的问题。
我的xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textDummyEmpty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/textDummyEmpty" />

    <NumberPicker 
        android:id="@+id/numberPicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal" /> 

</LinearLayout>

android:text="@string/textDummyEmpty" 

是一个空字符串。也许只使用视图而不是textView就足够了。


1
@RiThBo请分享你是如何解决这个问题的,这样其他用户也可以做到。 - Dick Lucas
1
@Richard 我用的是CyanogenMod。它在GitHub上某个地方,我找了一下确切的文件但是找不到了。抱歉。 - user2442638
嘿@RiThBo,你是如何解决对话框中蓝色线条一直延伸的问题的? - Etienne Lawlor
@Richard 如果你还有困难,可以看看我的答案。 - user2442638
@RiThBo,你在哪个类中设置那个XML文件? - Etienne Lawlor
显示剩余2条评论

1

在onCreateDialogView中返回LinearLayout而不是以下的NumberPicker:

@Override
protected View onCreateDialogView() {
    numberPicker = new NumberPicker(getContext());
    numberPicker.setMinValue(1);
    numberPicker.setMaxValue(12);
    numberPicker.setWrapSelectorWheel(false);
    numberPicker.setValue(lastValue);

    LinearLayout.LayoutParams pickerParams = new LinearLayout.LayoutParams
            (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    pickerParams.gravity = Gravity.CENTER;
    numberPicker.setLayoutParams(pickerParams);

    LinearLayout layout = new LinearLayout(getContext());
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
            (LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    layout.setOrientation(LinearLayout.VERTICAL);
    layout.setLayoutParams(params);

    layout.addView(numberPicker);
    return layout;

    //return numberPicker;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接