首页 > 其他 > 详细

SharedPreferences(共享参数)

时间:2016-05-17 11:31:27      阅读:309      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!