對於大部分初學者來講,若是想利用SharedPreferences進行數據存儲的話大部分人(包括本人)應該會這樣:java
存儲:android
SharedPreferences sharedPreferences = getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE); Editor editor = sharedPreferences.edit(); editor.putString("username", "Tom"); editor.putInt("password", 123456); editor.commit();
獲取:ide
SharedPreferences sharedPreferences = getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE); String username = sharedPreferences.getString("username", ""); int password = sharedPreferences.getInt("password", 0);
雖然這樣作代碼沒多少,若是是對於初學者練習來講無傷大雅。可是若是你是作一個項目開發來講,裏面使用了大量的數據存數。每次都將上面的代碼一遍一遍的重複,估計能讓人崩潰。因此,對於實際項目開發來講,最好能將能夠封裝的工具類封裝就進行封裝。工具
下面是一個封裝好的SharedPreferences的工具類,若是你有須要直接引用就OK:this
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(); } }
登陸界面:spa
Activity中應用:3d
/** * 登陸界面 * * @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(); } }