使用SharedPreferences读取系统设置参数

来源:互联网 发布:国内男士衬衣品牌 知乎 编辑:程序博客网 时间:2024/05/19 10:41

前面在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;@Overrideprotected 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);}}@Overrideprotected 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{@Overrideprotected 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>





原创粉丝点击