Android Studio SharedPreferences数据存储的学习

Android Studio SharedPreferences数据存储的学习,第1张

Android Studio SharedPreferences数据存储的学习

Android Studio 有三种数据存储的方式,作为入门,将上周学习的SharedPreferences形式的数据存储方式进行总结,发布在我的blog上
首先,我也是作为了解,先入门学习了一些相关知识

如下图所示,一般的使用方式是
1.获取SharePreferences对象,同时指定名称和访问权限
2.获取SharePreferences的编辑器对象,其中Editor是sharePreferences中的一个类
3.通过编辑器进行数据的存储,数据的存储方式有很多种,int ,String,boolean类型都有的。
4.使用editor.commit()进行提交


本次学习是完成了一个小登录界面。实现一个数据的存储和登录
像其他app一样,用户一打开app就要进行一个注册的 *** 作,那么注册的过程中这个后台就通过使用SharedPreferences对用户的数据进行存储,,然后注册后,就会提示注册成功,这个是使用Toast实现的,然后进行登录的 *** 作,同时对于用户名和密码要进行判断,是否密码和用户名匹配,如果匹配,那么就显示登录成功。

话不多说,上代码

<--这里是界面的代码,前端很简单,只是要记住有两个button后期是要为此写代码的-->
<?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=".MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@mipmap/ic_launcher"
        android:layout_marginTop="200dp"
        android:layout_marginLeft="150dp"/>

    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入用户名"
        android:layout_marginTop="20dp"
        />
    <EditText
        android:id="@+id/psw"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:password="true"
        android:hint="输入密码" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"
        android:id="@+id/login"
        android:layout_marginTop="10dp"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="注册"
        android:id="@+id/register"
        android:layout_marginTop="10dp"/>

</LinearLayout>
package com.example.myapplication;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.HashMap;
import java.util.Map;    //以上都是一些引用的包文件

public class UserInfo_SharePre   //这是在Java下面设立的一个共有类,名字叫做UserInfo_SharePre
{
    //用户数据的存储的方法
    public static boolean saveUserInfo(String userName, String PassWord, Context context){
        //获取SharePreferences对象,同时指定名称和访问权限
        SharedPreferences sp=context.getSharedPreferences("myData",Context.MODE_PRIVATE);
        //获取SharePreferences的编辑器对象
       SharedPreferences.Editor edit = sp.edit();//Editor是sharePreferences中的一个类
       //通过编辑器进行数据的存储
        edit.putString("Uname",userName);
        edit.putString("pwd",PassWord);
        edit.commit();//提交

        return true;
    }
    //读取用户的数据
    public static Map<String,String> getUserInfo(Context context){
        SharedPreferences sp=context.getSharedPreferences("myData",Context.MODE_PRIVATE);
        String usrName=sp.getString("Uname","");
        String Password=sp.getString("pwd","");
        Map<String,String> userMap=new HashMap<>();
        userMap.put("UserName",usrName);
        userMap.put("psd",Password);
        return userMap;
    }
}

MainActivity.java

package com.example.myapplication;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ActionBar;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private Button registerBtn,loginBtn;
    private EditText userName,password;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        registerBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //提取编辑框中输入的用户名和密码,将它们写入到文件中去
                //trim的方法是用于删除字符串的头尾空白符
                String Uname=userName.getText().toString().trim();
                String Pwd=password.getText().toString().trim();
                if(TextUtils.isEmpty(Uname)){
                    Toast.makeText(MainActivity.this,"用户名不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(TextUtils.isEmpty(Pwd)){
                    Toast.makeText(MainActivity.this,"请输入密码",Toast.LENGTH_SHORT).show();
                    return;
                }
                boolean isSuccess=UserInfo_SharePre.saveUserInfo(Uname,Pwd,MainActivity.this);
                if(isSuccess){
                    Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
                }
                else{
                    Toast.makeText(MainActivity.this, "保持失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Map<String,String> userMap=UserInfo_SharePre.getUserInfo(MainActivity.this);
                //提取编辑框中输入的用户名和密码,将它们写入到文件中去
                //trim的方法是用于删除字符串的头尾空白符
                String Uname=userName.getText().toString().trim();
                String Pwd=password.getText().toString().trim();
                String MapUname=userMap.get("UserName");
                String MapPwd=userMap.get("psd");
                if(userMap!=null&&Uname.equals(MapUname)&&Pwd.equals(MapPwd)){
                    Toast.makeText(MainActivity.this, "登录验证成功", Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }
    private void init(){
        registerBtn=(Button)findViewById(R.id.register);
        loginBtn=(Button)findViewById(R.id.login);
        userName=(EditText)findViewById(R.id.name);
        password=(EditText)findViewById(R.id.psw);
    }

}

有问题可以私聊,我也是刚学,还有很多东西要去学习

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/876621.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存