SharedPreferences

来源:互联网 发布:知君本无邪txt 编辑:程序博客网 时间:2024/05/17 01:00
public void saveuserinfo(Context context , String username , String password){
//第一个参数是创建xml的文件名,第二个参数是读取方式
SharedPreferences  sp = context.getSharedPreferences("userinfo", Context.MODE_PRIVATE);
Editor editor = sp.edit();//初始化SharedPreferences
editor.putString("username", username);
editor.putString("password", password);
editor.commit();//提交执行,相当于执行xml创建

}

//*******************************读取xml文件


private void showinfo(){
SharedPreferences sp = getSharedPreferences("userinfo", MODE_PRIVATE);
Map<String, ?>  userinfo= sp.getAll();
String username = (String) userinfo.get("username");
String password = (String) userinfo.get("password");
ATusername.setText(username);
ATpassword.setText(password);
}

//***************************************************生成的xml文件格式 在data/data/包名下找到该文件

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <string name="password">11fdsafdsafq</string>
    <string name="username">Eran Lasser</string>
</map>

0 0
原创粉丝点击