很多时候我们开发的软件需要向用户提供软件参数设置功能,例如我们常用的QQ,用户可以设置是否允许陌生人添加自己为好友。对于软件配置参数的保存,如果是window软件通常我们会采用ini文件进行保存,如果是j2se应用,我们会采用properties属性文件进行保存。如果是Android应用,我们最适合采用什么方式保存软件配置参数呢?Android平台给我们提供了一个SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数。使用SharedPreferences保存数据,其背后是用xml文件存放数据,文件存放在/data/data/<package name>/shared_prefs目录下:
SharedPreferences sharedPreferences = getSharedPreferences("itcast", Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();//获取编辑器
editor.putString("name", "传智播客");
editor.putInt("age", 4);
editor.commit();//提交修改
生成的itcast.xml文件内容如下:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="name">传智播客</string>
<int name="age" value="4" />
</map>
因为SharedPreferences背后是使用xml文件保存数据,getSharedPreferences(name,mode)方法的第一个参数用于指定该文件的名称,名称不用带后缀,后缀会由Android自动加上。方法的第二个参数指定文件的操作模式,共有四种操作模式,这四种模式前面介绍使用文件方式保存数据时已经讲解过。如果希望SharedPreferences背后使用的xml文件能被其他应用读和写,可以指定Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE权限。
另外Activity还提供了另一个getPreferences(mode)方法操作SharedPreferences,这个方法默认使用当前类不带包名的类名作为文件的名称。
关键代码示例:
首先设计ui
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- RElativelayout是相对布局 -->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
android:textSize="20px"
android:id="@+id/namelov"
/>
<EditText
android:layout_width="200px"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/namelov"
android:layout_alignTop="@id/namelov"
android:layout_marginLeft="10px"
android:id="@+id/name"
/>
</RelativeLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/age"
android:textSize="20px"
android:id="@+id/agelay"
/>
<EditText
android:layout_width="200px"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/agelay"
android:layout_alignTop="@id/agelay"
android:layout_marginLeft="10px"
android:id="@+id/age"
/>
</RelativeLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:layout_width="90px"
android:layout_height="wrap_content"
android:text="@string/set"
android:id="@+id/setbutton"
/>
<Button
android:layout_width="90px"
android:layout_height="wrap_content"
android:text="@string/show"
android:layout_toRightOf="@id/setbutton"
android:layout_alignTop="@id/setbutton"
android:id="@+id/showbutton"
/>
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20px"
android:id="@+id/result"
/>
</LinearLayout>
辅助的string.xml文件代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, SPActivity!</string>
<string name="app_name">SharedPreferences数据</string>
<string name="name">姓名:</string>
<string name="age">年龄:</string>
<string name="set">保存设置</string>
</resources>
设计的activity
package itcast.date;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class SPActivity extends Activity {
private EditText nametext;
private EditText agetext;
private TextView resultview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
nametext=(EditText)this.findViewById(R.id.name);
agetext=(EditText)this.findViewById(R.id.age);
resultview=(TextView)this.findViewById(R.id.result);
Button button=(Button)this.findViewById(R.id.setbutton);
Button showbutton=(Button)this.findViewById(R.id.showbutton);
button.setOnClickListener(listenser);
showbutton.setOnClickListener(listenser);
//回显示功能
SharedPreferences sharedPreferences =SPActivity.this.getSharedPreferences("itcast", Context.MODE_PRIVATE);
String namevalue=sharedPreferences.getString("name", "");
int agevalue=sharedPreferences.getInt("age", 1);
nametext.setText(namevalue);
agetext.setText(String.valueOf(agevalue));
// resultview.setText("姓名:"+namevalue+",年龄:"+agevalue);
}
private View.OnClickListener listenser=new View.OnClickListener() {
@Override
public void onClick(View v) {
Button button=(Button)v;
//获取SharedPreferences对象 参数一 后台生成的xml文件名 参数二 操作模式
SharedPreferences sharedPreferences =SPActivity.this.getSharedPreferences("itcast", Context.MODE_PRIVATE);
switch(button.getId()){
case R.id.setbutton:
String name=nametext.getText().toString();
String age=agetext.getText().toString();
/**
*很多时候我们开发的软件需要向用户提供软件参数设置功能,例如我们常用的QQ,
*用户可以设置是否允许陌生人添加自己为好友。对于软件配置参数的保存,
*如果是window软件通常我们会采用ini文件进行保存,如果是j2se应用
*我们会采用properties属性文件进行保存。如果是Android应用,
*我们最适合采用什么方式保存软件配置参数呢?Android平台给我们提供了一个SharedPreferences类,
*它是一个轻量级的存储类,特别适合用于保存软件配置参数。使用SharedPreferences保存数据,
*其背后是用xml文件存放数据,文件存放在/data/data/<package name>/shared_prefs目录下:
*/
Editor editor = sharedPreferences.edit();//获取编辑器
editor.putString("name", name);
editor.putInt("age", Integer.parseInt(age));
editor.commit();//提交修改
//提示消息
Toast.makeText(SPActivity.this, "保存成功", 1).show();
break;
case R.id.showbutton:
//参数一是name键 参数二 是默认值 这行 表示还回name键的值 没有就还回默认值
String namevalue=sharedPreferences.getString("name", "");
int agevalue=sharedPreferences.getInt("age", 1);
resultview.setText("姓名:"+namevalue+",年龄:"+agevalue);
break;
}
}
};
}