用 SharedPreferences 存储进行记住密码 和保存用户名(记住密码)

1.什么是SharedPreferences 存储?

SharedPreferences是Android平台上一个轻量级的存储类,用来存储少量数据时简单,便捷(保存记住密码状态,设置开关状态等)。
key-value(键值对)形式存储数据,可以存储的数据类型为:String、float、int、long、boolean。
存储位置在/data/data/<包名>/shared_prefs目录下。
保存的数据以XML形式存储。

2.使用SharedPreferences写入数据步骤:

1.获得使用SharedPreferences对象;
2.获得Editor对象;
3.通过Editor对象putXXX函数,设置写入数据;
4.通过Editor对象的commit提交写入;

获取SharePreferences对象

在有Context环境中使用getSharePreferences方法获得

获取Editor对象

通过SharePreferences.edit()方法获得

使用SharedPreferences存储数据案例
这里写图片描述

package com.example.myapplication15;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
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;

public class MainActivity extends AppCompatActivity {
    private EditText edit_username;
    private EditText edit_password;
    private CheckBox checkBox;
    private Button button;
    private CheckBox checkBox_zidong;
    private int remamberFlag = 0;
    private String password = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        binID();
        SharedPreferences sharedPreferences = getSharedPreferences("test", MODE_PRIVATE);


        //如果不为空
        if (sharedPreferences != null) {
            String name = sharedPreferences.getString("name", "");
            password = sharedPreferences.getString("password", "");
            remamberFlag = sharedPreferences.getInt("remeber_flag", 0);
            edit_username.setText(name);
        }

        //确定为1获取 记住密码,打钩
        if (remamberFlag == 1) {
            checkBox.setChecked(true);
            edit_password.setText(password);
        }
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //1 创建 SharePreferences 对象
                String username = edit_username.getText().toString();
                String password = edit_password.getText().toString();
                SharedPreferences sharedPreferences = getSharedPreferences("test", MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                //2  创建Editor对象,写入值
                editor.putString("name", username);
                if (checkBox.isChecked()) {
                    remamberFlag = 1;
                    editor.putInt("remeber_flag", remamberFlag);
                    editor.putString("password", password);
                } else {
                    remamberFlag = 0;
                    editor.putInt("remeber_flag", remamberFlag);
                }
                //3  提交
                editor.commit();
                Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_LONG).show();
            }
        });
    }
    private void binID() {
        edit_username = findViewById(R.id.edit_username);
        edit_password = findViewById(R.id.edit_password);
        checkBox = findViewById(R.id.Cb);
        checkBox_zidong = findViewById(R.id.zidong_check);
        button = findViewById(R.id.main_button);

    }
}

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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.myapplication15.MainActivity">


    <RelativeLayout
        android:layout_marginTop="200dp"
        android:gravity="center"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:id="@+id/edit_username"
            android:hint="输入账号"
            android:layout_gravity="center"
            android:layout_width="250dp"
            android:layout_height="50dp" />

    </RelativeLayout>
    <RelativeLayout
        android:gravity="center"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:id="@+id/edit_password"
            android:hint="输入密码"
            android:password="true"
            android:layout_gravity="center"
            android:layout_width="250dp"
            android:layout_height="50dp" />

    </RelativeLayout>

    <Button
        android:id="@+id/main_button"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="登录"
        />

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

    <CheckBox
        android:id="@+id/Cb"
        android:layout_width="30dp"
        android:layout_height="20dp"
        android:layout_marginLeft="60dp"
        />

    <TextView
        android:textColor="#000000"
        android:layout_width="80dp"
        android:layout_height="30dp"
        android:text="记住密码"
        />
    <CheckBox
        android:id="@+id/zidong_check"
        android:layout_width="30dp"
        android:layout_height="20dp"
        android:layout_marginLeft="60dp"
        />

    <TextView
        android:textColor="#000000"
        android:layout_width="80dp"
        android:layout_height="30dp"
        android:text="自动登录"
        />
</LinearLayout>

</LinearLayout>

这里写图片描述

  • 0
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值