带数字选择器的Android首选项活动对话框
我看到了许多针对这个问题的定制解决方案和答案。我需要一些非常简单的东西,我有一个偏好活动,我所需要的是,其中一个选项将打开一个数字选择器对话框,并保存结果。你能一步一步地指导我如何做到这一点吗带数字选择器的Android首选项活动对话框,android,dialog,numberpicker,Android,Dialog,Numberpicker,我看到了许多针对这个问题的定制解决方案和答案。我需要一些非常简单的东西,我有一个偏好活动,我所需要的是,其中一个选项将打开一个数字选择器对话框,并保存结果。你能一步一步地指导我如何做到这一点吗 public class SettingsActivity extends PreferenceActivity { @Override protected void onCreate(final Bundle savedInstanceState) { super
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);
}
}
}
<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:
<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:
<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>
<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>
实现
对话框首选项
是一种解决方案:
<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>
- 这真的很简单
- 或者检查已解决的问题:
对话框首选项
来构建您自己的NumberPickerPreference
<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声明中的属性。要使其正常工作,您需要添加一个attrs.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属性的NumberPicker首选项小部件的完整实现以及演示如何使用它的演示应用程序,请参阅GitHub:
<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>
您可以将该小部件用作任何其他首选项小部件,除非您必须完全限定名称:
<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>
preferences.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>
<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>
基于ListPreference的简单解决方案,动态添加值/条目:
<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>
root_preferences.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>
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
...
<ListPreference
app:key="myNumber"
app:title="my title"
app:useSimpleSummaryProvider="true"/>
以下是我在androidx和kotlin中的做法:
<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>
NumberPickerPreference.kt
NumberPickerPreferenceDialog.kt
设置碎片.kt
settings.xml
希望这有帮助。onDialogClosed应该调用if(callChangeListener(value)){setValue(picker.getValue());}以获得正确的更改通知(注意,Andoird Studio中的股票偏好实现依赖于此更新偏好摘要)。@RobinDavies谢谢,我完全错过了这一点的基础设施,我已经更新了代码。有没有办法让这个选择器“非循环”,即它应该停止在最大值。@SrujanBarai,是的,您可以通过在数字选择器上使用
setWrapSelectorWheel()
。我已更新代码以包含此选项。看一看,您将希望将其设置为false;在ONDIALOG上,在picker.getValue()之前关闭。因此,如果此时有焦点,手动输入的文本更改也将保持不变。非常感谢!它工作得很好!嘿,伙计,这段代码在我的android emulator上运行得很好,但在我的手机上运行得不好我的android emulator使用的是android 7和我的手机android 11,我得到了java.lang.ClassCastException:java.lang.String不能转换为java.lang.Integer,这一行返回getPersistedInt(初始值)。toString()…有什么解决办法吗?好像我必须把那行的值改成整数?
<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>