Android-SharedPreferences

幽沉谢世事,俯默窥唐虞。这篇文章主要讲述Android-SharedPreferences相关的知识,希望能为你提供帮助。
在android开发过程中,数据都保存,有种方式就是通过  SharedPreferences,共享首选项
共享首选项SharedPreferences,的作用是可以以键值对应的方式保存数据读取数据,什么时候使用SharedPreferences,当APP需要保存配置信息当时候就可以使用SharedPreferences
 
例如:Settings APP应用里面的很多配置信息就是使用共享首选项来保存的,例如:登录时候的记住用户名,记住密码操作就是使用共享首选项来保存的
    Checkbox控件,RadioButtonk控件,等等都是通过共享首选项来保存的,只要是保存APP应用里面的配置信息,就是用共享首选项来保存的
 
可以以下这种方式创建 共享首选项

/** * 这种方式是Android提供的,只需传入模式,SP的名称就是当前的类名 * @param mode 传入Mode * @return */ @Override public SharedPreferences getPreferences(int mode) { return super.getPreferences(mode); }/** * 这种方式是Android提供的,只需传入模式,SP的名称就是当前的类名 * @param name 传入SP的名称 * @param mode 传入Mode * @return */

 
创建共享首选项介绍:
// 取名为 login_config模式为:私有模式,每次保存覆盖之前保存的信息 sp = getSharedPreferences("login_config", Context.MODE_PRIVATE);

 
保存数据,需要使用到 edit,记得commit():
sp.edit().putString("et_name", etName.getText().toString()) .putBoolean("cb_remember_name", true).commit();

 
只有commit(); 后才会出现:
Android-SharedPreferences

文章图片

【Android-SharedPreferences】 
读取共享首选项数据:
sp.getBoolean("cb_remember_name", false);

 
注意:??注意:?? 要加上这个判断 if (buttonView.isPressed()) ,否则设置 setChecked就会激发这个监听,从而引发问题

package liudeli.datastorage; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; public class MainActivity2 extends Activity {private final String TAG = MainActivity2.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); initView(); initListener(); }private EditText etName; private EditText etPwd; private CheckBox cbRememberName; private CheckBox cbRememberNamePwd; private Button btLogin; private SharedPreferences sp; /** * 这种方式是Android提供的,只需传入模式,SP的名称就是当前的类名 * @param mode 传入Mode * @return */ @Override public SharedPreferences getPreferences(int mode) { return super.getPreferences(mode); }/** * 这种方式是Android提供的,只需传入模式,SP的名称就是当前的类名 * @param name 传入SP的名称 * @param mode 传入Mode * @return */ @Override public SharedPreferences getSharedPreferences(String name, int mode) { return super.getSharedPreferences(name, mode); }private void initView() { etName = findViewById(R.id.et_name); etPwd = findViewById(R.id.et_pwd); cbRememberName = findViewById(R.id.cb_remember_name); cbRememberNamePwd = findViewById(R.id.cb_remember_name_pwd); btLogin = findViewById(R.id.bt_login); // 取名为 login_config模式为:私有模式,每次保存覆盖之前保存的信息 sp = getSharedPreferences("login_config", Context.MODE_PRIVATE); }private void initListener() {btLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {} }); cbRememberName.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // 注意:?? 要加上这个判断,否则设置 setChecked就会激发这个监听,从而引发问题 if (buttonView.isPressed()) {boolean checkName = checkName(); if (cbRememberName.isChecked() & & checkName) { // 保存用户名 sp.edit().putString("et_name", etName.getText().toString()) .putBoolean("cb_remember_name", true).commit(); cbRememberName.setChecked(true); } else { sp.edit().putBoolean("cb_remember_name", false).commit(); cbRememberName.setChecked(false); } } } }); cbRememberNamePwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// 注意:?? 要加上这个判断,否则设置 setChecked就会激发这个监听,从而引发问题 if(buttonView.isPressed()) {boolean checkNamePwd = checkNamePwd(); if (cbRememberNamePwd.isChecked() & & checkNamePwd) { // 保存用户名和密码 sp.edit().putString("et_name", etName.getText().toString()) .putString("et_pwd", etPwd.getText().toString()) .putBoolean("cb_remember_name_pwd", true) .putBoolean("cb_remember_name", true).commit(); cbRememberNamePwd.setChecked(true); cbRememberName.setChecked(true); } else { sp.edit().putBoolean("cb_remember_name_pwd", false).commit(); cbRememberNamePwd.setChecked(false); } } } }); }private boolean checkName() { return !TextUtils.isEmpty(etName.getText().toString()); }private boolean checkNamePwd() { return !TextUtils.isEmpty(etName.getText().toString()) & & !TextUtils.isEmpty(etPwd.getText().toString()); }@Override protected void onResume() { super.onResume(); // 判断是否记住了用户名,如果保存了 或者 没有保存 都更新Checkbox,默认第一次是false booleanrememberName = sp.getBoolean("cb_remember_name", false); Log.d(TAG, "rememberName:" + rememberName); cbRememberName.setChecked(rememberName); Log.d(TAG, "cbRememberName.isChecked():" + cbRememberName.isChecked()); if (cbRememberName.isChecked()) { // 获取用户名保存到EditText etName.setText(sp.getString("et_name", null)); } else{ // 如果记住用户名是false就没有必要往下走了 return; }// 判断是否记住了用户名和密码,如果保存了 或者 没有保存 都更新Checkbox,默认第一次是false booleanrememberNamePwd = sp.getBoolean("cb_remember_name_pwd", false); cbRememberNamePwd.setChecked(rememberNamePwd); if (cbRememberNamePwd.isChecked()) { // 获取用户名保存到EditText etName.setText(sp.getString("et_name", null)); etPwd.setText(sp.getString("et_pwd", null)); } }@Override protected void onDestroy() { super.onDestroy(); }}

 
布局相关代码:
< ?xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> < LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp"> < TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户" /> < EditText android:id="@+id/et_name" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginLeft="6dp" /> < /LinearLayout> < LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp"> < TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码" /> < EditText android:id="@+id/et_pwd" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginLeft="6dp" android:inputType="textPassword" /> < /LinearLayout> < RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp"> < CheckBox android:id="@+id/cb_remember_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记住用户名"/> < CheckBox android:id="@+id/cb_remember_name_pwd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="记住用户名和密码" android:layout_alignParentRight="true" /> < /RelativeLayout> < Button android:id="@+id/bt_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录" /> < /LinearLayout>

 
布局相关:
Android-SharedPreferences

文章图片

 

    推荐阅读