Android中SharedPreferences与Editor的使用

来源:互联网 发布:中国红十字会知乎 编辑:程序博客网 时间:2024/05/22 01:35

我们在开发Android程序的时候,经常会遇到一些需要保存数据,以妨下次再用到这些数据。

如果不是有接触,我想大家应该比较容易想到的是,数据库(SQLite数据库)和文件存储。其实,在Android开发中,还有三种保存数据的方式,SharedPreferences存储、ContentProvider存储和Network存储。后两种,本篇博客不做详细介绍。

SharedPreferences保存的数据主要是类似配置信息格式的数据,因此它保存的数据主要是简单类型的key-value对。从用法角度来看,SharedPreferences和SharedPreferences.Editor组合起来非常像Map,SharedPreferences负责根据key读取数据,而SharedPreferences.Editor则用写入数据。

SharedPreferences的API超连接。。。。。。。。

SharedPreferences.Editor的API超连接。。。。。。

这里还有一个小问题要大家注意。那就是SharedPreferences存储数据时,只能保存一组数据。它可能有多个key-value对,不过不管它是怎么的多,都只能是一组数据。下面我就给出一个注册和登录小Demo中的关键代码来更好的说明一下。

注册(写入数据):

public class RegistrationUI extends Activity {private String TAG = "RegistrationUI";SharedPreferences preferences;SharedPreferences.Editor editer;private EditText userNameEditTxt;private EditText userPwEditTxt1;private EditText userPwEditTxt2;private Button ok;private Button cancel;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.registration);preferences = getSharedPreferences("myinfo", MODE_WORLD_READABLE);editer = preferences.edit();userNameEditTxt = (EditText) findViewById(R.id.reg_userNameEditTxt);userPwEditTxt1 = (EditText) findViewById(R.id.reg_userPwEditTxt1);userPwEditTxt2 = (EditText) findViewById(R.id.reg_userPwEditTxt2);ok = (Button) findViewById(R.id.determineBn);cancel = (Button) findViewById(R.id.cancelBn);ok.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubSimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日" + "hh:mm:ss");editer.putString("date_and_time", sdf.format(new Date()));String name = userNameEditTxt.getText().toString();String password1 = userPwEditTxt1.getText().toString();String password2 = userPwEditTxt2.getText().toString();if (password1.equals(password2)){editer.putString("userName", name);editer.putString("userPw", password1);editer.commit();}else{Toast.makeText(RegistrationUI.this, "前后两次输入的密码不一致,请重新输入。。。", Toast.LENGTH_SHORT);}}});cancel.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub}});}}

登录(读取数据):

public class LoginUI extends Activity {SharedPreferences preferences;SharedPreferences.Editor editer;private EditText userName;private EditText userPw;private Button loginBn;private Button cancelBn;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.login);preferences = getSharedPreferences("myinfo", MODE_WORLD_READABLE);userName = (EditText) findViewById(R.id.login_userNameEditTxt);userPw = (EditText) findViewById(R.id.login_userPwEditTxt);loginBn = (Button) findViewById(R.id.login_determineBn);cancelBn = (Button) findViewById(R.id.login_cancelBn);loginBn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubString userName_DB = preferences.getString("userName", null);String userPw_DB = preferences.getString("userPw", null);if (userName_DB.equals(userName.getText().toString()) && userPw_DB.equals(userPw.getText().toString())){Toast.makeText(LoginUI.this, "登录成功!", Toast.LENGTH_LONG).show();}else{Toast.makeText(LoginUI.this, "用户名或密码用误,请重新登录。", Toast.LENGTH_LONG).show();}}});}}

在这个例子中,大家可以看到,你在注册时填写的用户名和密码被保存到SharedPreferences中之后,我就可以用这个注册的用户名和密码来登录了。不过,要是用这个来实现注册和登录,只能是单用户的,因为上一次注册的账号会被下一次注册给覆盖掉。也就是说一台机子,一个程序只能跑一个用户。这样太不合理。所以这种存储数据的方式只能是保存一些配置信息(如是否打开音效,是否使用振动效果,小游戏的玩家积分等等)

这里也给出,我上传在CSDN上的程序源码:点击打开源码链接



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 手机突然动不了怎么办 苹果手机密码锁死了怎么办 节目指南无响应怎么办 电信卡网速不好怎么办 网上有人黑我怎么办? 公司前台没事做怎么办 股票跌到谷底了怎么办 cad无法进一步缩小怎么办 dnf更新错误280怎么办 win7电脑屏幕倒过来了怎么办 电脑画面缩小了怎么办 每次客户不付款怎么办 如果客户不付款怎么办 支付宝逾期起诉怎么办 卖号被骗了怎么办 网上交易视频被骗了怎么办 闲鱼卖游戏账号被骗怎么办 网银锁住了怎么办 电脑密码锁住了怎么办 苹果x旋转不了怎么办 苹果x黑白屏怎么办 dell网卡坏了怎么办 笔记本连网速慢怎么办 无线充电太慢怎么办 vivo手机速度慢怎么办 台式电脑很卡怎么办 电脑网卡驱动程序不正常怎么办 电脑没有网卡驱动怎么办 电脑网络网速差怎么办 小米wifi测速慢怎么办 电脑网速特别慢怎么办 戴尔电脑网速慢怎么办 电脑网络特别慢怎么办? 电脑上网很慢怎么办 笔记本网速太慢怎么办 ns下载特别慢怎么办 人人视频弹广告怎么办 忘记华为账号密码怎么办 手机电源键不灵怎么办 智能手机返回键失灵怎么办 手机关闭失灵了怎么办