带数字select器的Android PreferenceActivity对话框

我看到了很多定制的解决scheme和对这个问题的答案。 我需要的东西很简单,我有一个偏好活动,我所需要的是,其中一个选项将打开与数字select器的对话框,并保存结果。 你能指导我一步一步如何做到这一点?

public class SettingsActivity extends PreferenceActivity { @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit(); //requestWindowFeature(Window.FEATURE_NO_TITLE); } public static class MyPreferenceFragment extends PreferenceFragment { @Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.prefs); } } } 

XML:

  <SwitchPreference android:key="cross" android:summaryOff="Cross is invisible" android:summaryOn="Cross is visible" android:switchTextOff="OFF" android:switchTextOn="ON" android:title="Cross" android:defaultValue="true"/> <SwitchPreference android:key="autoP" android:summaryOff="App will go to sleep" android:summaryOn="App will not go to sleep" android:switchTextOff="OFF" android:switchTextOn="ON" android:title="Always On" android:defaultValue="true"/> <SwitchPreference android:key="tempD" android:summaryOff="Temprature not displayed" android:summaryOn="Temprature displayed" android:switchTextOff="OFF" android:switchTextOn="ON" android:title="Tempature Display" android:defaultValue="true"/> <ListPreference android:entries="@array/units" android:entryValues="@array/lunits" android:key="listUnits" android:summary="Units schosssing" android:title="Units" android:defaultValue="C"/> <!--Need to add button to open dialog--> </PreferenceScreen> 

号码选取器XML:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <NumberPicker android:id="@+id/numberPicker1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="64dp" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/numberPicker1" android:layout_marginLeft="20dp" android:layout_marginTop="98dp" android:layout_toRightOf="@+id/numberPicker1" android:text="Cancel" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button2" android:layout_alignBottom="@+id/button2" android:layout_marginRight="16dp" android:layout_toLeftOf="@+id/numberPicker1" android:text="Set" /> </RelativeLayout> 

Solutions Collecting From Web of "带数字select器的Android PreferenceActivity对话框"

子类DialogPreference构build您自己的NumberPickerPreference

我为你实施了一个。 它工作得很好,但function不完整。 例如,最小值和最大值是硬编码的常量。 这些应该真的是偏好xml声明的属性。 为了达到这个目的,你需要添加一个attrs.xml文件来指定你的自定义属性。

有关在库项目中支持自定义xml属性的NumberPicker首选项小部件的完整实现以及演示如何使用它的演示应用程序,请参阅GitHub: https : //github.com/Alobar/AndroidPreferenceTest

除了必须完全限定名称之外,您将使用该小部件作为其他首选小部件:

的preferences.xml

 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <com.example.preference.NumberPickerPreference android:key="key_number" android:title="Give me a number" android:defaultValue="55" /> </PreferenceScreen> 

NumberPickerPreference.java

 package com.example.preference; import android.content.Context; import android.content.res.TypedArray; import android.preference.DialogPreference; import android.util.AttributeSet; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; import android.widget.NumberPicker; /** * A {@link android.preference.Preference} that displays a number picker as a dialog. */ public class NumberPickerPreference extends DialogPreference { // allowed range public static final int MAX_VALUE = 100; public static final int MIN_VALUE = 0; // enable or disable the 'circular behavior' public static final boolean WRAP_SELECTOR_WHEEL = true; private NumberPicker picker; private int value; public NumberPickerPreference(Context context, AttributeSet attrs) { super(context, attrs); } public NumberPickerPreference(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected View onCreateDialogView() { FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); layoutParams.gravity = Gravity.CENTER; picker = new NumberPicker(getContext()); picker.setLayoutParams(layoutParams); FrameLayout dialogView = new FrameLayout(getContext()); dialogView.addView(picker); return dialogView; } @Override protected void onBindDialogView(View view) { super.onBindDialogView(view); picker.setMinValue(MIN_VALUE); picker.setMaxValue(MAX_VALUE); picker.setWrapSelectorWheel(WRAP_SELECTOR_WHEEL); picker.setValue(getValue()); } @Override protected void onDialogClosed(boolean positiveResult) { if (positiveResult) { picker.clearFocus(); int newValue = picker.getValue(); if (callChangeListener(newValue)) { setValue(newValue); } } } @Override protected Object onGetDefaultValue(TypedArray a, int index) { return a.getInt(index, MIN_VALUE); } @Override protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) { setValue(restorePersistedValue ? getPersistedInt(MIN_VALUE) : (Integer) defaultValue); } public void setValue(int value) { this.value = value; persistInt(this.value); } public int getValue() { return this.value; } } 

实现DialogPreference是一个解决scheme:

  • 用于Android的NumberPicker首选项对话框非常简单。
  • 或者检查已解决的问题: 优先创buildNumberPicker对话框