Inventory before the Android final exam (five): 100% SharedPreferences storage for the exam

In terms of the storage method that must be tested in the final exam, my SQLite brother can only rank second, and SharedPreferences must rank first, with the least amount of code to achieve the most basic and necessary functions-automatic login or remember passwords .

SharedPreferences (hereinafter referred to as SP) only needs to remember two pieces of code, reading data and saving data

The SP storage method is the key-value pair data saved in the XML format file inside the mobile phone

Let's take a look at how we can see this XML file. You can verify whether it is successful at any time during the exam.

Still the original: Click to open the simulator resource management

 Open the folder of data/data/package name

 Then the files in the shared_prefs folder are the storage files of the SP.

Open it and have a look: you can see the form of key-value pair storage

Next, you can look at a simple example. The interface uses the interface of the previous experiment.

The XML code is as follows:

<?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>

The logic code is as follows:

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

            }
        });
    }
}

Obviously, in the login code, the code for saving data of SP is used, and the editor is required, while the code for reading data is used for reading data in the interface, and this stuff is not needed.

So you must even memorize these two codes, so if it is an open book, this is a bonus:

SP save data:

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 read data:

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: If you do not check Remember Password, just use the code to save the data to set the account and password to empty "" (but this is not the only method, just use this at the end of the period)

Guess you like

Origin blog.csdn.net/m0_59558544/article/details/131329353