今天實際來做看看連接資料庫
登入之後保存進資料庫
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);
}
}
}