对于大部分初学者来说,如果想利用SharedPreferences进行数据存储的话大部分人(包括本人)应该会这样:
存储:
SharedPreferences sharedPreferences = getSharedPreferences(context.getPackagename(), Context.MODE_PRIVATE); Editor editor = sharedPreferences.edit(); editor.putString("username", "Tom"); editor.putInt("password", 123456); editor.commit();
获取:
SharedPreferences sharedPreferences = getSharedPreferences(context.getPackagename(), Context.MODE_PRIVATE); String username = sharedPreferences.getString("username", ""); int password = sharedPreferences.getInt("password", 0);
虽然这样做代码没多少,如果是对于初学者练习来说无伤大雅。但是如果你是做一个项目开发来说,里面使用了大量的数据存数。每次都将上面的代码一遍一遍的重复,估计能让人崩溃。所以,对于实际项目开发来说,最好能将可以封装的工具类封装就进行封装。
下面是一个封装好的SharedPreferences的工具类,如果你有需要直接引用就OK:
package com.example.utl;import java.util.Map;import androID.content.Context;import androID.content.SharedPreferences;/** * 保存信息配置类 * * @author admin */public class SharedPreferencesHelper { private SharedPreferences sharedPreferences; /* * 保存手机里面的名字 */private SharedPreferences.Editor editor; public SharedPreferencesHelper(Context context,String file_name) { sharedPreferences = context.getSharedPreferences(file_name, Context.MODE_PRIVATE); editor = sharedPreferences.edit(); } /** * 存储 */ public voID put(String key, Object object) { if (object instanceof String) { editor.putString(key, (String) object); } else if (object instanceof Integer) { editor.putInt(key, (Integer) object); } else if (object instanceof Boolean) { editor.putBoolean(key, (Boolean) object); } else if (object instanceof float) { editor.putfloat(key, (float) object); } else if (object instanceof Long) { editor.putLong(key, (Long) object); } else { editor.putString(key, object.toString()); } editor.commit(); } /** * 获取保存的数据 */ public Object getSharedPreference(String key, Object defaultObject) { if (defaultObject instanceof String) { return sharedPreferences.getString(key, (String) defaultObject); } else if (defaultObject instanceof Integer) { return sharedPreferences.getInt(key, (Integer) defaultObject); } else if (defaultObject instanceof Boolean) { return sharedPreferences.getBoolean(key, (Boolean) defaultObject); } else if (defaultObject instanceof float) { return sharedPreferences.getfloat(key, (float) defaultObject); } else if (defaultObject instanceof Long) { return sharedPreferences.getLong(key, (Long) defaultObject); } else { return sharedPreferences.getString(key, null); } } /** * 移除某个key值已经对应的值 */ public voID remove(String key) { editor.remove(key); editor.commit(); } /** * 清除所有数据 */ public voID clear() { editor.clear(); editor.commit(); } /** * 查询某个key是否存在 */ public Boolean contain(String key) { return sharedPreferences.contains(key); } /** * 返回所有的键值对 */ public Map<String, ?> getAll() { return sharedPreferences.getAll(); }}
登录界面:
Activity中应用:
/** * 登录界面 * * @author admin * */public class LoginActivity extends Activity implements OnClickListener { private EditText username; private EditText password; private button login;// 登录按钮 private CheckBox holdPwd;// 保存用户和密码 private MyDialog mydialog;// 自定义对话框 private SharedPreferencesHelper sharedPreferencesHelper; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestwindowFeature(Window.FEATURE_NO_Title); setContentVIEw(R.layout.activity_login); init(); } /** * 初始化界面 */ private voID init() { sharedPreferencesHelper = new SharedPreferencesHelper( LoginActivity.this, "anhua"); username = (EditText) findVIEwByID(R.ID.username); password = (EditText) findVIEwByID(R.ID.password); holdPwd = (CheckBox) findVIEwByID(R.ID.login_hold); holdPwd.setChecked(true);
//取出存储的值在登陆界面中显示 username.setText(sharedPreferencesHelper.getSharedPreference("username", "").toString().trim()); password.setText(sharedPreferencesHelper.getSharedPreference("password", "").toString().trim());
login = (button) findVIEwByID(R.ID.login); login.setonClickListener(this); } @OverrIDe public voID onClick(VIEw v) { // Todo auto-generated method stub switch (v.getID()) { case R.ID.login: login(); break; default: break; } } private voID login() { String name = username.getText().toString().trim(); String pwd = password.getText().toString().trim();
//如果复选框被选中则存储账户和密码 if (holdPwd.isChecked()) { sharedPreferencesHelper.put("username", name); sharedPreferencesHelper.put("password", pwd); sharedPreferencesHelper.put("isremember", holdPwd.isChecked()); } else {
//如果复选框未被选中则清除记录的账号和密码 sharedPreferencesHelper.remove("username"); sharedPreferencesHelper.remove("password"); sharedPreferencesHelper.remove("isremember"); } login.setBackgroundResource(R.drawable.bg_login_submit); login.setTextcolor(getResources().getcolor(R.color.white)); Intent intent = new Intent(LoginActivity.this, MainActivity.class); startActivity(intent); finish(); }}
原文网址:https://www.cnblogs.com/1925yiyi/p/7489367.HTML
总结
以上是内存溢出为你收集整理的Android- SharedPreferences的封装全部内容,希望文章能够帮你解决Android- SharedPreferences的封装所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)