开发步骤:
- 创建一个SharedPerferences接口对象,并使用其putString方法放入相关的公共数据
- 将验证通过的注册账号写入到该文件中
- 将数据进行提交
- 给出客户提示
1 //Register_Activity.java 2 3 if (flag) { 4 SharedPreferences.Editor editor=getSharedPreferences("publicData",MODE_PRIVATE).edit();//创建一个SharedPerferences接口对象 5 editor.putString("account", txtRegAccount.getText().toString().trim());//使用SharedPerferences接口对象的putString方法放入相关的公共数据 6 editor.commit(); //将数据进行提交 7 Toast.makeText(getApplicationContext(), "新用户注册成功!",Toast.LENGTH_LONG).show(); //给出客户提示 8 9 Intent intent = getIntent(); 10 intent.setClass(Register_Activity.this, LoginActivity.class); 11 startActivity(intent); 12 }
在登陆时,要获取SharedPerferences中存在的公共账号数据,代码如下:
1 //loginActivity.java 2 SharedPreferences preferences = getSharedPreferences("publicData",MODE_WORLD_READABLE); 3 String account= preferences.getString("account",""); 4 this.txtAccount.setText(account);
运行:
验证数据文件:
(1)通过DDMS找到SharedPerferences设置的数据存储文件publicData
(2)通过File Explorer的导出功能导出publicData.xml
(3)用记事本打开publicdata.xml,验证其内容