SharedPreferences

来源:互联网 发布:易语言彩票预测源码 编辑:程序博客网 时间:2024/06/06 11:39

SharedPreferences用来保存数据,保存数据到文件

写入数据

  public void savedb(Context c,String username,String password){
      SharedPreferences sp= c.getSharedPreferences("config", Context.MODE_PRIVATE);//参数一是文件的名称
      SharedPreferences.Editor ed=sp.edit();
      ed.putString("username",username);
      ed.putString("password",password);
      ed.commit();
   }


读数据

SharedPreferences sp=this.getSharedPreferences("config",Context.MODE_PRIVATE);//this指的是Context
String username_=sp.getString("username", "");
String password_=sp.getString("password","");


文件保存位置



config.xml的位置位于data/data/[项目路径]/shared_prefs/config.xml


文件路径是

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <string name="password">er4</string>
    <string name="username">uuu</string>
</map>



0 0