Android学习10

SharedPreferences

今天练习了利用SharedPreferences保存登录用户名密码;

layout布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp">

    <EditText
        android:id="@+id/et_user"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:textSize="15sp"
        android:textColor="#FF8F44"
        android:background="@drawable/bg_username"
        android:hint="用户名"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"/>

    <EditText
        android:id="@+id/et_password"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:textSize="15sp"
        android:textColor="#FF8F44"
        android:layout_below="@id/et_user"
        android:background="@drawable/bg_username"
        android:hint="密码"
        android:inputType="textPassword"
        android:layout_marginTop="15dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"/>

    <CheckBox
        android:id="@+id/cb_pwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="记住密码"
        android:checked="false"
        android:layout_below="@+id/et_password"
        android:layout_marginLeft="46dp"
        android:layout_marginTop="20dp"/>

    <CheckBox
        android:id="@+id/cb_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="自动登录"
        android:layout_marginLeft="237dp"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/et_password"/>

    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@id/cb_login"
        android:layout_marginTop="20dp"
        android:background="@drawable/bg_btn4"
        android:text="登录"
        android:textSize="20sp"
        android:textColor="#fff"/>


</RelativeLayout>

activity:

package com.example.helloworld.datastorage;

import androidx.appcompat.app.AppCompatActivity;

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

import com.example.helloworld.R;

public class SpLoginActivity extends AppCompatActivity {

    private EditText mEtUser;
    private EditText mEtPassword;
    private Button mBtnlogin;
    private CheckBox mCbremember, mCbautomatic;
    private SharedPreferences mSharedPreferences;
    private SharedPreferences.Editor mEditor;

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

        mEtUser = findViewById(R.id.et_user);
        mEtPassword = findViewById(R.id.et_password);
        mBtnlogin = findViewById(R.id.btn_login);
        mCbremember = findViewById(R.id.cb_pwd);
        mCbautomatic = findViewById(R.id.cb_login);

        //获得mSharedPreferences的实例
        mSharedPreferences = getSharedPreferences("logindata", MODE_PRIVATE);
        //获取一个Edit对象,所有对数据的操作都需要经过Edit
        mEditor = mSharedPreferences.edit();
        //从SharedPreferences中取出记住密码的状态值
        boolean isremember = mSharedPreferences.getBoolean("isremember", false);

        //判断状态值
        if (isremember) {
            //取出账号密码
            String names = mSharedPreferences.getString("name", null);
            String passs = mSharedPreferences.getString("pass", null);
            //设置复选框的状态是勾选的状态
            mCbremember.setChecked(true);
            mEtUser.setText(names);
            mEtPassword.setText(passs);
        }

        //取出自动登录的状态值
        boolean isautomatic = mSharedPreferences.getBoolean("isautomatic", false);
        if (isautomatic) {
            //跳转
            Toast.makeText(this, "登陆成功", Toast.LENGTH_LONG).show();
        }

        //对登录按钮添加监听
        mBtnlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //判断记住密码的复选框是否勾选如果勾选就在SharedPreferences中存入账号片密码,状态值
                if (mCbremember.isChecked()) {
                    mEditor.putString("name", mEtUser.getText().toString());//获取EditText里的文本内容
                    mEditor.putString("password",mEtPassword.getText().toString());
                    //存入状态值,(代表如果已经勾选了,那么就存一个true,的值)
                    mEditor.putBoolean("isremember", true);
                    mEditor.commit();
                }

                //自动登录
                if (mCbautomatic.isChecked()) {
                    //当勾选了自动登录 存一个为true的状态值
                    mEditor.putBoolean("isautomatic", true);
                    mEditor.commit();
                }

                //跳转
                Toast.makeText(SpLoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();

            }
        });
    }

}

Android学习10