您的位置:首页 > Web前端

SharedPreferences简单使用 String类型数据保存和获取

2016-06-05 20:51 351 查看
package com.example.denglujiemian.zhaoi.Utils;
import android.content.Context;
import android.content.SharedPreferences;

public class SharedPreferencesUtils {
    private static final String path ="urse";  

    // 保存用户的密码  
    public static   void setPasswd(Context context,String key,String Value) {
  
SharedPreferences sp = context.getSharedPreferences(path, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();  
        editor.putString(key, Value);  
        editor.commit();  
    }  
    // 获取用户的密码  
    public  static  String getPasswd(Context context,String key) { 
    SharedPreferences sp = context.getSharedPreferences(path, Context.MODE_PRIVATE);  
        return sp.getString(key, null);  
    } 

}

使用方式

// 把数据保存到SharedPreferences里边

SharedPreferencesUtils.setPasswd(getApplicationContext(),"mPhoneCode", mPhoneCode); 
SharedPreferencesUtils.setPasswd(getApplicationContext(),"mPassword", mPassword);


//获取数据

String user_id = SharedPreferencesUtils.getPasswd(mContext,"user_id");
String user_name = SharedPreferencesUtils.getPasswd(mContext,"user_name");
String user_header_img = SharedPreferencesUtils.getPasswd(mContext,"user_header_img");

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐