Android中的SharedPreferences 增 删 改 查 操作详解
2013-12-01 15:16
393 查看
1.概述。
SharePreferences是用来存储一些简单配置信息的一种机制,使用Map数据结构来存储数据,以键值对的方式存储,采用了XML格式将数据存储到设备中。例如保存登录用户的用户名和密码。只能在同一个包内使用,不能在不同的包之间使用,其实也就是说只能在创建它的应用中使用,其他应用无法使用。
创建的存储文件保存在/data/data/<package name>/shares_prefs文件夹下。
2.使用。
通过Context.getSharedPreferences方法获取SharedPreferences对象,参数分别为存储的文件名和存储模式。
[java] view
plaincopy
// 获取SharedPreferences对象
SharedPreferences sp = getSharedPreferences(DATABASE, Activity.MODE_PRIVATE);
// 获取Editor对象
Editor editor = sp.edit();
3.操作。
SharePreferences存储数据是通过获取Editor编辑器对象来操作的。
插入数据:
调用Editor.putxxxx方法,两个参数分别为键和值。
获取数据:
调用Editor.getxxxx方法,两个参数分别为键和不存在指定键时的默认值。
删除数据:
调用Editor.remove方法,参数为指定的键。
清空所有数据:
调用Editor.clear方法
上述所有方法调用都要执行Editor.commit方法来提交。
下面通过对数据的增删改查来演示下SharePreferences的使用。
程序截图:
布局代码简单 忽略;
程序主代码 MainActivity;
[java] view
plaincopy
/**
* MainActivity
*
* @author zuolongsnail
*/
public class MainActivity extends Activity {
private EditText keyET;
private EditText valueET;
private Button insertBtn;
private Button deleteBtn;
private Button modifyBtn;
private Button queryBtn;
private Button clearBtn;
private TextView textView;
/** 存储的文件名 */
public static final String DATABASE = "Database";
/** 存储后的文件路径:/data/data/<package name>/shares_prefs + 文件名.xml */
public static final String PATH = "/data/data/code.sharedpreferences/shared_prefs/Database.xml";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
keyET = (EditText) findViewById(R.id.key);
valueET = (EditText) findViewById(R.id.value);
insertBtn = (Button) findViewById(R.id.insert);
deleteBtn = (Button) findViewById(R.id.delete);
modifyBtn = (Button) findViewById(R.id.modify);
queryBtn = (Button) findViewById(R.id.query);
clearBtn = (Button) findViewById(R.id.clear);
// 用于显示存储文件中数据
textView = (TextView) findViewById(R.id.content);
insertBtn.setOnClickListener(new OperateOnClickListener());
deleteBtn.setOnClickListener(new OperateOnClickListener());
modifyBtn.setOnClickListener(new OperateOnClickListener());
queryBtn.setOnClickListener(new OperateOnClickListener());
clearBtn.setOnClickListener(new OperateOnClickListener());
}
class OperateOnClickListener implements OnClickListener {
@Override
public void onClick(View v) {
// 获取SharedPreferences对象
SharedPreferences sp = getSharedPreferences(DATABASE,
Activity.MODE_PRIVATE);
// 获取Editor对象
Editor editor = sp.edit();
// 获取界面中的信息
String key = keyET.getText().toString();
String value = valueET.getText().toString();
switch (v.getId()) {
// 插入数据
case R.id.insert:
editor.putString(key, value);
editor.commit();
textView.setText(MainActivity.this.print());
break;
// 删除数据
case R.id.delete:
editor.remove(key);
editor.commit();
textView.setText(MainActivity.this.print());
break;
// 修改数据
case R.id.modify:
editor.putString(key, value);
editor.commit();
textView.setText(MainActivity.this.print());
break;
// 查询数据
case R.id.query:
String result = sp.getString(key, "");
textView.setText("key=" + key + ",value=" + result);
break;
// 清空所有数据
case R.id.clear:
editor.clear();
editor.commit();
textView.setText(MainActivity.this.print());
break;
}
}
}
/** 获取存储文件的数据 */
private String print() {
StringBuffer buff = new StringBuffer();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(PATH)));
String str;
while ((str = reader.readLine()) != null) {
buff.append(str + "/n");
}
} catch (Exception e) {
e.printStackTrace();
}
return buff.toString();
}
}
下面提供一个SharedPreferences工具类,在开发中直接调用即可。
[java] view
plaincopy
/**
* SharedPreferences存储数据方式工具类
* @author zuolongsnail
*/
public class SharedPrefsUtil {
public final static String SETTING = "Setting";
public static void putValue(Context context,String key, int value) {
Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE).edit();
sp.putInt(key, value);
sp.commit();
}
public static void putValue(Context context,String key, boolean value) {
Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE).edit();
sp.putBoolean(key, value);
sp.commit();
}
public static void putValue(Context context,String key, String value) {
Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE).edit();
sp.putString(key, value);
sp.commit();
}
public static int getValue(Context context,String key, int defValue) {
SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
int value = sp.getInt(key, defValue);
return value;
}
public static boolean getValue(Context context,String key, boolean defValue) {
SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
boolean value = sp.getBoolean(key, defValue);
return value;
}
public static String getValue(Context context,String key, String defValue) {
SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
String value = sp.getString(key, defValue);
return value;
}
}
原文出处.http://blog.csdn.net/zuolongsnail/article/details/656703
SharePreferences是用来存储一些简单配置信息的一种机制,使用Map数据结构来存储数据,以键值对的方式存储,采用了XML格式将数据存储到设备中。例如保存登录用户的用户名和密码。只能在同一个包内使用,不能在不同的包之间使用,其实也就是说只能在创建它的应用中使用,其他应用无法使用。
创建的存储文件保存在/data/data/<package name>/shares_prefs文件夹下。
2.使用。
通过Context.getSharedPreferences方法获取SharedPreferences对象,参数分别为存储的文件名和存储模式。
[java] view
plaincopy
// 获取SharedPreferences对象
SharedPreferences sp = getSharedPreferences(DATABASE, Activity.MODE_PRIVATE);
// 获取Editor对象
Editor editor = sp.edit();
3.操作。
SharePreferences存储数据是通过获取Editor编辑器对象来操作的。
插入数据:
调用Editor.putxxxx方法,两个参数分别为键和值。
获取数据:
调用Editor.getxxxx方法,两个参数分别为键和不存在指定键时的默认值。
删除数据:
调用Editor.remove方法,参数为指定的键。
清空所有数据:
调用Editor.clear方法
上述所有方法调用都要执行Editor.commit方法来提交。
下面通过对数据的增删改查来演示下SharePreferences的使用。
程序截图:
布局代码简单 忽略;
程序主代码 MainActivity;
[java] view
plaincopy
/**
* MainActivity
*
* @author zuolongsnail
*/
public class MainActivity extends Activity {
private EditText keyET;
private EditText valueET;
private Button insertBtn;
private Button deleteBtn;
private Button modifyBtn;
private Button queryBtn;
private Button clearBtn;
private TextView textView;
/** 存储的文件名 */
public static final String DATABASE = "Database";
/** 存储后的文件路径:/data/data/<package name>/shares_prefs + 文件名.xml */
public static final String PATH = "/data/data/code.sharedpreferences/shared_prefs/Database.xml";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
keyET = (EditText) findViewById(R.id.key);
valueET = (EditText) findViewById(R.id.value);
insertBtn = (Button) findViewById(R.id.insert);
deleteBtn = (Button) findViewById(R.id.delete);
modifyBtn = (Button) findViewById(R.id.modify);
queryBtn = (Button) findViewById(R.id.query);
clearBtn = (Button) findViewById(R.id.clear);
// 用于显示存储文件中数据
textView = (TextView) findViewById(R.id.content);
insertBtn.setOnClickListener(new OperateOnClickListener());
deleteBtn.setOnClickListener(new OperateOnClickListener());
modifyBtn.setOnClickListener(new OperateOnClickListener());
queryBtn.setOnClickListener(new OperateOnClickListener());
clearBtn.setOnClickListener(new OperateOnClickListener());
}
class OperateOnClickListener implements OnClickListener {
@Override
public void onClick(View v) {
// 获取SharedPreferences对象
SharedPreferences sp = getSharedPreferences(DATABASE,
Activity.MODE_PRIVATE);
// 获取Editor对象
Editor editor = sp.edit();
// 获取界面中的信息
String key = keyET.getText().toString();
String value = valueET.getText().toString();
switch (v.getId()) {
// 插入数据
case R.id.insert:
editor.putString(key, value);
editor.commit();
textView.setText(MainActivity.this.print());
break;
// 删除数据
case R.id.delete:
editor.remove(key);
editor.commit();
textView.setText(MainActivity.this.print());
break;
// 修改数据
case R.id.modify:
editor.putString(key, value);
editor.commit();
textView.setText(MainActivity.this.print());
break;
// 查询数据
case R.id.query:
String result = sp.getString(key, "");
textView.setText("key=" + key + ",value=" + result);
break;
// 清空所有数据
case R.id.clear:
editor.clear();
editor.commit();
textView.setText(MainActivity.this.print());
break;
}
}
}
/** 获取存储文件的数据 */
private String print() {
StringBuffer buff = new StringBuffer();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(PATH)));
String str;
while ((str = reader.readLine()) != null) {
buff.append(str + "/n");
}
} catch (Exception e) {
e.printStackTrace();
}
return buff.toString();
}
}
下面提供一个SharedPreferences工具类,在开发中直接调用即可。
[java] view
plaincopy
/**
* SharedPreferences存储数据方式工具类
* @author zuolongsnail
*/
public class SharedPrefsUtil {
public final static String SETTING = "Setting";
public static void putValue(Context context,String key, int value) {
Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE).edit();
sp.putInt(key, value);
sp.commit();
}
public static void putValue(Context context,String key, boolean value) {
Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE).edit();
sp.putBoolean(key, value);
sp.commit();
}
public static void putValue(Context context,String key, String value) {
Editor sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE).edit();
sp.putString(key, value);
sp.commit();
}
public static int getValue(Context context,String key, int defValue) {
SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
int value = sp.getInt(key, defValue);
return value;
}
public static boolean getValue(Context context,String key, boolean defValue) {
SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
boolean value = sp.getBoolean(key, defValue);
return value;
}
public static String getValue(Context context,String key, String defValue) {
SharedPreferences sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
String value = sp.getString(key, defValue);
return value;
}
}
原文出处.http://blog.csdn.net/zuolongsnail/article/details/656703
相关文章推荐
- (转)【Android游戏开发十二】(保存游戏数据 [上文])详解SharedPreference 与 FIleInputStream/FileOutputStream将数据存储到SD卡中!
- Android 中的数据操作方式:文件操作/SharedPreferences /SQLite
- 【Android游戏开发十二】(保存游戏数据 [上文])详解SharedPreference 与 FIleInputStream/FileOutputStream将数据存储到SD卡中!
- android SharedPreferences 详解与开发实例
- 【Android游戏开发十二】(保存游戏数据 [上文])详解SharedPreference 与 FIleInputS
- android studio sharedpreference基本操作
- 【Android游戏开发十二】(保存游戏数据 [上文])详解SharedPreference 与 FIleInputStream/FileOutputStream将数据存储到SD卡中!
- Android SharedPreferences 详解
- 【Android游戏开发十二】(保存游戏数据 [上文])详解SharedPreference 与 FIleInputStream/FileOutputStream将数据存储到SD卡中!
- 【Android游戏开发十二】(保存游戏数据 [上文])详解SharedPreference 与 FIleInputStream/FileOutputStream将数据存储到SD卡中!
- android sharedpreferences 详解 代码 实例
- 【Android游戏开发十二】(保存游戏数据 [上文])详解SharedPreference 与 FIleInputStream/FileOutputStream将数据存储到SD卡中!
- 【Android游戏开发十二】(保存游戏数据 [上文])详解SharedPreference 与 FIleInputStream/FileOutputStream将数据存储到SD卡中!
- android sharedpreferences 详解 代码 实例
- 【Android游戏开发十二】(保存游戏数据 [上文])详解SharedPreference 与 FIleInputStream/FileOutputStream将数据存储到SD卡中!
- 【Android游戏开发十二】(保存游戏数据 [上文])详解SharedPreference 与 FIleInputStream/FileOutputStream将数据存储到SD卡中!
- Android开发之sharedpreferences 详解
- Android 中关于SharedPreferences 操作模式的设置
- Android开发--详解SharedPreference/PreferenceActivity
- Android开发--详解SharedPreference/PreferenceActivity