Android期末考考前盘点(五):100%会考的SharedPreferences存储

要论期末考试必须考的存储方式,我SQLite大哥只能排第二,而SharedPreferences必须排第一,用最少的代码量做到最基础最必要的功能----自动登录或记住密码

SharedPreferences(以下简称SP)只需要记住两段代码,读取数据与保存数据

SP存储的方式是在手机内部,以XML格式文件保存的键值对数据

先看一下我们如何看到这个XML文件,考试时可以随时验证是否成功

还是原来的:点击打开模拟器资源管理

 打开data/data/包名的文件夹

 那么在shared_prefs这个文件夹下的文件就是SP的存储文件

打开看看吧:可以看到键值对存储的形式

接下来可以看一个简单的实例,界面使用的是之前实验的界面

XML代码如下:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#E6E6E6"
    android:orientation="vertical"
    android:padding="10dp">

    <ImageView
        android:layout_marginTop="100dp"
        android:id="@+id/imageView1"
        android:layout_width="140dp"
        android:layout_height="140dp"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal"
        android:scaleType="fitCenter"
        android:src="@drawable/a111"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:background="@android:color/white"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="账号:"
            android:textColor="#000"
            android:textSize="20sp" />
        <EditText

            android:id="@+id/et_account"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:background="@null"
            android:padding="10dp" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@android:color/white"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/tv_password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="密码:"
            android:textColor="#000"
            android:textSize="20sp" />
        <EditText
            android:id="@+id/et_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:background="@null"
            android:inputType="textPassword"
            android:padding="10dp" />
    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="选择功能"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <CheckBox

            android:layout_marginLeft="30dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="记住密码"
            android:id="@+id/cb1"/>
        <CheckBox
            android:layout_marginLeft="150dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="自动登录"
            android:id="@+id/cb2"/>
    </LinearLayout>

    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:background="#3C8DC4"
        android:text="登录"
        android:textColor="@android:color/white"
        android:textSize="20sp" />

</LinearLayout>

逻辑代码如下:

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity71 extends AppCompatActivity {
    CheckBox cb1;
    EditText account,password;
    SharedPreferences share;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main71);
        //绑定控件
        cb1=findViewById(R.id.cb1);
        account=findViewById(R.id.et_account);
        password=findViewById(R.id.et_password);
        //在进入界面执行的时候,去调取名为data的SharedPreferences文件下的数据
        share = getSharedPreferences("data",MODE_PRIVATE);
        //给account password设置上次记住登录的值,share.getString方法是获取存储的String类型的值
        //两个参数,第一个参数是键值对的键,第二个参数是若没有数据则获取的默认数据
        account.setText(share.getString("account",""));
        password.setText(share.getString("password",""));

        //给登录按钮生成点击事件
        findViewById(R.id.btn_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //如果记住密码多选框被选中则记住密码
                 if (cb1.isChecked()){
                    SharedPreferences.Editor edt = share.edit();
                    //保存一个键值对  键为account 值为输入的账号
                    edt.putString("account",account.getText().toString());
                    //保存一个键值对  键为password  值为输入的密码
                    edt.putString("password",password.getText().toString());
                    //提交修改  必须提交!否则不会保存
                    edt.commit();
                    Toast.makeText(MainActivity71.this,"登陆成功",Toast.LENGTH_SHORT).show();
                }

            }
        });
    }
}

显而易见在登录代码中,使用的就是SP的保存数据代码,需要用到Editor这个东东,而进入界面读取数据的则是使用的SP读取数据代码,不需要这个东东。

所以你必须甚至是记住这两个代码,所以说如果是开卷,这则是送分:

SP保存数据:

SharedPreferences share = getSharedPreferences("data",MODE_PRIVATE);
SharedPreferences.Editor edt = share.edit();
//保存一个键值对  键为account 值为输入的账号
edt.putString("account",account.getText().toString());
//保存一个键值对  键为password  值为输入的密码
edt.putString("password",password.getText().toString());
//提交修改  必须提交!否则不会保存
edt.commit();

SP读取数据:

SharedPreferences share = getSharedPreferences("data",MODE_PRIVATE);
SharedPreferences.Editor edt = share.edit();
//保存一个键值对  键为account 值为输入的账号
edt.putString("account",account.getText().toString());
//保存一个键值对  键为password  值为输入的密码
edt.putString("password",password.getText().toString());
//提交修改  必须提交!否则不会保存
edt.commit();

PS:如果没有勾选记住密码,使用保存数据的代码设置account、password为空“”即可(但不止这一个方法,应付期末用这个即可了)

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
SharedPreferencesAndroid中轻量级的数据存储方式之一,用于存储一些简单的键值对数据并进行读取。以下是在Android Studio中使用SharedPreferences进行数据存储和读取的步骤: 1. 创建SharedPreferences对象 在Activity中,我们可以通过以下代码创建SharedPreferences对象: ```java SharedPreferences preferences = getSharedPreferences("my_prefs", Context.MODE_PRIVATE); ``` 其中,第一个参数是SharedPreferences的名称,第二个参数是访问模式,MODE_PRIVATE表示只有当前应用程序可以访问该SharedPreferences。 2. 存储数据 使用SharedPreferences存储数据可以使用其Editor对象,示例代码如下: ```java SharedPreferences preferences = getSharedPreferences("my_prefs", Context.MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); editor.putString("key", "value"); editor.apply(); ``` 其中,putString()方法用于存储字符串类型的数据,第一个参数是键,第二个参数是值。apply()方法用于提交修改。 3. 读取数据 使用SharedPreferences读取数据可以通过以下代码: ```java SharedPreferences preferences = getSharedPreferences("my_prefs", Context.MODE_PRIVATE); String value = preferences.getString("key", ""); ``` 其中,getString()方法用于读取字符串类型的数据,第一个参数是键,第二个参数是默认值。 以上就是在Android Studio中使用SharedPreferences进行数据存储和读取的基本步骤。需要注意的是,SharedPreferences适合存储一些简单的键值对数据,对于复杂数据建议使用SQLite数据库等其他数据存储方式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林林要一直努力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值