SP存储

package com.example.shishangyuan;

import android.content.Context;
import android.content.SharedPreferences;


//存储类
public class SharedUtils {
    //存账号的方法
    public static void setName(Context context,String name){ //SharedPreferences存储类 1.参数 键 2.参数 值 SharedPreferences sharedPreferences=context.getSharedPreferences("name",0); //写入Editor类 定义写入对象 SharedPreferences.Editor editor=sharedPreferences.edit(); //1参数 定义的标记 键 2参数 传的值 值 editor.putString("name",name); // 提交 editor.commit(); } //取的方法 public static String getName(Context context){ SharedPreferences sharedPreferences=context.getSharedPreferences("name",0); //1参数 存的时候的一个键 2参数 取出来的值 ,默认值为“”; Null和“” 不一样的 “”一个占内存空间,null一个不占内存空间 return sharedPreferences.getString("name",""); } //存密码 public static void setPwd(Context context,String pwd){ //存储类 SharedPreferences sharedPreferences=context.getSharedPreferences("pwd",0); //写入类 SharedPreferences.Editor editor=sharedPreferences.edit(); editor.putString("pwd",pwd); editor.commit(); } //取的密码方法 public static String getPwd(Context context){ SharedPreferences sharedPreferences=context.getSharedPreferences("pwd",0); return sharedPreferences.getString("pwd",""); } //返回Userid Token 加密用的 账号的钱 昵称 年龄 性别 }

你可能感兴趣的:(SP存储)