马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
引言
在 Android 应用步伐中,管理用户数据至关重要。SQLite 是 Android 中广泛使用的轻量级数据库,非常得当存储和管理用户登录和注册信息。本文将指导你如安在 Android Studio 中使用 SQLite 数据库实现登录和注册功能。
创建 SQLite 数据库
首先,你需要创建一个 SQLite 数据库来存储用户数据。为此,请执行以下步骤:
- 在你的项目中创建一个名为 DBHelper 的类,它扩展自 SQLiteOpenHelper。
- 在 DBHelper 类中,实现 onCreate() 方法,该方法将在数据库初次创建时调用。在 onCreate() 方法中,使用 execSQL() 方法创建名为 userInfo 的表,其中包含 uname(用户名)和 psw(暗码)列。
- 在 DBHelper 类中,还实现 onUpgrade() 方法,该方法将在数据库版本更改时调用。在这个方法中,你可以执行须要的更新操作。
实现登录功能
要实现登录功能,请执行以下步骤:
- 在你的登录运动中,获取用户名和暗码输入框的引用。
- 使用 DBHelper 类获取数据库的可写实例。
- 执行 SQL 查询以查抄输入的用户名和暗码是否与数据库中的记载匹配。
- 如果查询效果不为空,则表示找到了匹配的记载,而且显示 "登录成功" 的消息。
- 如果查询效果为空,则显示 "用户名或暗码输入错误" 的消息。
实现注册功能
要实现注册功能,请执行以下步骤:
- 在你的注册运动中,获取用户名和暗码输入框的引用。
- 使用 DBHelper 类获取数据库的可写实例。
- 查抄用户名和暗码是否为空。如果为空,则显示 "用户名或暗码未输入" 的消息。
- 执行 SQL 查询以查抄输入的用户名是否已存在。
- 如果查询效果为空,则表示用户名不存在,可以进行注册。
- 使用 ContentValues 对象创建包含用户名和暗码的新记载。
- 使用 insert() 方法将新记载插入到数据库中。
- 显示 "注册成功" 的消息,并跳转回登录界面。
示例代码
DBHelper.java
- package com.example.loginandregister;
- import android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- public class DBHelper extends SQLiteOpenHelper {
- public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
- super(context, name, factory, version);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- //创建一个表
- String SQLstr = "create table userInfo(uname text,psw text)";
- db.execSQL(SQLstr);
- //往表里边插入一个数据
- SQLstr = "insert into userInfo(uname,psw) values('admin','123456')";
- db.execSQL(SQLstr);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- }
- }
复制代码 登录界面计划
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <EditText
- android:id="@+id/username"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="请输入用户名" />
- <EditText
- android:id="@+id/password"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="请输入密码"
- android:inputType="textPassword" />
- <Button
- android:id="@+id/btn"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="登录" />
- <TextView
- android:id="@+id/zhuce"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="注册" />
- </LinearLayout>
复制代码 登录界面功能代码
- package com.example.login;
- import android.content.Intent;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import androidx.appcompat.app.AppCompatActivity;
- public class MainActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- // 导入必要的包
- import android.database.sqlite.SQLiteDatabase;
- // 添加必要的权限
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- // 创建 DBHelper 类的实例,用于管理名为 "test.db" 的 SQLite 数据库
- DBHelper dbHelper = new DBHelper(MainActivity.this,"test.db",null,1);
- // 获取数据库的可写实例
- SQLiteDatabase db = dbHelper.getWritableDatabase();
- // 获取用户名和密码输入框的引用
- EditText unametxt = findViewById(R.id.username);
- EditText pswtxt = findViewById(R.id.password);
- // 处理登录按钮的点击事件
- Button btn = findViewById(R.id.btn);
- btn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // 获取用户名和密码输入框中的文本
- String uname = unametxt.getText().toString();
- String psw = pswtxt.getText().toString();
- // 执行 SQL 查询,以检查输入的用户名和密码是否与数据库中的记录匹配
- Cursor cursor = db.query("userInfo",new String[]{"uname,psw"},"uname=? and psw=?",new String[]{uname,psw},null,null,null);
- // 检查查询结果是否为空
- if(cursor.moveToFirst()) {
- // 如果结果不为空,则表示找到了匹配的记录,并且显示 "登录成功" 的 Toast 消息
- Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
- } else {
- // 如果结果为空,则显示 "用户名或密码输入错误" 的 Toast 消息
- Toast.makeText(MainActivity.this,"用户名或密码输入错误",Toast.LENGTH_SHORT).show();
- }
- }
- });
- // 处理注册按钮的点击事件
- TextView zhuce = findViewById(R.id.zhuce);
- zhuce.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // 创建到 MainActivity2 类的意图并启动该活动
- Intent intent = new Intent(MainActivity.this,MainActivity2.class);
- startActivity(intent);
- }
- });
- }
- }
复制代码 注册界面计划
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <EditText
- android:id="@+id/username"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="请输入用户名" />
- <EditText
- android:id="@+id/password"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="请输入密码"
- android:inputType="textPassword" />
- <Button
- android:id="@+id/btn"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="注册" />
- </LinearLayout>
复制代码 注册界面功能代码
- package com.example.login;
- import android.content.Intent;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import androidx.appcompat.app.AppCompatActivity;
- public class MainActivity2 extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main2);
- // 导入必要的包
- import android.database.sqlite.SQLiteDatabase;
- // 添加必要的权限
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- // 创建 DBHelper 类的实例,用于管理名为 "test.db" 的 SQLite 数据库
- DBHelper dbHelper = new DBHelper(MainActivity2.this,"test.db",null,1);
- // 获取数据库的可写实例
- SQLiteDatabase db = dbHelper.getWritableDatabase();
- // 获取用户名和密码输入框的引用
- EditText unametxt = findViewById(R.id.username);
- EditText pswtxt = findViewById(R.id.password);
- // 处理注册按钮的点击事件
- Button btn = findViewById(R.id.btn);
- btn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // 获取用户名和密码输入框中的文本
- String uname = unametxt.getText().toString();
- String psw = pswtxt.getText().toString();
- // 执行 SQL 查询,以检查输入的用户名是否已存在
- Cursor cursor = db.query("userInfo",new String[]{"uname"},"uname=?",new String[]{uname},null,null,null);
- // 检查查询结果是否为空
- if(cursor.moveToFirst()) {
- // 如果结果不为空,则表示用户名已存在,并且显示 "用户名已存在" 的 Toast 消息
- Toast.makeText(MainActivity2.this,"用户名已存在",Toast.LENGTH_SHORT).show();
- } else {
- // 如果结果为空,则表示用户名不存在,并且执行 SQL 插入语句,将用户名和密码插入数据库
- String SQLstr = "insert into userInfo(uname,psw) values(?,?)";
- db.execSQL(SQLstr,new String[]{uname,psw});
- // 显示 "注册成功" 的 Toast 消息
- Toast.makeText(MainActivity2.this,"注册成功",Toast.LENGTH_SHORT).show();
- // 创建到 MainActivity 类的意图并启动该活动
- Intent intent = new Intent(MainActivity2.this,MainActivity.class);
- startActivity(intent);
- }
- }
- });
- }
- }
复制代码 通过使用 SQLite 数据库,我们成功地实现了 Android 应用步伐中的登录和注册功能。本教程旨在为开辟者提供一个循规蹈矩的指南,资助他们轻松地将这些功能集成到自己的应用步伐中。希望这篇文章能为广大开辟者带来资助,使他们能够为用户提供安全且无缝的登录和注册体验。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |