安卓SharedPreferences存储数据之实现记住密码功能

时间:2020-05-20
本文章向大家介绍安卓SharedPreferences存储数据之实现记住密码功能,主要包括安卓SharedPreferences存储数据之实现记住密码功能使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

         SharedPreferences是使用键值对的方式去存储数据的,也就是说,当保存一条数据的时候,需要给这条数据提供一个相应的键,这样在读取数据的时候就可以通过这个键把值读出来。还有,SharedPreferences文件是以xml格式去存储数据的。要先学习存储数据到SharedPreferences与读取SharedPreferences文件中的数据,才开始实现记住密码功能。

一、将数据存储到SharedPreferences中

第一步:先获得SharedPreferences对象,获得对象有三个方法:

(1)Context类的getSharedPreferences(String fileName, MODE_PRIVATE) 方法:第一个参数是指定存储的文件名,若不存在则创建一个,文件存放在/data/data/<package name>/shared_prefs目录下。第二个参数用于指定的操作模式,目前只有MODE_PRIVATE可以选,表示只有当前的应用程序可以修改这个SharedPreferences文件。

(2)Activity类中的getPreferences(String fileName):参数是文件名称

(3)PreferenceManager类中的getDefaultSharedPreferences(Context context)

第二步:得到SharedPreferences对象后,主要可以分三步实现:

①调用SharedPreferences对象的edit()方法来获取一个SharedPreferences.Editor对象

②向SharedPreferences.Editor对象中添加数据,使用putXXX()方法,如添加字符串使用putString();

③调用SharedPreferences.Editor对象的apply()方法,完成数据存储操作

简单的代码:

SharedPreferences.Editor  editor=getSharedPreferences("data",MODE_PRIVATE).edit();
editor.putString("name",Tom);
editor.putInt("age",19);
editor.apply();

二、读取SharedPreferences文件中的数据

         读数据非常简单,只需要先获得SharedPreferences对象,然后调用SharedPreferences对象的getXXX()方法,如读取一个字符串就调用getString(),getXXX()方法有两个参数,第一个参数是键,即你要获取的数据对应的键;第二个参数是默认值,即表示当传入的键找不到对应的值时以这个默认值返回。

简单的代码:

SharedPreferences pref=getSharedPreferences("data",MODE_PRIVATE);
String name=pref.getString("name","");//默认是空字符串
int age=pref.getInt("age",0);//默认值0岁

三、使用SharedPreferences实现记住密码功能

先写一个登陆界面

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent">
 6 
 7     <LinearLayout
 8         android:orientation="horizontal"
 9         android:layout_width="match_parent"
10         android:layout_height="60dp">
11         <TextView
12             android:layout_width="90dp"
13             android:layout_height="wrap_content"
14             android:layout_gravity="center_vertical"
15             android:textSize="18sp"
16             android:text="用户名:"/>
17         <EditText
18             android:id="@+id/username"
19             android:layout_width="0dp"
20             android:layout_height="wrap_content"
21             android:layout_weight="1"
22             android:layout_gravity="center_vertical"/>
23     </LinearLayout>
24 
25     <LinearLayout
26         android:orientation="horizontal"
27         android:layout_width="match_parent"
28         android:layout_height="60dp">
29         <TextView
30             android:layout_width="90dp"
31             android:layout_height="wrap_content"
32             android:layout_gravity="center_vertical"
33             android:textSize="18sp"
34             android:text="密码:"/>
35         <EditText
36             android:id="@+id/password"
37             android:layout_width="0dp"
38             android:layout_height="wrap_content"
39             android:layout_weight="1"
40             android:inputType="textPassword"
41             android:layout_gravity="center_vertical"/>
42     </LinearLayout>
43 
44     <LinearLayout
45         android:orientation="horizontal"
46         android:layout_width="match_parent"
47         android:layout_height="wrap_content">
48 
49         <CheckBox
50             android:id="@+id/remember"
51             android:layout_width="wrap_content"
52             android:layout_height="wrap_content"/>
53         <TextView
54             android:layout_width="wrap_content"
55             android:layout_height="wrap_content"
56             android:text="记住密码"/>
57     </LinearLayout>
58     <Button
59         android:id="@+id/login"
60         android:layout_width="match_parent"
61         android:layout_height="60dp"
62         android:text="登录"/>
63 
64 
65 </LinearLayout>

MainActivity.java

 1 package com.example.myapplication;
 2 
 3 import androidx.appcompat.app.AppCompatActivity;
 4 
 5 import android.content.Intent;
 6 import android.content.SharedPreferences;
 7 import android.os.Bundle;
 8 import android.view.View;
 9 import android.widget.Button;
10 import android.widget.CheckBox;
11 import android.widget.EditText;
12 import android.widget.Toast;
13 
14 import java.sql.PreparedStatement;
15 
16 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
17     private EditText userName,password;
18     private Button Login;
19     private CheckBox checkBox;
20     private SharedPreferences.Editor editor;
21     private SharedPreferences pref;
22 
23 
24     @Override
25     protected void onCreate(Bundle savedInstanceState) {
26         super.onCreate(savedInstanceState);
27         setContentView(R.layout.activity_lmain);
28         init();//初始化组件
29         boolean isRemember=pref.getBoolean("remember_password",false);
30         if(isRemember){
31             String account=pref.getString("account","");
32             String passWord=pref.getString("password","");
33             userName.setText(account);
34             password.setText(passWord);
35             checkBox.setChecked(true);
36         }
37 
38     }
39     private void init(){
40         userName=(EditText)findViewById(R.id.username);
41         password=(EditText)findViewById(R.id.password);
42         checkBox=(CheckBox)findViewById(R.id.remember);
43         Login=(Button)findViewById(R.id.login);
44         Login.setOnClickListener(this);
45         pref= getSharedPreferences("Data",MODE_PRIVATE);
46     }
47 
48     @Override
49     public void onClick(View v) {
50         String username=userName.getText().toString().trim();
51         String passwordText=password.getText().toString().trim();
52         //如果账号是admin,密码是12345,就认为登录成功
53         if(username.equals("admin")& passwordText.equals("12345")){
54             editor=pref.edit();
55             if(checkBox.isChecked()){//检查复选框是否被选中
56                 editor.putBoolean("remember_password",true);
57                 editor.putString("account",username);
58                 editor.putString("password",passwordText);
59             }else{
60                 editor.clear();
61             }
62             editor.apply();
63         }else{
64             Toast.makeText(LoginActivity.this,"用户名或密码不正确",Toast.LENGTH_SHORT).show();
65         }
66     }
67 }

运行程序,当用户名输入admin,密码输入12345,勾选记住密码框,然后点登陆按钮,按back键退出,重新回到登录界面,你会发现账号密码已经自动填充到界面上了。

原文地址:https://www.cnblogs.com/panqiaoyan/p/12923408.html