1、創建一個SharedPreferencesUtil類
2、聲明:
SharedPreferences sp;
SharedPreferences.Editor edtor;
3、創建構造方法:
public SharedPreferencesUtil(Context context ,String file ){
sp=context.getSharedPreferences(file, context.MODE_PRIVATE);
4、添加方法:
//加入密碼
public void setPassword(String password){
editor.put("password" ,password);
editor.commit();
}
//獲取密碼
public String getPassword(){
return sp.getString("password","");
]
萬用SharedPreferences就寫完了
接下來調用方法:
SharedPreferencesUtil spUtil=new SharedPreferencesUtil(this,"save");
spUtil.setPassword("1234567");//存儲密碼
spUtil.getPassword();//獲取密碼