Android的SharedPreferences的使用

工程目录:
在这里插入图片描述
MainActivity

package com.example.demo_eight;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.demo_eight.R;

public class MainActivity extends AppCompatActivity {
    private EditText et1, et2;
    SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et1 = findViewById(R.id.edit1);
        et2 = findViewById(R.id.edit2);
        sp = getPreferences(Activity.MODE_PRIVATE);
        String username = sp.getString("username", "");  //第2参数表示按键名取不到值时,设置为空串
        String password = sp.getString("password", "");
        et1.setText(username);
        et2.setText(password);

        Button btn1=findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                String s1 = et1.getText().toString();
                String s2 = et2.getText().toString();
                Toast.makeText(getApplicationContext(), "输入的用户名为" + s1 + ",输入的密码为" + s2, Toast.LENGTH_LONG).show();
            }
        });
        Button btn2=findViewById(R.id.btn2);
        btn2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                et1.setText("");
                et2.setText("");
                Toast.makeText(getApplicationContext(), "取消登录", Toast.LENGTH_SHORT).show();
            }
        });

        CheckBox cb = findViewById(R.id.cb);
        cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                if (arg1) {
                    Toast.makeText(MainActivity.this, "已保存登录信息", Toast.LENGTH_LONG).show();
                    sp.edit()
                            .putString("username", et1.getText().toString())
                            .putString("password", et2.getText().toString())
                            .commit();
                } else {
                    Toast.makeText(MainActivity.this, "不保存登录信息", Toast.LENGTH_LONG).show();
                    sp.edit()
                            .putString("username", null)
                            .putString("password", null)
                            .commit();
                }
            }
        });
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="用户名:" />

            <EditText
                android:id="@+id/edit1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:inputType="textPersonName"
                android:text="" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="密    码:" />

            <EditText
                android:id="@+id/edit2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10"
                android:inputType="textPersonName"
                android:text="" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/btn1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="登录" />

            <Button
                android:id="@+id/btn2"
                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:layout_weight="1"
            android:orientation="vertical">

            <CheckBox
                android:id="@+id/cb"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="记住我" />
        </LinearLayout>
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43873198/article/details/108814521