Android Studio如何通过结合SQLite数据库实现简单的注册,登录功能。 ...

打印 上一主题 下一主题

主题 518|帖子 518|积分 1554

第一步:创建一个活动DatabaseHelper.java;
  1. package com.example.myapplication;
  2. import android.content.ContentValues;
  3. import android.content.Context;
  4. import android.database.Cursor;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.database.sqlite.SQLiteOpenHelper;
  7. public class DatabaseHelper extends SQLiteOpenHelper {
  8.     private static final String DATABASE_NAME = "user.db";
  9.     private static final int DATABASE_VERSION = 1;
  10.     private static final String TABLE_USERS = "users";
  11.     private static final String COLUMN_ID = "id";
  12.     private static final String COLUMN_USERNAME = "username";
  13.     private static final String COLUMN_PASSWORD = "password";
  14.     private static final String COLUMN_PHONE_NUMBER = "phone_number";
  15.     private static final String COLUMN_ADDRESS = "address";
  16.     private static final String CREATE_TABLE_USERS =
  17.             "CREATE TABLE " + TABLE_USERS + "(" +
  18.                     COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
  19.                     COLUMN_USERNAME + " TEXT NOT NULL UNIQUE, " +
  20.                     COLUMN_PASSWORD + " TEXT NOT NULL, " +
  21.                     COLUMN_PHONE_NUMBER + " TEXT, " +
  22.                     COLUMN_ADDRESS + " TEXT" +
  23.                     ")";
  24.     public DatabaseHelper(Context context) {
  25.         super(context, DATABASE_NAME, null, DATABASE_VERSION);
  26.     }
  27.     @Override
  28.     public void onCreate(SQLiteDatabase db) {
  29.         db.execSQL(CREATE_TABLE_USERS);
  30.     }
  31.     @Override
  32.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  33.         db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
  34.         onCreate(db);
  35.     }
  36.     /*@SuppressLint("Range")*/
  37.     public long insertUser(String username, String password, String phoneNumber, String address) {
  38.         SQLiteDatabase db = this.getWritableDatabase();
  39.         ContentValues values = new ContentValues();
  40.         values.put(COLUMN_USERNAME, username);
  41.         values.put(COLUMN_PASSWORD, password);
  42.         values.put(COLUMN_PHONE_NUMBER, phoneNumber);
  43.         values.put(COLUMN_ADDRESS, address);
  44.         long result = db.insert(TABLE_USERS, null, values);
  45.         db.close();
  46.         return result;
  47.     }
  48.     public boolean checkUserExist(String username) {
  49.         SQLiteDatabase db = this.getReadableDatabase();
  50.         Cursor cursor = db.query(TABLE_USERS, new String[]{COLUMN_ID},
  51.                 COLUMN_USERNAME + "=?", new String[]{username},
  52.                 null, null, null);
  53.         boolean exists = cursor.getCount() > 0;
  54.         cursor.close();
  55.         db.close();
  56.         return exists;
  57.     }
  58.     public boolean checkUserExist(String username, String password) {
  59.         SQLiteDatabase db = this.getReadableDatabase();
  60.         Cursor cursor = db.query(TABLE_USERS, new String[]{COLUMN_ID},
  61.                 COLUMN_USERNAME + "=? AND " + COLUMN_PASSWORD + "=?",
  62.                 new String[]{username, password},
  63.                 null, null, null);
  64.         boolean exists = cursor.getCount() > 0;
  65.         cursor.close();
  66.         db.close();
  67.         return exists;
  68.     }
  69. }
复制代码
第二步:创建一个活动RegisterActivity.java;
  1. package com.example.myapplication;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.text.TextUtils;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.Toast;
  10. public class RegisterActivity extends AppCompatActivity {
  11.     EditText etUsername, etPassword, etPhoneNumber, etAddress;
  12.     Button btnRegister;
  13.     DatabaseHelper dbHelper;
  14.     @Override
  15.     protected void onCreate(Bundle savedInstanceState) {
  16.         super.onCreate(savedInstanceState);
  17.         setContentView(R.layout.activity_register);
  18.         etUsername = findViewById(R.id.etUsername);
  19.         etPassword = findViewById(R.id.etPassword);
  20.         etPhoneNumber = findViewById(R.id.etPhoneNumber);
  21.         etAddress = findViewById(R.id.etAddress);
  22.         btnRegister = findViewById(R.id.btnRegister);
  23.         dbHelper = new DatabaseHelper(this);
  24.         btnRegister.setOnClickListener(new View.OnClickListener() {
  25.             @Override
  26.             public void onClick(View v) {
  27.                 String username = etUsername.getText().toString();
  28.                 String password = etPassword.getText().toString();
  29.                 String phoneNumber = etPhoneNumber.getText().toString();
  30.                 String address = etAddress.getText().toString();
  31.                 if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
  32.                     Toast.makeText(RegisterActivity.this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
  33.                     return;
  34.                 }
  35.                 if (dbHelper.checkUserExist(username)) {
  36.                     Toast.makeText(RegisterActivity.this, "该用户名已被注册", Toast.LENGTH_SHORT).show();
  37.                     return;
  38.                 }
  39.                 long result = dbHelper.insertUser(username, password, phoneNumber, address);
  40.                 if (result != -1) {
  41.                     Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
  42.                     Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
  43.                     startActivity(intent);
  44.                 } else {
  45.                     Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
  46.                 }
  47.             }
  48.         });
  49.     }
  50. }
复制代码
 第三步:创建一个activity_register.xml布局文件;
  1. <LinearLayout
  2.     xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical"
  6.     >
  7.     <TextView
  8.         android:layout_width="wrap_content"
  9.         android:layout_height="wrap_content"
  10.         android:layout_gravity="center"
  11.         android:text="注册"
  12.         android:textSize="24sp"
  13.         android:textStyle="bold" />
  14.     <EditText
  15.         android:id="@+id/etUsername"
  16.         android:layout_width="match_parent"
  17.         android:layout_height="wrap_content"
  18.         android:layout_margin="16dp"
  19.         android:hint="用户名" />
  20.     <EditText
  21.         android:id="@+id/etPassword"
  22.         android:layout_width="match_parent"
  23.         android:layout_height="wrap_content"
  24.         android:layout_margin="16dp"
  25.         android:hint="密码"
  26.         android:inputType="textPassword" />
  27.     <EditText
  28.         android:id="@+id/etPhoneNumber"
  29.         android:layout_width="match_parent"
  30.         android:layout_height="wrap_content"
  31.         android:layout_margin="16dp"
  32.         android:hint="电话号码"
  33.         android:inputType="phone" />
  34.     <EditText
  35.         android:id="@+id/etAddress"
  36.         android:layout_width="match_parent"
  37.         android:layout_height="wrap_content"
  38.         android:layout_margin="16dp"
  39.         android:hint="地址" />
  40.     <Button
  41.         android:id="@+id/btnRegister"
  42.         android:layout_width="wrap_content"
  43.         android:layout_height="wrap_content"
  44.         android:layout_gravity="center"
  45.         android:layout_margin="16dp"
  46.         android:text="注册" />
  47. </LinearLayout>
复制代码
第四步:创建一个活动LoginActivity.java;
该活动登录成功后,会通过代码中的(意图)Intent跳转到MainActivity.java,读者可根据自己的需求更改代码,从而改变跳转的活动文件java的位置。
  1. import android.content.Intent;
  2. import android.os.Bundle;
  3. import android.text.TextUtils;
  4. import android.view.View;
  5. import android.widget.Button;
  6. import android.widget.EditText;
  7. import android.widget.Toast;
  8. import androidx.appcompat.app.AppCompatActivity;
  9. public class LoginActivity extends AppCompatActivity {
  10.     EditText etUsername, etPassword;
  11.     Button btnLogin;
  12.     DatabaseHelper dbHelper;
  13.     @Override
  14.     protected void onCreate(Bundle savedInstanceState) {
  15.         super.onCreate(savedInstanceState);
  16.         setContentView(R.layout.activity_login);
  17.         etUsername = findViewById(R.id.etUsername);
  18.         etPassword = findViewById(R.id.etPassword);
  19.         btnLogin = findViewById(R.id.btnLogin);
  20.         dbHelper = new DatabaseHelper(this);
  21.         btnLogin.setOnClickListener(new View.OnClickListener() {
  22.             @Override
  23.             public void onClick(View v) {
  24.                 String username = etUsername.getText().toString();
  25.                 String password = etPassword.getText().toString();
  26.                 if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
  27.                     Toast.makeText(LoginActivity.this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
  28.                     return;
  29.                 }
  30.                 if (dbHelper.checkUserExist(username, password)) {
  31.                     Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
  32.                         //创建意图
  33.                         Intent intent = new Intent(LoginActivity.this,MainActivity.class);
  34.                         //启动新的activity
  35.                         startActivity(intent);
  36.                 } else {
  37.                     Toast.makeText(LoginActivity.this, "用户名或密码不正确", Toast.LENGTH_SHORT).show();
  38.                 }
  39.             }
  40.         });
  41.     }
  42. }
复制代码
第五步:创建一个activity_login.xml布局文件;
  1. <LinearLayout
  2.     xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical">
  6.     <TextView
  7.         android:layout_width="wrap_content"
  8.         android:layout_height="wrap_content"
  9.         android:layout_gravity="center"
  10.         android:text="登录"
  11.         android:textSize="24sp"
  12.         android:textStyle="bold"  />
  13.     <EditText
  14.         android:id="@+id/etUsername"
  15.         android:layout_width="wrap_content"
  16.         android:layout_height="wrap_content"
  17.         android:layout_margin="16dp"
  18.         android:hint="用户名"  />
  19.     <EditText
  20.         android:id="@+id/etPassword"
  21.         android:layout_width="wrap_content"
  22.         android:layout_height="wrap_content"
  23.         android:layout_margin="16dp"
  24.         android:hint="密码"
  25.         android:inputType="textPassword"  />
  26.    
  27.     <Button
  28.         android:id="@+id/btnLogin"
  29.         android:layout_width="wrap_content"
  30.         android:layout_height="58dp"
  31.         android:layout_gravity="center"
  32.         android:layout_margin="16dp"
  33.         android:text="登录" />
  34. </LinearLayout>
复制代码
若完成上述内容,则有下面的运行结果展示:
且注册成功后通过Intent会自动跳转到登录页面:


完成后,可探求‘’user.db”数据库文件的位置所在:
点击右上角View->Tool Windows->Device Exploer出现如下画面:

点击data->data->"packge包的位置 (活动文件java的代码开头)"->database;可检察到数据库user.db。

右键该user.db,选择save as(保存位于电脑某位置)
导入进软件SQLite Expert Personal 5.4(x64)中,即展示如下:

以上为通过SQLite实现一个简单的注册,登录页面的功能。
若以上存在什么问题,各人可以私信我,大概从评论区说出。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

天津储鑫盛钢材现货供应商

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表