今天实际来做看看连接资料库
登入之后保存进资料库
private void loginSuccess() {
String desc = String.format("手机号码:%s通过登入验证,点击确定返回验证页面",et_phone.getText().toString());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("验证成功");
builder.setMessage(desc);
builder.setPositiveButton("确认", (dialogInterface, i) -> {
finish();
});
builder.setNegativeButton("验证其他号码",null);
AlertDialog dialog = builder.create();
dialog.show();
//保存至数据库
Logininfo info =new Logininfo();
info.phone = et_phone.getText().toString();
info.password = et_password.getText().toString();
info.remember = ck_remember.isChecked();
mHelper.save(info);
}
开始前先打开读写然后reload之前记住的密码
@Override
protected void onStart() {
super.onStart();
mHelper = LoginDBHelper.getInstance(this);
mHelper.openReadLink();
mHelper.openWriteLink();
reload();
}
reload
private void reload() {
Logininfo info = mHelper.queryTop();
if(info != null&&info.remember){
et_phone.setText(info.phone);
et_password.setText(info.password);
ck_remember.setChecked(true);
}
}
还有侦测资料库内有没有匹配的电话号码
@Override
public void onFocusChange(View view, boolean b) {
if(view.getId() == R.id.et_password&&b){
Logininfo info = mHelper.queryByPhone(et_phone.getText().toString());
if(info!=null){
et_password.setText(info.password);
ck_remember.setChecked(info.remember);
}else{
et_password.setText("");
ck_remember.setChecked(false);
}
}
}