From: http://xys289187120.blog.51cto.com/3361352/656784/
- <?xml version="1.0" encoding="utf-8"?>
- <PreferenceScreen
- xmlns:android="http://schemas.android.com/apk/res/android">
- <PreferenceCategory android:title="CheckBoxPreference">
- <CheckBoxPreference android:key="checkbox_0"
- android:title="CheckBox_A"
- android:summary="这是一个勾选框A" >
- </CheckBoxPreference>
- <CheckBoxPreference android:key="checkbox_1"
- android:title="CheckBox_B"
- android:summary="这是一个勾选框B" >
- </CheckBoxPreference>
- </PreferenceCategory>
- </PreferenceScreen>
![](https://i-blog.csdnimg.cn/blog_migrate/8446413b30b5bf5038b0d6d01b1ce4a1.jpeg)
- import android.content.Context;
- import android.os.Bundle;
- import android.preference.CheckBoxPreference;
- import android.preference.Preference;
- import android.preference.PreferenceActivity;
- import android.preference.Preference.OnPreferenceChangeListener;
- import android.preference.Preference.OnPreferenceClickListener;
- import android.widget.Toast;
-
- public class CheckBoxActivity extends PreferenceActivity {
-
- Context mContext = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // 从资源文件中添Preferences ,选择的值将会自动保存到SharePreferences
- addPreferencesFromResource(R.xml.checkbox);
-
- mContext = this;
-
- //CheckBoxPreference组件
- CheckBoxPreference mCheckbox0 = (CheckBoxPreference) findPreference("checkbox_0");
- mCheckbox0.setOnPreferenceClickListener(new OnPreferenceClickListener() {
-
- @Override
- public boolean onPreferenceClick(Preference preference) {
- //这里可以监听到这个CheckBox 的点击事件
- return true;
- }
- });
-
- mCheckbox0.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
-
- @Override
- public boolean onPreferenceChange(Preference arg0, Object newValue) {
- //这里可以监听到checkBox中值是否改变了
- //并且可以拿到新改变的值
- Toast.makeText(mContext, "checkBox_0改变的值为" + (Boolean)newValue, Toast.LENGTH_LONG).show();
- return true;
- }
- });
-
- CheckBoxPreference mCheckbox1 = (CheckBoxPreference) findPreference("checkbox_1");
- mCheckbox1.setOnPreferenceClickListener(new OnPreferenceClickListener() {
-
- @Override
- public boolean onPreferenceClick(Preference preference) {
- //这里可以监听到这个CheckBox 的点击事件
- return true;
- }
- });
-
- mCheckbox1.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
-
- @Override
- public boolean onPreferenceChange(Preference arg0, Object newValue) {
- //这里可以监听到checkBox中值是否改变了
- //并且可以拿到新改变的值
- Toast.makeText(mContext, "checkBox_1改变的值为" + (Boolean)newValue, Toast.LENGTH_LONG).show();
- return true;
- }
- });
-
- }
-
- }
- <?xml version="1.0" encoding="utf-8"?>
- <PreferenceScreen
- xmlns:android="http://schemas.android.com/apk/res/android">
- <PreferenceCategory android:title="EditTextPreference">
- <EditTextPreference android:key="edit_0"
- android:title="输入信息_A"
- android:summary="请输入您的信息"
- android:defaultValue="请输入信息"
- android:dialogTitle="输入框">
- </EditTextPreference>
-
- <EditTextPreference android:key="edit_1"
- android:title="输入信息_B"
- android:summary="请输入您的信息"
- android:defaultValue="请输入信息"
- android:dialogTitle="输入框">
- </EditTextPreference>
- </PreferenceCategory>
- </PreferenceScreen>
- import android.content.Context;
- import android.os.Bundle;
- import android.preference.EditTextPreference;
- import android.preference.PreferenceActivity;
- public class EditTextActivity extends PreferenceActivity {
- Context mContext = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // 从资源文件中添Preferences ,选择的值将会自动保存到SharePreferences
- addPreferencesFromResource(R.xml.edittext);
- mContext = this;
- // EditTextPreference组件
- EditTextPreference mEditText = (EditTextPreference) findPreference("edit_0");
- //设置dialog按钮信息
- mEditText.setPositiveButtonText("确定");
- mEditText.setNegativeButtonText("取消");
- //设置按钮图标
- mEditText.setDialogIcon(R.drawable.jay);
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string-array name="auto_logout_time_key">
- <item>10 mins.</item>
- <item>20 mins.</item>
- <item>30 mins.</item>
- <item>60 mins.</item>
- </string-array>
- <string-array name="auto_logout_time_value">
- <item>600000</item>
- <item>1200000</item>
- <item>1800000</item>
- <item>3600000</item>
- </string-array>
- </resources>
- <?xml version="1.0" encoding="utf-8"?>
- <PreferenceScreen
- xmlns:android="http://schemas.android.com/apk/res/android">
- <PreferenceCategory android:title="ListPreference">
- <ListPreference
- android:key="list_0"
- android:title="登录设置A"
- android:dialogTitle="选择在线时间"
- android:entries="@array/auto_logout_time_key"
- android:entryValues="@array/auto_logout_time_value" >
- </ListPreference>
- <ListPreference
- android:key="list_0"
- android:title="登录设置A"
- android:dialogTitle="选择在线时间"
- android:entries="@array/auto_logout_time_key"
- android:entryValues="@array/auto_logout_time_value" >
- </ListPreference>
- </PreferenceCategory>
- </PreferenceScreen>
- import android.os.Bundle;
- import android.preference.PreferenceActivity;
- public class ListActivity extends PreferenceActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // 从资源文件中添Preferences ,选择的值将会自动保存到SharePreferences
- addPreferencesFromResource(R.xml.list);
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <PreferenceScreen
- xmlns:android="http://schemas.android.com/apk/res/android">
- <PreferenceCategory android:title="RingtonePreference">
- <RingtonePreference
- android:key="ringtone_0"
- android:summary="选择系统铃声A"
- android:title="铃声设置"
- android:ringtoneType="all"
- android:showSilent="true" ></RingtonePreference>
- <RingtonePreference
- android:key="ringtone_!"
- android:summary="选择系统铃声B"
- android:title="铃声设置"
- android:ringtoneType="all"
- android:showSilent="true" ></RingtonePreference>
- </PreferenceCategory>
- </PreferenceScreen>
![](https://i-blog.csdnimg.cn/blog_migrate/23190f5464adf8c4a6fd4f3d9c8252e6.jpeg)
- import android.os.Bundle;
- import android.preference.PreferenceActivity;
-
- public class RingtoneActivity extends PreferenceActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // 从资源文件中添Preferences ,选择的值将会自动保存到SharePreferences
- addPreferencesFromResource(R.xml.ringtone);
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#00000000">
- <LinearLayout
- android:gravity="center_vertical"
- android:background="@drawable/preference_mid_background"
-
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- >
- <ImageView
- android:focusable="false"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:src="@drawable/setting_about_us">
- </ImageView>
- <RelativeLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="15dip"
- android:layout_marginTop="6dip"
- android:layout_marginRight="6dip"
- android:layout_marginBottom="6dip"
- android:layout_weight="1"
- >
- <TextView
- android:textSize="15dip"
- android:textColor="#000000"
- android:ellipsize="marquee"
- android:id="@+android:id/title"
- android:fadingEdge="horizontal"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:singleLine="true"
- >
- </TextView>
- <TextView
- android:textAppearance="?android:attr/textAppearanceSmall"
- android:textColor="#565656"
- android:id="@+android:id/summary"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:maxLines="4"
- android:layout_below="@+android:id/title"
- android:layout_alignLeft="@+android:id/title"
- >
- </TextView>
- </RelativeLayout>
- <ImageView
- android:focusable="false"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/preference_arrows"/>
- </LinearLayout>
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <selector
- xmlns:android="http://schemas.android.com/apk/res/android">
- <item
- android:state_focused="true"
- android:drawable="@drawable/preference_mid_pressed"
- >
- </item>
- <item
- android:state_pressed="true"
- android:drawable="@drawable/preference_mid_pressed"
- >
- </item>
- <item
-
- android:drawable="@drawable/preference_mid"
- >
- </item>
-
- </selector>
- import android.content.Context;
- import android.os.Bundle;
- import android.preference.Preference;
- import android.preference.PreferenceActivity;
- import android.preference.Preference.OnPreferenceClickListener;
- import android.widget.Toast;
-
- public class AllActivity extends PreferenceActivity {
-
- /**自定义布局A**/
- Preference preference0 = null;
-
- /**自定义布局B**/
- Preference preference1 = null;
-
- Context mContext = null;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // 从资源文件中添Preferences ,选择的值将会自动保存到SharePreferences
- addPreferencesFromResource(R.xml.all);
- mContext = this;
-
- preference0 = findPreference("pref_key_0");
-
- preference0.setOnPreferenceClickListener(new OnPreferenceClickListener() {
-
- @Override
- public boolean onPreferenceClick(Preference preference) {
- Toast.makeText(mContext, "自定义布局A被按下", Toast.LENGTH_LONG).show();
- return false;
- }
- });
- preference1 = findPreference("pref_key_1");
-
- preference1.setOnPreferenceClickListener(new OnPreferenceClickListener() {
-
- @Override
- public boolean onPreferenceClick(Preference preference) {
- Toast.makeText(mContext, "自定义布局B被按下", Toast.LENGTH_LONG).show();
- return false;
- }
- });
- }
- }
- SharedPreferences prefs =PreferenceManager.getDefaultSharedPreferences(this) ;
- boolean something = prefs.getBoolean("something",false);
- <?xml version='1.0' encoding='utf-8' standalone='yes' ?>
- <map>
- <string name="ringtone_!">content://media/external/audio/media/1</string>
- <string name="ringtone_0">content://media/external/audio/media/1</string>
- <string name="list_0">1800000</string>
- <string name="edit_1">请输入信息1212</string>
- <string name="list">1200000</string>
- <string name="ringtone">content://settings/system/ringtone</string>
- <boolean name="checkbox_0" value="true" />
- <boolean name="checkbox_1" value="true" />
- <string name="edit_0">请输入信息</string>
- </map>