使用SharedPreferences保存用户名和密码

使用SharedPreferences保存用户名和密码

一,什么是SharedPreferences?

这里写图片描述
二.SharedPreferences怎么用?

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

三.SharedPreferences保存用户名和密码代码实例:
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.administrator.myapplicat321.Main2Activity">
<EditText
    android:id="@+id/s1"
    android:layout_width="match_parent"
    android:layout_height="50dp" />
<EditText
    android:id="@+id/s2"
    android:layout_width="match_parent"
    android:inputType="textPassword"
    android:layout_height="50dp" />
    <CheckBox
        android:id="@+id/zx"
        android:text="记住密码"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        />
    <Button
        android:id="@+id/btn"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:text="登录"
        android:layout_gravity="center"
        />
</LinearLayout>

activity中代码:

package com.example.administrator.myapplicat321;

import android.app.Activity;
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.EditText;
import android.widget.Toast;

public class Main2Activity extends AppCompatActivity {
    private EditText editText1;
    private EditText editText2;
    private Button button;
    private String name;
    private String password;
    private CheckBox checkBox;
    private int rememberFlag = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        editText1 = findViewById(R.id.s1);
        editText2 = findViewById(R.id.s2);
        button = findViewById(R.id.btn);
        checkBox = findViewById(R.id.zx);


        onClick_ReadData();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                name = editText1.getText().toString();//获取edittext中输入的消息
                password = editText2.getText().toString();

                onClick_WriteData();

            }
        });

    }

    public void onClick_WriteData() {
        SharedPreferences mySharedPreferences = getSharedPreferences("test",
                MODE_PRIVATE);//创建文件
        SharedPreferences.Editor editor = mySharedPreferences.edit();//创建editor对象,写入值
        editor.putString("name", name);//保存用户名


        if (checkBox.isChecked()){//判断是否勾选单选框
            rememberFlag=1;//勾选的话为1,则显示密码
            editor.putInt("remember_flag", rememberFlag);
            editor.putString("password", password);
        }else {
            rememberFlag=0;
            editor.putInt("remember_flag", rememberFlag);
        }
        editor.commit();//提交值
        Toast.makeText(this, "数据成功写入SharedPreferences!",
                Toast.LENGTH_LONG).show();

    }

    public void onClick_ReadData() {
        SharedPreferences sharedPreferences = getSharedPreferences("test",
                MODE_PRIVATE);//从ps中提取文件
        if (sharedPreferences != null) {//判断写入值是否为空
            name = sharedPreferences.getString("name", "");
            password = sharedPreferences.getString("password", "");//取出密码
            rememberFlag = sharedPreferences.getInt("remember_flag", 0);//取出单选框的值
            editText1.setText(name);

        }
        if (rememberFlag==1){
            checkBox.setChecked(true);
            editText2.setText(password);
        }

    }
}
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值