一、介绍
SharedPreferences 是 Android 平台上用于存储应用程序的轻量级键值对数据的 API。它可以用来存储简单的配置信息、用户偏好设置等数据,以便应用程序在下次运行时可以快速访问和使用这些数据。数据存储在/ data/data/your_app_package_name/shared_prefs /下,可供整个应用程序的所有Activity访问
二、使用
传入两个参数,一个name,文件名;另一个是指定操作的模式,该方法返回一个SharedPreference对象:
MODE_PRIVATE:默认操作模式,只有本应用程序才可以对这个SharedPreferences 文件进行读写。
MODE_WORLD_READABLE:其他应用对这个 SharedPreferences 文件只能读不能修改。
MODE_WORLD_WRITEABLE:这个 SharedPreferences 文件能被其他的应用读写。
MODE_MULTI_PROCESS:这个模式在 Android2.3 之后已经弃之不用了,可以省略。
读:
调用SharedPreferences的getXXX方法,第一个参数为key,第二个参数为默认值
实例:boolean autologin = sp1.getBoolean("autologin", false);
写:
参数为key和value
sp1.edit().putBoolean("rememberPassword",true).apply();
三、Demo(自动登录和记住密码)
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private EditText etUsername;
private EditText etPassword;
private CheckBox cbRememberPassword;
private CheckBox cbAutoLogin;
private Button btnLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etUsername=(EditText)findViewById(R.id.etUsername);
etPassword=(EditText)findViewById(R.id.etPassword);
cbRememberPassword=(CheckBox)findViewById(R.id.cbRememberPassword);
cbAutoLogin=(CheckBox)findViewById(R.id.cbAutoLogin);
//获取SharedPreferences对象
SharedPreferences sp1=getSharedPreferences("login",MODE_PRIVATE);
findViewById(R.id.btnLogin).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username=etUsername.getText().toString();
String password=etPassword.getText().toString();
//账号 abc 密码 123
if("abc".equals(username)&&"123".equals(password)){
if(cbRememberPassword.isChecked()){
//记住密码
sp1.edit().putBoolean("rememberPassword",true).apply();
}else{
sp1.edit().putBoolean("rememberPassword",false).apply();
}
if(cbAutoLogin.isChecked()){
sp1.edit().putBoolean("autologin",true).apply();
}else {
sp1.edit().putBoolean("autologin",false).apply();
}
Intent intent=new Intent(MainActivity.this,NextActivity.class);
startActivity(intent);
}
}
});
//如果自动登录,直接跳转到下一个页面
boolean autologin = sp1.getBoolean("autologin", false);
if(autologin){
Intent intent=new Intent(MainActivity.this,NextActivity.class);
startActivity(intent);
}
boolean rememberPassword=sp1.getBoolean("rememberPassword",false);
if(rememberPassword){
cbRememberPassword.setChecked(true);
etUsername.setText("abc");
etPassword.setText("123");
}
}
}
activity_main.xml:
<!-- activity_login.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/etUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="账号" />
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:inputType="textPassword" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/cbRememberPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码" />
<CheckBox
android:id="@+id/cbAutoLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动登录" />
</LinearLayout>
<Button
android:id="@+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录" />
</LinearLayout>
UI效果: