记住密码功能需求分析:先输入手机号码,然后根据手机号码匹配保存的密码,一个手机号码对应一个密码,从而实现具体手机号码的密码记忆功能。现在运用数据库技术分条存储各用户的登录信息,并支持根据手机号查找登录信息,从而同时记住多个手机号的密码。
(1)声明一个数据库的帮助器对象,然后在活动页面的onResume方法中打开数据库连接,在onPasue方法中关闭数据库连接,示例代码如下:
1.png
(2)登录成功时,如果用户勾选了“记住密码”,就将手机号码及其密码保存至数据库。也就是在loginSuccess方法中增加如下代码:
2.png
(3)再次打开登录页面,用户输入手机号再点击密码框的时候,根据光标切到密码框触发焦点变更事件,获得密码,具体处理逻辑要求重写监听器的onFocusChange方法,重写后的方法代码如下所示:
3.png
App,先打开登录页面,勾选“记住密码”,并确保本次登录成功。然后再次进入登录页面,输入手机号码后光标还停留在手机框,如图所示:
4.png
其中LoginDBHelper的实现代码如下:
public class LoginDBHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "login.db";
private static final String TABLE_NAME = "login_info";
private static final int DB_VERSION = 1;
private static LoginDBHelper mHelper = null;
private SQLiteDatabase mRDB = null;
private SQLiteDatabase mWDB = null;
private LoginDBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
// 利用单例模式获取数据库帮助器的唯一实例
public static LoginDBHelper getInstance(Context context) {
if (mHelper == null) {
mHelper = new LoginDBHelper(context);
}
return mHelper;
}
// 打开数据库的读连接
public SQLiteDatabase openReadLink() {
if (mRDB == null || !mRDB.isOpen()) {
mRDB = mHelper.getReadableDatabase();
}
return mRDB;
}
// 打开数据库的写连接
public SQLiteDatabase openWriteLink() {
if (mWDB == null || !mWDB.isOpen()) {
mWDB = mHelper.getWritableDatabase();
}
return mWDB;
}
// 关闭数据库连接
public void closeLink() {
if (mRDB != null && mRDB.isOpen()) {
mRDB.close();
mRDB = null;
}
if (mWDB != null && mWDB.isOpen()) {
mWDB.close();
mWDB = null;
}
}
// 创建数据库,执行建表语句
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
" phone VARCHAR NOT NULL," +
" password VARCHAR NOT NULL," +
" remember INTEGER NOT NULL);";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void save(LoginInfo info) {
// 如果存在则先删除,再添加
try {
mWDB.beginTransaction();
delete(info);
insert(info);
mWDB.setTransactionSuccessful();
} catch (Exception e) {
e.printStackTrace();
} finally {
mWDB.endTransaction();
}
}
public long delete(LoginInfo info) {
return mWDB.delete(TABLE_NAME, "phone=?", new String[]{info.phone});
}
public long insert(LoginInfo info) {
ContentValues values = new ContentValues();
values.put("phone", info.phone);
values.put("password", info.password);
values.put("remember", info.remember);
return mWDB.insert(TABLE_NAME, null, values);
}
public LoginInfo queryTop() {
LoginInfo info = null;
String sql = "select * from " + TABLE_NAME + " where remember = 1 ORDER BY _id DESC limit 1";
// 执行记录查询动作,该语句返回结果集的游标
Cursor cursor = mRDB.rawQuery(sql, null);
if (cursor.moveToNext()) {
info = new LoginInfo();
info.id = cursor.getInt(0);
info.phone = cursor.getString(1);
info.password = cursor.getString(2);
info.remember = (cursor.getInt(3) == 0) ? false : true;
}
return info;
}
public LoginInfo queryByPhone(String phone) {
LoginInfo info = null;
String sql = "select * from " + TABLE_NAME;
// 执行记录查询动作,该语句返回结果集的游标
Cursor cursor = mRDB.query(TABLE_NAME, null, "phone=? and remember=1", new String[]{phone}, null, null, null);
if (cursor.moveToNext()) {
info = new LoginInfo();
info.id = cursor.getInt(0);
info.phone = cursor.getString(1);
info.password = cursor.getString(2);
info.remember = (cursor.getInt(3) == 0) ? false : true;
}
return info;
}
}
网友评论