您的位置:首页 > Web前端

注册界面设计及实现之(三)SharedPerferences实现数据暂存

2015-12-30 09:36 323 查看
开发步骤:

创建一个SharedPerferences接口对象,并使用其putString方法放入相关的公共数据

将验证通过的注册账号写入到该文件中

将数据进行提交

给出客户提示

//Register_Activity.java

if (flag) {
SharedPreferences.Editor editor=getSharedPreferences("publicData",MODE_PRIVATE).edit();//创建一个SharedPerferences接口对象
editor.putString("account", txtRegAccount.getText().toString().trim());//使用SharedPerferences接口对象的putString方法放入相关的公共数据
editor.commit();   //将数据进行提交
Toast.makeText(getApplicationContext(), "新用户注册成功!",Toast.LENGTH_LONG).show();  //给出客户提示

Intent intent = getIntent();
intent.setClass(Register_Activity.this, LoginActivity.class);
startActivity(intent);
}


在登陆时,要获取SharedPerferences中存在的公共账号数据,代码如下:

//loginActivity.java
SharedPreferences preferences = getSharedPreferences("publicData",MODE_WORLD_READABLE);
String account= preferences.getString("account","");
this.txtAccount.setText(account);


运行:





验证数据文件:

(1)通过DDMS找到SharedPerferences设置的数据存储文件publicData



(2)通过File Explorer的导出功能导出publicData.xml



(3)用记事本打开publicdata.xml,验证其内容

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: