• Android学习笔记 44. SP数据存储


    Android学习笔记

    Android基础开发——数据存储

    44. SP数据存储

    44.1 数据存储是什么

    创建一个新工程

    在这里插入图片描述

    数据保存到APP本身。

    44.2 数据存储有哪些

    SP 、 SQLite 【原生】、 Room【更简洁】

    记住用户名、自动登录、看了书的页数…配置信息 → SP

    44.3 SP特点介绍

    sharedpreference 首选项

    存储软件的配置信息: window → ini 、 Android → xml

    自动登录、记住密码、主题记录等

    首选项不能记录太多的信息,特点:当程序运行首选项里面的数据会全部加载进内存【很小、很简单的数据可以保存到首选项 SP 中】

    运行项目

    在这里插入图片描述

    44.4 SP简单使用

    布局

    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
        
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="SaveToSP"
            android:text="保存到SP"
            />
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="getSPdata"
            android:text="从SP得到数据"
            />
        
    LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    package com.dingjiaxiong.myshujucunchu;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.Context;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.view.View;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
    
        /***
         *     参数1:SP的名字
         *     参数2:SP保存时用的模式,① 常规:每次保存都更新 ② 追加(每次都追加到后面)
         *     @Override
         *     public SharedPreferences getSharedPreferences(String name, int mode) {
         *         return mBase.getSharedPreferences(name, mode);
         *     }
         *
         * 保存到SP
         * @param view
         */
        public void SaveToSP(View view) {
            SharedPreferences sharedPreferences = getSharedPreferences("SPName", Context.MODE_PRIVATE);///常规模式
            sharedPreferences.edit().putString("dingjiaxiong","Android").apply();  //apply后才会写入
    
        }
    
        //从SP获取数据
        public void getSPdata(View view) {
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    运行

    在这里插入图片描述

    在这里插入图片描述

    成功

    如何获取

    //从SP获取数据
    public void getSPdata(View view) {
        SharedPreferences sharedPreferences = getSharedPreferences("SPName", Context.MODE_PRIVATE);
        String string = sharedPreferences.getString("dingjiaxiong", "默认值");
        Toast.makeText(this,"" + string,Toast.LENGTH_SHORT).show();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    运行

    在这里插入图片描述

    44.5 SP真实实战

    布局

    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名" />
    
        <EditText
            android:id="@+id/name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码" />
    
        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:password="true" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <CheckBox
                android:id="@+id/remember_pwd"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="记住密码" />
    
            <CheckBox
                android:id="@+id/auto_login"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="自动登录" />
    
        LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <Button
                android:id="@+id/register"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="20dp"
                android:layout_weight="1"
                android:text="注册" />
    
            <Button
                android:id="@+id/login"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_weight="1"
                android:text="登录" />
    
    
        LinearLayout>
    
    LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76

    在这里插入图片描述

    package com.dingjiaxiong.spshizhan;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.Context;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.text.TextUtils;
    import android.view.View;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
    
    
        private SharedPreferences sp;
    
        private EditText et_name;
        private EditText et_pwd;
        private CheckBox cb_rememberpwd;
        private CheckBox cb_autologin;
        private Button regist_btn;
        private Button login_btn;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
            //获取首选项SP
            sp = getSharedPreferences("config", Context.MODE_PRIVATE);
    
            initView();
    
            //第二次打开,从SP获取
            boolean rememberpwd = sp.getBoolean("rememberpwd",false);
            boolean autologin = sp.getBoolean("autologin",false);
    
            if(rememberpwd){
                String name = sp.getString("name","");
                String pwd = sp.getString("pwd","");
    
                et_name.setText(name);
                et_pwd.setText(pwd);
    
                cb_rememberpwd.setChecked(true);
            }
    
            if(autologin){
                cb_autologin.setChecked(true);
                //模拟自动登录
    
                Toast.makeText(MainActivity.this,"自动登录成功",Toast.LENGTH_SHORT).show();
            }
        }
    
        // 初始化
        private void initView() {
            et_name = findViewById(R.id.name);
            et_pwd = findViewById(R.id.password);
            cb_rememberpwd = findViewById(R.id.remember_pwd);
            cb_autologin = findViewById(R.id.auto_login);
    
            regist_btn = findViewById(R.id.register);
            login_btn = findViewById(R.id.login);
    
            //设置监听
            MyOnclickListener l = new MyOnclickListener();
            regist_btn.setOnClickListener(l);
            login_btn.setOnClickListener(l);
        }
    
        private class MyOnclickListener implements View.OnClickListener {
    
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.login:
    
                        String name = et_name.getText().toString().trim();
                        String pwd = et_pwd.getText().toString().trim();
                        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)) {
                            Toast.makeText(MainActivity.this, "用户名或密码为空", Toast.LENGTH_SHORT).show();
                        }
                        else{
                            //判断记住密码、自动登录是否打勾
                            if(cb_rememberpwd.isChecked()){
                                //用户名和密码都保存,同时记住密码的状态也要保存
                                SharedPreferences.Editor editor = sp.edit();
                                editor.putString("name",name);
                                editor.putString("pwd",pwd);
                                editor.putBoolean("rememberpwd",true);
                                editor.apply();
                            }
    
                            if(cb_autologin.isChecked()){
                                SharedPreferences.Editor editor = sp.edit();
                                editor.putBoolean("autologin",true);
                                editor.apply();
                            }
    
                        }
    
    
    
    
                        break;
                    case R.id.register:
                        break;
                }
            }
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117

    运行

    在这里插入图片描述

    在这里插入图片描述

  • 相关阅读:
    qt-双臂SCARA机器人动画
    一种基于E3处理器平台的NAS完整方案(从电脑组装到网站部署)
    Redis 学习笔记
    新版微信小程序发布指南
    dockerfile介绍与使用案例
    SQL基础知识(一)
    vue3之el-table单选
    美亚杯长安杯总结,特别感谢浙警许专家,22年取证辉煌缔造者,努力学习,学习!
    学习笔记-ThinkPHP 之 SQLI审计分析(三)
    Mac 睡眠唤醒 不睡眠 问题
  • 原文地址:https://blog.csdn.net/weixin_44226181/article/details/126314389