Browse Source

login app add sqlite support

master
Mangogo 2 months ago
parent
commit
d907c56df2
7 changed files with 215 additions and 20 deletions
  1. +3
    -4
      .idea/gradle.xml
  2. +1
    -1
      build.gradle
  3. +2
    -2
      gradle/wrapper/gradle-wrapper.properties
  4. +1
    -0
      login_app/src/main/java/com/example/login_app/ForgetActivity.java
  5. +61
    -13
      login_app/src/main/java/com/example/login_app/LoginActivity.java
  6. +21
    -0
      login_app/src/main/java/com/example/login_app/bean/User.java
  7. +126
    -0
      login_app/src/main/java/com/example/login_app/database/DBHelper.java

+ 3
- 4
.idea/gradle.xml View File

@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
<compositeConfiguration>
<compositeBuild compositeDefinitionSource="SCRIPT" />
</compositeConfiguration>
<option name="delegatedBuild" value="false" />
<option name="testRunner" value="PLATFORM" />
<option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="modules">
@@ -17,7 +17,6 @@
</set>
</option>
<option name="resolveModulePerSourceSet" value="false" />
<option name="testRunner" value="PLATFORM" />
</GradleProjectSettings>
</option>
</component>

+ 1
- 1
build.gradle View File

@@ -7,7 +7,7 @@ buildscript {
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath 'com.android.tools.build:gradle:3.6.1'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files


+ 2
- 2
gradle/wrapper/gradle-wrapper.properties View File

@@ -1,6 +1,6 @@
#Thu Jan 30 19:05:40 CST 2020
#Tue Mar 03 20:38:49 CST 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip

+ 1
- 0
login_app/src/main/java/com/example/login_app/ForgetActivity.java View File

@@ -39,6 +39,7 @@ public class ForgetActivity extends AppCompatActivity implements View.OnClickLis
int id = v.getId();
if (id == R.id.bt_phone) {
verifyCode = String.format("%06d", (int) (Math.random() * 1000000 % 1000000));
// verifyCode="111111";
// 弹出提醒对话框,提示用户六位验证码数字
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请记住验证码");


+ 61
- 13
login_app/src/main/java/com/example/login_app/LoginActivity.java View File

@@ -22,9 +22,13 @@ import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.login_app.bean.User;
import com.example.login_app.database.DBHelper;
import com.example.login_app.util.TextUtil;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
import java.util.Date;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener, View.OnFocusChangeListener {

private CheckBox ck_remember;
private RadioGroup rg_login;
@@ -42,7 +46,8 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
private String verifyCode;
private String pwd = "111111";
private int mRequestCode = 0;
private SharedPreferences share;
// private SharedPreferences share;
private DBHelper dbHelper;

@Override
protected void onRestart() {
@@ -53,8 +58,11 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == mRequestCode && data != null)
if (requestCode == mRequestCode && data != null) {
pwd = data.getStringExtra("pwd");
dbHelper.openWriteLink();
dbHelper.update(pwd, et_tel.getText().toString());
}
}

@Override
@@ -82,13 +90,28 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList

initSpinner();

share = getSharedPreferences("pwd", MODE_PRIVATE);
String passwd = share.getString("passwd", "");
String phone = share.getString("phone", "");
pwd = passwd.equals("") ? pwd : passwd;
// share = getSharedPreferences("pwd", MODE_PRIVATE);
// String passwd = share.getString("passwd", "");
// String phone = share.getString("phone", "");
// pwd = passwd.equals("") ? pwd : passwd;
// et_tel.setText(phone);
// et_pwd.setText(passwd);

// 给密码编辑框注册一个焦点变化监听器,一旦焦点发生变化,就触发监听器的onFocusChange方法
et_pwd.setOnFocusChangeListener(this);
}

@Override
protected void onResume() {
super.onResume();
dbHelper = DBHelper.getInstance(this, 1);
dbHelper.openWriteLink();
}

et_tel.setText(phone);
et_pwd.setText(passwd);
@Override
protected void onPause() {
super.onPause();
dbHelper.closeLink();
}

@Override
@@ -121,6 +144,9 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
return;
}
if (rb_pwd.isChecked()) { // 密码方式校验
User tmp = dbHelper.queryByPhone(et_tel.getText().toString());
if (tmp != null)
pwd = tmp.password;
if (!et_pwd.getText().toString().equals(pwd)) {
Toast.makeText(this, "请输入正确的密码", Toast.LENGTH_SHORT).show();
} else { // 密码校验通过
@@ -141,10 +167,13 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
et_tel.getText().toString(), whoami[userType]);

if (remeberPwd) {
SharedPreferences.Editor editor = share.edit();
editor.putString("phone", et_tel.getText().toString());
editor.putString("passwd", et_pwd.getText().toString());
editor.commit();
// SharedPreferences.Editor editor = share.edit();
// editor.putString("phone", et_tel.getText().toString());
// editor.putString("passwd", et_pwd.getText().toString());
// editor.commit();
String tmp = (new Date()).toString();

dbHelper.insert(new User(et_tel.getText().toString(), et_pwd.getText().toString(), tmp));
}

// 弹出提醒对话框,提示用户登录成功
@@ -240,4 +269,23 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
}
}
}

// 焦点变更事件的处理方法,hasFocus表示当前控件是否获得焦点。
// 为什么光标进入密码框事件不选onClick?因为要点两下才会触发onClick动作(第一下是切换焦点动作)
@Override
public void onFocusChange(View v, boolean hasFocus) {
String phone = et_tel.getText().toString();
// 判断是否是密码编辑框发生焦点变化
if (v.getId() == R.id.et_pwd) {
// 用户已输入手机号码,且密码框获得焦点
if (phone.length() > 0 && hasFocus) {
// 根据手机号码到数据库中查询用户记录
User info = dbHelper.queryByPhone(phone);
if (info != null) {
// 找到用户记录,则自动在密码框中填写该用户的密码
et_pwd.setText(info.password);
}
}
}
}
}

+ 21
- 0
login_app/src/main/java/com/example/login_app/bean/User.java View File

@@ -0,0 +1,21 @@
package com.example.login_app.bean;

public class User {
public String phone;
public String password;
public String updatetime;

public User() {
}

public User(String phone, String password) {
this.phone = phone;
this.password = password;
}

public User(String phone, String password, String updatetime) {
this.phone = phone;
this.password = password;
this.updatetime = updatetime;
}
}

+ 126
- 0
login_app/src/main/java/com/example/login_app/database/DBHelper.java View File

@@ -0,0 +1,126 @@
package com.example.login_app.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.login_app.bean.User;

import java.util.ArrayList;

public class DBHelper extends SQLiteOpenHelper {
private static final String TAG = "DBHelper";
private static final String DB_NAME = "user.db"; // 数据库的名称
private static final int DB_VERSION = 1; // 数据库的版本号
private static DBHelper mHelper = null; // 数据库帮助器的实例
private SQLiteDatabase mDB = null; // 数据库的实例
public static final String TABLE_NAME = "user_info"; // 表的名称

public DBHelper(Context context, int version) {
super(context, DB_NAME, null, version);
}

public DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}

// 利用单例模式获取数据库帮助器的唯一实例
public static DBHelper getInstance(Context context, int version) {
if (version > 0 && mHelper == null) {
mHelper = new DBHelper(context, version);
} else if (mHelper == null) {
mHelper = new DBHelper(context);
}
return mHelper;
}

// 打开数据库的读连接
public SQLiteDatabase openReadLink() {
if (mDB == null || !mDB.isOpen()) {
mDB = mHelper.getReadableDatabase();
}
return mDB;
}

// 打开数据库的写连接
public SQLiteDatabase openWriteLink() {
if (mDB == null || !mDB.isOpen()) {
mDB = mHelper.getWritableDatabase();
}
return mDB;
}

// 关闭数据库连接
public void closeLink() {
if (mDB != null && mDB.isOpen()) {
mDB.close();
mDB = null;
}
}

@Override
public void onCreate(SQLiteDatabase db) {
Log.d(TAG, "onCreate");
String drop_sql = "DROP TABLE IF EXISTS " + TABLE_NAME + ";";
Log.d(TAG, "drop_sql:" + drop_sql);
db.execSQL(drop_sql);
String create_sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " ("
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
+ "name VARCHAR," + "update_time VARCHAR,"
+ "phone VARCHAR" + ",password VARCHAR"
+ ");";
Log.d(TAG, "create_sql:" + create_sql);
db.execSQL(create_sql);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.d(TAG, "onUpgrade oldVersion=" + oldVersion + ", newVersion=" + newVersion);
if (newVersion > 1) {
//Android的ALTER命令不支持一次添加多列,只能分多次添加
String alter_sql = "ALTER TABLE " + TABLE_NAME + " ADD COLUMN " + "age INTEGER;";
Log.d(TAG, "alter_sql:" + alter_sql);
db.execSQL(alter_sql);
alter_sql = "ALTER TABLE " + TABLE_NAME + " ADD COLUMN " + "married INTEGER;";
Log.d(TAG, "alter_sql:" + alter_sql);
db.execSQL(alter_sql);
}
}

public long insert(User info) {
long result = -1;
ContentValues cv = new ContentValues();
cv.put("phone", info.phone);
cv.put("password", info.password);
result = mDB.insert(TABLE_NAME, "", cv);
return result;
}

public long update(String password ,String phone) {
ContentValues cv = new ContentValues();
cv.put("password", password);
mDB.update(TABLE_NAME, cv, "phone=?", new String[]{phone});
return 1;
}

public User queryByPhone(String phone) {
String sql = String.format("select rowid,_id,update_time," +
"phone,password from %s where phone=%s;", TABLE_NAME, phone);
Cursor cursor = mDB.rawQuery(sql, null);
while (cursor.moveToNext()) {
User info = new User();
info.phone = cursor.getString(3);
info.password = cursor.getString(4);
cursor.close(); // 查询完毕,关闭游标
return info;
}
return null;
}
}

Loading…
Cancel
Save