44. SP数据存储

44. SP数据存储

44.1 数据存储是什么

创建一个新工程

在这里插入图片描述

数据保存到APP本身。

44.2 数据存储有哪些
SP 、 SQLite 【原生】、 Room【更简洁】

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

44.3 SP特点介绍
sharedpreference 首选项

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

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

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

运行项目

44.4 SP简单使用
布局

<?xml version="1.0" encoding="utf-8"?>
<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>

 


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) {
    }
}

运行

在这里插入图片描述

在这里插入图片描述

成功

如何获取

//从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();
}

在这里插入图片描述

运行

在这里插入图片描述

44.5 SP真实实战
布局

<?xml version="1.0" encoding="utf-8"?>
<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>

在这里插入图片描述

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;
            }
        }
    }

}

运行

在这里插入图片描述

在这里插入图片描述

 

posted @ 2022-09-19 09:05  随遇而安==  阅读(52)  评论(0编辑  收藏  举报