• Android移动应用开发之登录用户密码记住及创建数据库存储查询用户名密码


    前言

    在这里插入图片描述
    这篇文章主要是实现上示功能。
    首先创建数据库(只能创建一次)
    然后输入用户名密码,店家注册,将数据插入数据库
    点击记住密码能够在下次登录时自动输入用户名密码
    最后点击登录能够进入登录界面。

    ps:这里有个bug就是得注册两次才能生效,不太理解哪里有问题了,以后知道了再来补…

    问题找到了
    没关数据库,所以会有点bug:

     cursor.close();
     database.close();
    
    • 1
    • 2

    及时关闭,就可以解决bug了。

    主要文件目录

    在这里插入图片描述
    首先我们看看布局文件:

    activity_main.xml

    就是一个简单的登录界面的布局。

    
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
    
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="用户名:"
                android:textSize="20sp"
    
                />
    
            <EditText
                android:id="@+id/uname"
                android:layout_width="281dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPersonName" />
        LinearLayout>
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="密    码:"
                android:textSize="20sp"
    
                />
    
            <EditText
                android:id="@+id/upwd"
                android:layout_width="281dp"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPersonName" />
        LinearLayout>
        <CheckBox
            android:id="@+id/cb"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="记住密码" />
    
        <Button
            android:id="@+id/createdb"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="创建数据库"/>
    
        <Button
            android:id="@+id/register"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="注册" />
        <Button
            android:id="@+id/login"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="登录" />
    LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77

    main.xml

    就是一个简单的登录成功界面的布局

    
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    
        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="欢迎光临!"
            android:textSize="50sp"
            tools:layout_editor_absoluteX="131dp"
            tools:layout_editor_absoluteY="62dp" />
    androidx.constraintlayout.widget.ConstraintLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    MainActivity

    内容结构应该比较清晰,按钮绑定各自对应的事件。

    稍微解释一下部分代码:

     Cursor cursor = database.rawQuery(
             "select * from userTb where uname = ?", new String[]{userName});
    
    • 1
    • 2

    表明查询语句,查询到的内容在cursor中,其中?的位置用userName这个字符串来表示,简单来说就是让sql语句写起来方便一点的写法。

    String String_upwd = cursor.getString(cursor.getColumnIndex("upwd"));
    
    • 1

    表明获取查询内容中的upwd字段的内容。

    package icy.hunter;
    
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.fragment.app.Fragment;
    import androidx.viewpager2.widget.ViewPager2;
    
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.EditText;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.Toast;
    
    import java.util.ArrayList;
    
    
    public class MainActivity extends AppCompatActivity{
        private CheckBox cb;
        private EditText uname;
        private EditText upwd;
        private static final String SP_INFO = "myuser";
        private static final String USER_ID = "UserId";
        private static final String USERPWD = "UserPwd";
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            this.uname = findViewById(R.id.uname);
            this.upwd = findViewById(R.id.upwd);
            this.cb = findViewById(R.id.cb);
            Button bt_create = findViewById(R.id.createdb);
            Button bt_register = findViewById(R.id.register);
            Button bt_login = findViewById(R.id.login);
    
            //        记录保存数据情况
            checkIfRemember();
    //        创建数据库
            bt_create.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase("data/data/icy.hunter/user.db", null);
                    String sql = "create table userTb(uid integer primary key autoincrement, uname text, upwd text)";
                    db.execSQL(sql);
                    Toast.makeText(MainActivity.this, "创建数据库以及表成功!", Toast.LENGTH_LONG).show();
                }
            });
    
            //插入数据库
            bt_register.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase("data/data/icy.hunter/user.db", null);
                    String userName = uname.getText().toString().trim();
                    String userPass = upwd.getText().toString().trim();
                    if(userName.equals("")||userPass.equals("")){
                        Toast.makeText(MainActivity.this, "用户名或者密码为空,请重新输入!", Toast.LENGTH_LONG).show();
                    }else{
                        String sql = "insert into userTb(uname, upwd) values('"+userName+"', '"+userPass+"')";
                        db.execSQL(sql);
                        Toast.makeText(MainActivity.this, "注册用户成功!", Toast.LENGTH_LONG).show();
                    }
                }
            });
    
    
            bt_login.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String userName = uname.getText().toString().trim();
                    String userPass = upwd.getText().toString().trim();
    
                    SQLiteDatabase database = SQLiteDatabase.openDatabase("data/data/icy.hunter/user.db", null,
                            SQLiteDatabase.OPEN_READONLY);
                    Cursor cursor = database.rawQuery(
                            "select * from userTb where uname = ?", new String[]{userName});
                    int success = 0;
                    while (cursor.moveToNext()) {
                        String String_upwd = cursor.getString(cursor.getColumnIndex("upwd"));
                        if(userPass.equals(String_upwd)){
                            success = 1;
                            Toast.makeText(MainActivity.this, userName+"用户登录成功!", Toast.LENGTH_LONG).show();
                            Intent it = new Intent(MainActivity.this, Main.class);
                            startActivity(it);
                        }
                    }
                    if(success == 0){
                        Toast.makeText(MainActivity.this, userName+"用户名或密码错误或用户不存在", Toast.LENGTH_LONG).show();
                    }
                }
            });
        }
    
        //存数据
        public void rememberMe(String uid, String pwd){
            SharedPreferences sp = getSharedPreferences(SP_INFO, MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();
            editor.putString(USER_ID, uid);
            editor.putString(USERPWD, pwd);
            editor.commit();
        }
        //读数据
        public void checkIfRemember(){
            SharedPreferences sp = getSharedPreferences(SP_INFO, MODE_PRIVATE);
            String uidStr = sp.getString(USER_ID, null);
            String pwdStr = sp.getString(USERPWD, null);
            if(uidStr != null && pwdStr != null){
                uname.setText(uidStr);
                upwd.setText(pwdStr);
                cb.setChecked(true);
            }
        }
    
        @Override
        protected void onStop() {
            super.onStop();
                String uidStr = upwd.getText().toString().trim();
                String pwdStr = uname.getText().toString().trim();
                if(cb.isChecked()){
                    rememberMe(uidStr, pwdStr);
                }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130

    Main

    为了跳转而设定的activity。

    package icy.hunter;
    
    import android.os.Bundle;
    
    import androidx.annotation.Nullable;
    import androidx.appcompat.app.AppCompatActivity;
    
    public class Main extends AppCompatActivity {
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    结果

    在这里插入图片描述

    然后我们可以看一下数据库文件和密码记录的文件。

    首先我们可以重启一下虚拟机,防止生成的文件没刷新出来…

    注意要查看文件虚拟机必须要启动,否则是看不到的。

    点击右下角

    在这里插入图片描述
    然后进入data/data滑到最下面。
    在这里插入图片描述
    这里就是存数据的地方了。
    可以看到我们存的数据库user.db出现了

    在这里插入图片描述
    这里就是记住密码的文件了
    点开可以看到:
    在这里插入图片描述
    里面就是我们存储记住密码的键值对。

    一些实现的细节还请详细阅读代码。

    代码已打包好可0积分放心下载:

    Android移动应用开发之登录用户密码记住,创建数据库存储查询密码

  • 相关阅读:
    Redis事务_锁机制 _主从复制
    java servlet大学生旅游网站的设计与开发源码
    win7 64位安装vs code
    机器学习中常见的特征工程处理
    1.4_29 Axure RP 9 for mac 高保真原型图 - 案例28【中继器-后台管理系统6】功能-原位修改数据
    flutter 与H5交互
    js如何区分数组和对象
    Python程序设计基础2
    NC26 括号生成
    SQL Server 日期范围按每月一行拆分
  • 原文地址:https://blog.csdn.net/qq_52785473/article/details/127769744