使用SharedPreferences读取系统设置参数
2013-07-02 20:09
260 查看
前面在LauncherActivity、PreferenceActivity、ExpandableListActivity的综合运用一文中简要介绍了PreferenceActivity的使用,本文接着使用Preference进行参数设置,并使用SharedPreferences读取保存后的数据,代码如下:
MainActivity:
package com.lovo; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class PreferencesTestActivity extends Activity { // 声明SharedPreferences 对象 private SharedPreferences sp; private TextView show; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_preferences_test); show = (TextView) findViewById(R.id.activity_preferences_test_tv_show); // 获得只能被本应用程序读、写的SharedPreferences对象 sp = this.getSharedPreferences("com.lovo_preferences", MODE_PRIVATE); } public void click(View view) { switch (view.getId()) { case R.id.activity_preferences_test_btn_set: Intent intent = new Intent(PreferencesTestActivity.this, SetPreferencesActivity.class); startActivityForResult(intent, 0); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // 根据键读取值 String ring = sp.getString("ring", null); String name = sp.getString("name", null); String sex = sp.getString("sex", null); boolean systemSet = sp.getBoolean("systemSet", false); String str = (systemSet == true) ? "开启" : "关闭"; show.setText("铃声为:" + ring + "\n" + "用户名为:" + name + "\n" + "性别为:" + sex + "\n" + "自动保存进度:" + str + "\n"); } }
布局XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/activity_preferences_test_btn_set" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click" android:text="设置" /> <TextView android:id="@+id/activity_preferences_test_tv_show" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
PreferencesActivity:
package com.lovo.activity; import com.lovo.lesson13.R; import android.os.Bundle; import android.preference.PreferenceActivity; public class SetPreferencesActivity extends PreferenceActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.test_pref_main); } }
PreferencesXML(test_pref_main):
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 铃声设置 android:key="ring_key" - 设置保存信息的键 android:ringtoneType="all" - 铃声类型,可选值有电话铃声、警告音、通知音 android:showDefault="false" - 是否显示默认铃声 android:showSilent="false" - 是否显示静音选项 android:title="设置铃声" - 显示在设置中的标题 android:summary="选择铃声(test)" - 显示在标题下的概要信息 --> <RingtonePreference android:key="ring" android:ringtoneType="all" android:showDefault="true" android:showSilent="true" android:summary="设置铃声(test)" android:title="设置铃声" /> <!-- 分组标题 --> <PreferenceCategory android:title="个人信息设置" > <!-- 可编辑选项 android:dialogTitle="你输入的用户名为:" - 弹出窗口上的标题 --> <EditTextPreference android:dialogTitle="请输入用户名" android:key="name" android:summary="这里是输入用户名的选项" android:title="用户名" /> <!--单选列表 android:entries="@array/sexAry" - 显示在列表上的名称 android:entryValues="@array/sexValueAry" - 保存的值 --> <ListPreference android:dialogTitle="请输入性别" android:entries="@array/sexAry" android:entryValues="@array/sexValueAry" android:key="sex" android:summary="这里是输入性别的选项" android:title="性别" > </ListPreference> </PreferenceCategory> <PreferenceCategory android:title="系统功能组" > <!-- 复选框设置 android:defaultValue="true" - 是否默认选中 android:summaryOff="自动关闭 " - 没选中时的显示信息 android:summaryOn="自动开启" - 选中时的显示信息 --> <CheckBoxPreference android:defaultValue="true" android:key="systemSet" android:summaryOff="自动关闭 " android:summaryOn="自动开启" android:title="自动保存进度" > </CheckBoxPreference> </PreferenceCategory> </PreferenceScreen>
数组XML:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="sexAry">
<item>男</item>
<item>女</item>
</string-array>
<string-array name="sexValueAry">
<item>male</item>
<item>female</item>
</string-array>
</resources>
相关文章推荐
- 使用ADB更改系统设置中的参数 如背光亮度、休眠时间等
- JavaScript使用cookie,参数设置,说明;读取,写入,删除函数
- 织梦CMS安装使用教程 系统基本参数设置之附件设置
- JavaScript使用cookie,参数设置,说明;读取,写入,删除函数
- [转]JavaScript使用cookie,参数设置,说明;读取,写入,删除函数
- JavaScript使用cookie,参数设置,说明;读取,写入,删除函数
- 织梦CMS安装使用教程 系统基本参数设置之附件设置
- 使用ADB更改系统设置中的参数 如背光亮度、休眠时间等
- JavaScript使用cookie,参数设置,说明;读取,写入,删除函数
- Ubuntu系统使用ifconfig ,route命令手动设置网络接口参数
- JavaScript使用cookie,参数设置,说明;读取,写入,删除函数 【转】
- JavaScript使用cookie,参数设置,说明;读取,写入,删除函数
- 【11gR2】使用runfixup.sh脚本彻底告别曾经琐碎的系统参数设置步骤
- 关于使用PyTorch设置多线程(threads)进行数据读取而导致GPU显存始终不释放的问题
- 反射机制中使用JavaBean来模拟Hibernate或Ibatis设置参数
- 使用Python设置系统时间(给系统校时)
- linux chkconfig命令参数及用法详解--linux系统服务设置命令
- java 读取资源文件并且动态设置资源里面的参数demo
- Keras中使用LSTM层时设置的units参数是什么
- 使用js 设置url参数