码迷,mamicode.com
首页 > 其他好文 > 详细

SharedPreferences(共享参数)

时间:2016-05-17 11:31:43      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

//共享参数,多用于自动登录,是 android常用的数据储存的一种,用于储存少量数据,Integer,Double,String等。

public class sharp {
    Context context;                 //接收上下文对象

    public sharp(Context context) { //构造函数
        this.context = context;
    }
//存储数据
    public void read(User user){
        SharedPreferences pre=context.getSharedPreferences("pp",Context.MODE_PRIVATE);          //“pp”为共享文件名
        SharedPreferences.Editor editor=pre.edit();//创建Editor
        editor.putString("id",user.getId());        //把用户名刺入到共享文件里
        editor.putString("pwd",user.getPwd());
        editor.putString("qx",user.getQx());
        editor.commit();
    }
//读取数据
public User write(){ SharedPreferences shp=context.getSharedPreferences("pp",Context.MODE_PRIVATE); String id=shp.getString("name","no");//“no”为默认值 String pwd=shp.getString("pwd","no"); String qx=shp.getString("qx","no"); User user=new User(id,pwd,qx); return user; } }

 

SharedPreferences(共享参数)

标签:

原文地址:http://www.cnblogs.com/Xacm/p/5500518.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!