基于SQLite的Android登录APP
该登录APP主要包括三个模块:
1、登录:用户选择登录方式、登录身份,输入账号密码,完成登录。
2、忘记密码:用户输入新密码及验证码修改登录密码。
3、个人信息:用户完成登录后设置个人信息并显示。
使用控件:
1、单选按钮RadioButton:区分是密码登录还是验证码登录。
2、下拉框Spinner:区分是个人用户还是公司用户。
3、编辑框EditText:输入手机号和密码(或验证码)。
4、复选框CheckBox:判断是否记住密码。
5、相对布局RelativeLayout:界面的整体布局,方便将各个控件按照相对位置摆放。
6、框架布局FrameLayout:在框架布局中后面添加的子视图会把之前的子视图覆盖掉,一般用于需要重叠显示的场合。用于实现忘记密码按钮和密码输入框的叠加。
采用的存储方式
1、共享参数SharedPreferences:
是Android的一个轻量级存储工具,采用的存储结构是Key-Value的键值对方式,类似于Java的Properties类,都是把Key-Value的键值对保存在配置文件中,不同的是Properties的文件内容是Key=Value的形式,而SharedPreferences的存储介质是符合XML规范的配置文件。本案例中用于保存用户的账号和密码。
2、数据库SQLite:
是一个小巧的嵌入式数据库。本案例中用于存储用户的个人信息。
成果展示:

界面设计:
1. 登录界面

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity"
android:paddingTop="10dp"
android:padding="8dp">
<RadioGroup
android:id="@+id/rg_login_way"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<RadioButton
android:id="@+id/rb_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码登录"
android:textSize="25sp" />
<RadioButton
android:id="@+id/rb_checkcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="验证码登录"
android:layout_marginLeft="50dp"
android:textSize="25sp" />
</RadioGroup>
<TextView
android:id="@+id/tv_shenfen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是:"
android:layout_below="@+id/rg_login_way"
android:textSize="25sp"
android:layout_marginTop="40dp"
android:textColor="@color/black"
android:layout_marginLeft="30dp"
android:layout_alignRight="@+id/tv_phonenum"/>
<Spinner
android:id="@+id/sp_shenfen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/tv_shenfen"
android:layout_alignBottom="@+id/tv_shenfen"
android:spinnerMode="dialog"/>
<TextView
android:id="@+id/tv_phonenum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_shenfen"
android:text="手机号码:"
android:textSize="25sp"
android:textColor="@color/black"
android:layout_marginTop="40dp"/>
<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignBaseline="@id/tv_phonenum"
android:layout_toRightOf="@+id/tv_phonenum"
android:background="@drawable/eb_selector"
android:textSize="25sp"
android:hint="请输入手机号码"
android:inputType="number" />
<TextView
android:id="@+id/tv_psw"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="登录密码:"
android:layout_below="@id/tv_phonenum"
android:textSize="25sp"
android:layout_marginTop="40dp"
android:textColor="@color/black"/>
<FrameLayout
android:id="@+id/fm_psw"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_toRightOf="@id/tv_psw"
android:layout_alignBottom="@+id/tv_psw"
android:layout_alignLeft="@+id/et_phone">
<EditText
android:id="@+id/et_psw"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="请输入密码"
android:textSize="25sp"
android:background="@drawable/eb_selector" />
<Button
android:id="@+id/btn_pswforget"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="忘记密码"
android:textSize="25sp"
android:background="@color/darkgray"
android:padding="10dp"
android:layout_gravity="end"/>
</FrameLayout>
<CheckBox
android:id=机号" + info.phone+info.password+info.name+info.update_time+info.married);
mHelper.insert(info);
Intent intent = new Intent(InfoWriteActivity.this, InfoReadActivity.class);
startActivity(intent);
showToast("数据已写入SQLite数据库");
}
}
private void showToast(String desc) {
Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
}
public static void startHome(Context mContext) {
Intent intent = new Intent(mContext, InfoWriteActivity.class);
mContext.startActivity(intent);
}
}
InfoReadActivity
package com.example.helloworld;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class InfoWriteActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "huahua";
private UserDBHelper mHelper;
private EditText et_name;
private EditText et_age;
private EditText et_height;
private EditText et_weight;
private boolean Married = false;
private String phone;
private String password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shared_preferences);
et_name = findViewById(R.id.et_name);
et_age = findViewById(R.id.et_age);
et_height = findViewById(R.id.et_height);
et_weight = findViewById(R.id.et_weight);
findViewById(R.id.btn_save).setOnClickListener(this);
SharedPreferences sps = getSharedPreferences("Login",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sps.edit();
phone = sps.getString("phone","");
password = sps.getString("password","");
ArrayAdapter<String> typeAdapter = new ArrayAdapter<String>(this, R.layout.item_dropdown, typeArray);
typeAdapter.setDropDownViewResource(R.layout.item_dropdown);
Spinner sp_married = findViewById(R.id.sp_married);
sp_married.setAdapter(typeAdapter);
sp_married.setPrompt("请选择婚姻状况");
sp_married.setSelection(0);
sp_married.setOnItemSelectedListener(new TypeSelectedListener());
}
private String[] typeArray = {"未婚", "已婚"};
class TypeSelectedListener implements AdapterView.OnItemSelectedListener {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Married = (arg2 == 0) ? false : true;
}
public void onNothingSelected(AdapterView<?> arg0) {
}
}
@Override
protected void onStart() {
super.onStart();
SQLiteDatabase mDB = getApplicationContext().openOrCreateDatabase("user.db", Context.MODE_PRIVATE, null);
mHelper = UserDBHelper.getInstance(this, 1);
mHelper.openWriteLink();
}
@Override
protected void onStop() {
super.onStop();
mHelper.closeLink();
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_save) {
String name = et_name.getText().toString();
String age = et_age.getText().toString();
String height = et_height.getText().toString();
String weight = et_weight.getText().toString();
if (name == null || name.length() <= 0) {
showToast("请先填写姓名");
return;
}
if (age == null || age.length() <= 0) {
showToast("请先填写年龄");
return;
}
if (height == null || height.length() <= 0) {
showToast("请先填写身高");
return;
}
if (weight == null || weight.length() <= 0) {
showToast("请先填写体重");
return;
}
UserInfo info = new UserInfo();
info.name = name;
info.age = Integer.parseInt(age);
info.height = Long.parseLong(height);
info.weight = Float.parseFloat(weight);
info.married = Married;
info.phone = phone;
info.password = password;
info.update_time = DateUtil.getNowDate(DateUtil.DatePattern.ALL_TIME);
Log.d(TAG, "onClick: 手机号" + info.phone+info.password+info.name+info.update_time+info.married);
mHelper.insert(info);
Intent intent = new Intent(InfoWriteActivity.this, InfoReadActivity.class);
startActivity(intent);
showToast("数据已写入SQLite数据库");
}
}
private void showToast(String desc) {
Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
}
public static void startHome(Context mContext) {
Intent intent = new Intent(mContext, InfoWriteActivity.class);
mContext.startActivity(intent);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持社区。 |