首页 > 其他分享 >44. SP数据存储

44. SP数据存储

时间:2022-09-19 09:12:33浏览次数:50  
标签:存储 layout 44 SP content wrap android id

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

}

运行

在这里插入图片描述

在这里插入图片描述

 

标签:存储,layout,44,SP,content,wrap,android,id
From: https://www.cnblogs.com/55zjc/p/16706579.html

相关文章

  • springboot中解析JSON参数
    解析psot请求中的JSON参数Map<String,String>attrMap=newHashMap<String,String>();BufferedReaderstreamReader=null;try{streamReader=newBufferedRead......
  • MySQL事务以及存储引擎
    MySQL事务以及存储引擎一、事务1.事务的概念●事务是一种机制、一个操作序列,包含了一组数据库操作命令,并且把所有的命令作为一个整体一起向系统提交或撤销操作请求,即......
  • AGC053F ESPers
    首先这题可以转化成“最后一次两个相等之前,有多少个ESPer投过票”计数。我的想法:把序列看成-1给少的投票,+1给大的投票,相等时只能使用+1。[1xxxxx][1xxxx......
  • Java【SpringBoot】——添加测试依赖
    在pom.xml添加依赖1<dependency>2<groupId>org.springframework.boot</groupId>3<artifactId>spring-boot-starter-test</artifactId>......
  • ESP8266升級SDK到V3.0版本編譯報錯
    編譯報錯信息bin/libmain2.a(app_main.o):Infunction`user_uart_wait_tx_fifo_empty':(.irom0.text+0x678):undefinedreferenceto`user_pre_init'bin/libmain2.a......
  • 「CSP-S 2022」初赛解析
    前言存疑点待补。有问题欢迎指出。想要题目部分源码请私信。有笨蛋连续\(2\)年第一题都错。乐。考前看了一眼一考就忘。如果不出意外的话,这是我最后一次更新初赛解析......
  • springboot集成mybatis获取插入数据的主键
    问题:我们想在插入一条数据后同时能够返回这条数据在表中的id,Mybatis提供了@SelectKey注解。student为数据表,主键自增SelectKey的四个属性:selectKey会将SELECTLAS......
  • SpringBoot集成Mybatis 实现InsertOrUpdate功能
    需求场景在项目开发过程中,难免会遇到这样的场景:对一张表,当数据不存在的时候,进行insert插入操作;数据存在的时候,进行update更新操作;下面就来使用Mybatis的InsertOrUpdate功......
  • 6000 字 | 16 图,吃透 Spring Cloud Gateway 原理
    大家好,我是小富~本篇给大家带来的是微服务框架中非常重要的一个组件:API网关。前言在PassJava项目中,我用到了SpringCloudGateway作为API网关,客户端的所有的请......
  • 2022 CSP-S 游记
    2022CSP-S游记看神仙们都写游记了我也来写写初赛Day0模拟赛又挂大分/fnrp--Day1上午vp了一下2019CSP-S的初赛题花了1h拿了58分觉得还行然后背了会......