Android SharedPreferences

一、介绍

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效果:
在这里插入图片描述

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值