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

来源:互联网 发布:淘宝卖家怎么看钱到账 编辑:程序博客网 时间:2024/06/05 08:19
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");

0 0
原创粉丝点击