Android中SharedPreferences与Editor的使用

我们在开发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中的关键代码来更好的说明一下。

注册(写入数据):

[java]  view plain  copy
 print ?
  1. public class RegistrationUI extends Activity {  
  2.       
  3.     private String TAG = "RegistrationUI";  
  4.     SharedPreferences preferences;  
  5.     SharedPreferences.Editor editer;  
  6.       
  7.     private EditText userNameEditTxt;  
  8.     private EditText userPwEditTxt1;  
  9.     private EditText userPwEditTxt2;  
  10.     private Button ok;  
  11.     private Button cancel;  
  12.       
  13.     @Override  
  14.     protected void onCreate(Bundle savedInstanceState) {  
  15.           
  16.         // TODO Auto-generated method stub  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.registration);  
  19.           
  20.         preferences = getSharedPreferences("myinfo", MODE_WORLD_READABLE);  
  21.         editer = preferences.edit();  
  22.           
  23.         userNameEditTxt = (EditText) findViewById(R.id.reg_userNameEditTxt);  
  24.         userPwEditTxt1 = (EditText) findViewById(R.id.reg_userPwEditTxt1);  
  25.         userPwEditTxt2 = (EditText) findViewById(R.id.reg_userPwEditTxt2);  
  26.           
  27.         ok = (Button) findViewById(R.id.determineBn);  
  28.         cancel = (Button) findViewById(R.id.cancelBn);  
  29.           
  30.         ok.setOnClickListener(new OnClickListener() {  
  31.               
  32.             @Override  
  33.             public void onClick(View v) {  
  34.                 // TODO Auto-generated method stub  
  35.                 SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日" + "hh:mm:ss");  
  36.                 editer.putString("date_and_time", sdf.format(new Date()));  
  37.                 String name = userNameEditTxt.getText().toString();  
  38.                 String password1 = userPwEditTxt1.getText().toString();  
  39.                 String password2 = userPwEditTxt2.getText().toString();  
  40.                   
  41.                 if (password1.equals(password2))  
  42.                 {  
  43.                     editer.putString("userName", name);  
  44.                     editer.putString("userPw", password1);  
  45.                     editer.commit();  
  46.                 }  
  47.                 else  
  48.                 {  
  49.                     Toast.makeText(RegistrationUI.this"前后两次输入的密码不一致,请重新输入。。。", Toast.LENGTH_SHORT);  
  50.                 }  
  51.             }  
  52.         });  
  53.           
  54.         cancel.setOnClickListener(new OnClickListener() {  
  55.               
  56.             @Override  
  57.             public void onClick(View v) {  
  58.                 // TODO Auto-generated method stub  
  59.                   
  60.             }  
  61.         });  
  62.     }  
  63. }  

登录(读取数据):

[java]  view plain  copy
 print ?
  1. public class LoginUI extends Activity {  
  2.       
  3.     SharedPreferences preferences;  
  4.     SharedPreferences.Editor editer;  
  5.       
  6.     private EditText userName;  
  7.     private EditText userPw;  
  8.     private Button loginBn;  
  9.     private Button cancelBn;  
  10.       
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         // TODO Auto-generated method stub  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.login);  
  16.           
  17.         preferences = getSharedPreferences("myinfo", MODE_WORLD_READABLE);  
  18.           
  19.         userName = (EditText) findViewById(R.id.login_userNameEditTxt);  
  20.         userPw = (EditText) findViewById(R.id.login_userPwEditTxt);  
  21.         loginBn = (Button) findViewById(R.id.login_determineBn);  
  22.         cancelBn = (Button) findViewById(R.id.login_cancelBn);  
  23.           
  24.         loginBn.setOnClickListener(new OnClickListener() {  
  25.               
  26.             @Override  
  27.             public void onClick(View v) {  
  28.                 // TODO Auto-generated method stub  
  29.                 String userName_DB = preferences.getString("userName"null);  
  30.                 String userPw_DB = preferences.getString("userPw"null);  
  31.                   
  32.                 if (userName_DB.equals(userName.getText().toString()) && userPw_DB.equals(userPw.getText().toString()))  
  33.                 {  
  34.                     Toast.makeText(LoginUI.this"登录成功!", Toast.LENGTH_LONG).show();  
  35.                 }  
  36.                 else  
  37.                 {  
  38.                     Toast.makeText(LoginUI.this"用户名或密码用误,请重新登录。", Toast.LENGTH_LONG).show();  
  39.                 }  
  40.                   
  41.             }  
  42.         });  
  43.           
  44.     }  
  45. }  

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

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

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值