【发布时间】:2013-11-29 01:56:08
【问题描述】:
我是 android 开发的新手,试图了解我们如何保存默认首选项。我有以下设置布局文件:
<RadioGroup android:id="@+id/rg"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rb1" android:checked="true"
android:text="RadioButton1">
</RadioButton>
<RadioButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rb2" android:text="RadioButton2" android:checked="true">
</RadioButton>
<RadioButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rb3" android:text="RadioButton3">
</RadioButton>
</RadioGroup>
<CheckBox
android:id="@+id/ch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chk_ios" />
<CheckBox
android:id="@+id/ch2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chk_android"
android:checked="true" />
<CheckBox
android:id="@+id/ch3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chk_windows" />
我希望当用户第一次打开应用程序并进入设置视图时,默认情况下应该检查 rb1 和 ch1。我在 main.java 文件中编写了以下代码:
PreferenceManager.setDefaultValues(this, R.xml.pref, false);
我的问题是如何为上面创建 pref.xml 文件?
到目前为止我有这个:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference android:title="Function"
android:defaultValue="true"
android:summary="ch1 should be default checked"
android:key="functionDefault" />
</PreferenceScreen>
xml 文件中复选框的必填字段是什么?还有如何为单选按钮创建 pref.xml?
【问题讨论】:
-
您是否考虑过以编程方式设置默认首选项值?我的意思是在 Java 代码中?
-
@Pro.metal - 我该怎么做?我在设置视图中设置这些值。但是我想在打开应用程序后立即设置默认值。如果我确实在 java 代码中设置它,我是否必须在设置视图和主视图上复制代码?
标签: android eclipse sharedpreferences