这是【Android学习之路】之从零开始做一个小项目的阶段性代码展示,目前也实现很多功能啦,待该学期结束后会继续更新,欢迎阅读~ 该安卓小项目是我在近期正式自学1个月左右后完成的,算是对自己的一个考验,同时也是对自己这学期数据库课程的一个收获的体现叭😊 目前该项目中应该存在很多代码的冗余和写的不好的地方,正在改进中~ 希望大家能一起学习、交流,共同进步! 界面可分为初始页面(欢迎页面)、登录页面(管理员、教师、学生)、注册页面(教师、学生)、管理员管理页面(班主任、任课老师、学生、班级)、忘记密码页面、登陆成功后的主页面(分成四个:我的、计划、学习、论坛) 如上,对于角色的选择其实监听的是Relative,这样的话用户点到图片、文字或者是外面的边框都能很好的监听到实现跳转,下面是这个页面中间部分的xml代码,因为调试时发现在一些手机屏幕较短的手机上不能很好地适应,所以我加上了ScrollView让其可上下滑动,增强不同机型的适配效果: 颜色有些刺眼嘻嘻,这里运用相对布局包起来的原因是想让底部的图片位置固定,不随屏幕上下移动,一定程度上增加舒适度。 登录页面的输入框中有对于输入的内容长度的监听和限制,账号我设置的是最多10位,密码最多16位,这里的位数是和注册是紧密相关的,监听以及限制的方法如下: 对了,这里如果触犯了“底线”,输入超过限制将会弹窗提示,但是不是一直弹的(这样不太友好),我就设置了最多三次,当然,如果重新进入该页面的话这个次数是会刷新的~ 最后的方法是用来和我定义的操纵数据库的工具类DBUtils里的相应方法联系起来的,详细的方法内容会在下面的数据库部分展示~ 填写的手机号和学号是通过管理员添加用户的时候同时添加的,所以,这里会对填写的内容进行到数据库中查询并对比,没有的话会弹窗提醒用户先从管理员端绑定,这里以学生注册的功能为例展示java代码: 这里的 关于验证码平台的选择和相关依赖的添加我在之前有介绍过,大家可以直接传送过去看到:【Android学习之路】之从零开始做一个小项目(一) 当然啦,如果你想看完整的代码请前往github看源码哦~代码实在太多了,这里放不下😶 这个部分主要说一下【个人资料】吧~ 功能上,给每个右边的ImageButton加了监听,如下: 方法就展示昵称的吧~ ① 首先是初始页面的点击【管理员】后弹出的AlertDialog,如下所示: 样式XML文件如下: 当然按钮的样式我也进行相应的修改,默认的样式确实在这里不太搭,登录按钮的背景样式如下,分成点击和未点击,这样在点击后会比较明显地看出来点击的效果~ ② 各个不需要输入的AlertDialog: 还有与之对应的让它看起来好看一些的xml文件: 总结起来的话其实就是相当于像写一个界面的布局一样地来设计这个Dialog,不过它的大小比较小而已,而且要注意它的内容、组件和AlertDialog的匹配,不能自己想设计成什么样就设计成什么样,要按照AlertDialog的方法来的~ 该部分我相信大家如果看过我之前记录过程的博客就能知道我使用SQLite数据库的方法啦😁 在该类中重写构造方法以及建表所需的SQL语句 该类是用于将需要用到数据库的页面和数据库联系起来的“中介”,写有很多操纵数据库以及相关数据的方法,在需要用到的页面写上一个调用的方法即可很好地实现😀 这个使用数据库的方法是我之前搜集和查看了很多资料后总结出来,然后结合自身的想法和实践写出来的,挺好用的,按目前使用来看,还未出现bug,至于性能方面有待考量(目前能力还不足以在多种方法间挑选出性能最优的,按目前使用情况和效果来看我自己会首选使用该方法😁) 👀本文其实主要是对该项目做一个大致的代码展示,将同类型的归结在了一起,代码中也做了很多的注释~ 至于具体的一些细节和使用,本文就不涉及啦,在之后会分开写几篇总结篇来经验和心得(写一起实在太多了哈哈),对于文中解释不清楚的地方或是小伙伴们感兴趣的地方欢迎邮箱联系我,一起探讨学习😊 我的邮箱:henry_626@qq.com📋本文目录
写在前面
目前阶段的项目源代码已经上传至github:SchoolSystem
该APP有一个演示视频哦~是我第一次这么正式地录制讲解视频,嘻嘻,而且目前技术还挺菜的,需要继续努力💪
视频发布在B站上,传送:https://www.bilibili.com/video/bv1fD4y1D7P6
因为最近学校要期末考啦,所以就先做到这里,其实,目前的进度下可参考借鉴的代码并不多,主要是思想吧我觉得,如果有小伙伴对其中的某一部分感兴趣可以发邮件给我一起探讨,我看到之后会及时回复的 ~
我的邮箱:henry_626@qq.com
界面及功能部分
当然每个页面的相应功能点击后会跳转至新的页面,这里就不做一一例举啦,我按类型来进行展示:
① 初始页面:
初始页面Activity关键代码://初始登录界面,由此选择老师端、学生端或是管理员 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_init); RelativeLayout chooseTeacher = (RelativeLayout) findViewById(R.id.ChooseTeacher); chooseTeacher.setOnClickListener(new JumpTeacher()); //教师登录页面跳转 RelativeLayout chooseStudent = (RelativeLayout) findViewById(R.id.ChooseStudent); chooseStudent.setOnClickListener(new JumpStudent()); //学生登录页面跳转 RelativeLayout chooseAdmin = (RelativeLayout) findViewById(R.id.ChooseAdmin); chooseAdmin.setOnClickListener(new JumpAdmin()); //管理员页面跳转 } private class JumpTeacher implements View.OnClickListener { @Override public void onClick(View view) { Intent intent = new Intent(); intent.setClass(InitActivity.this, TLoginActivity.class); startActivity(intent); } } private class JumpStudent implements View.OnClickListener { @Override public void onClick(View view) { Intent intent = new Intent(); intent.setClass(InitActivity.this, SLoginActivity.class); startActivity(intent); } } private class JumpAdmin implements View.OnClickListener { @Override public void onClick(View view) { customClick(view); //调用自定义的Dialog } }
<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="90dp"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!--选择了教师--> <RelativeLayout android:id="@+id/ChooseTeacher" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:layout_marginRight="45dp" android:layout_toLeftOf="@+id/canzhao" android:background="@drawable/translucent2"> <ImageView android:id="@+id/teacher_ico" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:src="@mipmap/teacher_ico" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/teacher_ico" android:layout_centerHorizontal="true" android:layout_marginTop="5dp" android:text="我是教师" android:textColor="#576B95" android:textSize="18sp" android:textStyle="bold" /> </RelativeLayout> <!--用作左右两个选择的参照物,保证对称性--> <TextView android:id="@+id/canzhao" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" /> <!--选择了学生--> <RelativeLayout android:id="@+id/ChooseStudent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="45dp" android:layout_marginTop="50dp" android:layout_toRightOf="@+id/canzhao" android:background="@drawable/translucent2"> <ImageView android:id="@+id/student_ico" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:src="@mipmap/student_ico" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/student_ico" android:layout_centerHorizontal="true" android:layout_marginTop="5dp" android:text="我是学生" android:textColor="#576B95" android:textSize="18sp" android:textStyle="bold" /> </RelativeLayout> <!--选择了管理员--> <RelativeLayout android:id="@+id/ChooseAdmin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/ChooseStudent" android:layout_centerHorizontal="true" android:layout_marginTop="25dp" android:background="@drawable/translucent2"> <ImageView android:id="@+id/admin_ico" android:layout_width="40dp" android:layout_height="40dp" android:layout_marginStart="8dp" android:layout_marginLeft="8dp" android:layout_marginEnd="8dp" android:layout_marginRight="8dp" android:src="@drawable/vector_drawable_admin" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_marginEnd="5dp" android:layout_marginRight="5dp" android:layout_toEndOf="@id/admin_ico" android:layout_toRightOf="@id/admin_ico" android:text="管理员" android:textColor="#576B95" android:textSize="16sp" android:textStyle="bold" /> </RelativeLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="300dp" android:layout_marginBottom="40dp" android:background="@drawable/translucent2" android:lineSpacingExtra="4sp" android:text="Copyright © 2020 Henry. All Rights Reserved" android:textAlignment="center" android:textSize="12sp" android:textStyle="bold" /> </RelativeLayout> </ScrollView> <!--底部的图片--> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:adjustViewBounds="true" android:src="@mipmap/welcomebottom"></ImageView> </RelativeLayout>
— — — — — — — — — — — — — — — — — — — — — —
② 登录页面
我这里就以学生端登录页面做介绍啦:
对于页面中的【学生登录】、【注册】、【登录】我都修改了其字体,在项目中加上assets文件夹,子目录创建了fonts文件夹(名字随意),然后把下载到本地的字体TTF文件复制过来
修改的方式及代码如下:btn_Login2 = findViewById(R.id.login2); //获取登录按钮 btn_forget2 = findViewById(R.id.forget2); //获取忘记密码 tv_student = findViewById(R.id.tv_student); //获取学生登录的TextView //修改以下三个组件的字体 Typeface customFont = Typeface.createFromAsset(this.getAssets(), "fonts/Coca-Cola.TTF"); tv_student.setTypeface(customFont); btn_Save2.setTypeface(customFont); btn_Login2.setTypeface(customFont);
/** * 监听账号输入框的文字 **/ private void nameCntentListener() { usernameEditText2.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { Editable editable = usernameEditText2.getText(); int len = editable.length();//输入文本的长度 if (len > 10) { int selEndIndex = Selection.getSelectionEnd(editable); String str = editable.toString(); //截取新字符串 String newStr = str.substring(0, 10); usernameEditText2.setText(newStr); editable = usernameEditText2.getText(); //新字符串长度 int newLen = editable.length(); //旧光标位置超过新字符串长度 if (selEndIndex > newLen) { selEndIndex = editable.length(); } //设置新光标的位置 Selection.setSelection(editable, selEndIndex); if (counter1 < 3) { //事不过三哈哈哈 Toast.makeText(SLoginActivity.this, "账号最多10位吖!", Toast.LENGTH_SHORT).show(); counter1++; } } } @Override public void afterTextChanged(Editable editable) { } }); } /** * 监听密码输入框的文字 **/ private void passwordCntentListener() { passwordEditText2.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { Editable editable = passwordEditText2.getText(); int len = editable.length();//输入文本的长度 if (len > 16) { int selEndIndex = Selection.getSelectionEnd(editable); String str = editable.toString(); String newStr = str.substring(0, 16); passwordEditText2.setText(newStr); editable = passwordEditText2.getText(); int newLen = editable.length(); if (selEndIndex > newLen) { selEndIndex = editable.length(); } Selection.setSelection(editable, selEndIndex); if (counter2 < 3) { //事不过三哈哈哈 Toast.makeText(SLoginActivity.this, "密码最多16位吖!", Toast.LENGTH_SHORT).show(); counter2++; } } } @Override public void afterTextChanged(Editable editable) { } }); }
除去这里提到的对于输入框内容的监听,该Activity的其余部分的java代码如下:public class SLoginActivity extends AppCompatActivity { private EditText usernameEditText2, passwordEditText2; private Button btn_Save2, btn_Login2; private TextView tv_student, btn_forget2; private int counter1 = 0, counter2 = 0; //限制弹窗的次数,防止用户一直不符合规则,弹窗弹个不停 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_slogin); usernameEditText2 = findViewById(R.id.login_username2); //获取添加用户名的编辑框 passwordEditText2 = findViewById(R.id.login_password2); //获取添加密码的编辑框 btn_Save2 = findViewById(R.id.register2); //获取注册按钮 btn_Login2 = findViewById(R.id.login2); //获取登录按钮 btn_forget2 = findViewById(R.id.forget2); //获取忘记密码 tv_student = findViewById(R.id.tv_student); //获取学生登录的TextView //修改以下三个组件的字体 Typeface customFont = Typeface.createFromAsset(this.getAssets(), "fonts/Coca-Cola.TTF"); tv_student.setTypeface(customFont); btn_Save2.setTypeface(customFont); btn_Login2.setTypeface(customFont); nameCntentListener(); passwordCntentListener(); //登录 btn_Login2.setOnClickListener(new View.OnClickListener() { //单击登录按钮查看是否有该用户以及密码是否正确 @Override public void onClick(View v) { String student = usernameEditText2.getText().toString().trim();//获取输入的登录名 StudentBean studentBean = null; //实例化DBUtils,同时调用其方法获取个人信息资料 studentBean = getStudentData(student); if (studentBean == null) { Toast.makeText(SLoginActivity.this, "该账号不存在,请重新输入或注册!", Toast.LENGTH_SHORT).show(); } else { String passwordIn = passwordEditText2.getText().toString().trim(); //获取输入的密码 nameCntentListener(); passwordCntentListener(); if (studentBean.password.equals(MD5Utils.md5(passwordIn))) { //只是比较内容,不能用== Toast.makeText(SLoginActivity.this, "登录成功!" + student + ",欢迎您~", Toast.LENGTH_SHORT).show(); CurrentUser.setUserID(studentBean.ID);//将登录成功的当前登录用户的ID、账号、姓名和昵称存为全局变量 CurrentUser.setUserName(student); CurrentUser.setRealName(studentBean.realName); CurrentUser.setNickName(studentBean.nickName); Intent intent = new Intent(SLoginActivity.this, SMainActivity.class); //登录成功跳转学生主界面 startActivity(intent); finish(); } else { if (passwordIn.equals("")) { Toast.makeText(SLoginActivity.this, "密码不能为空哦!", Toast.LENGTH_SHORT).show(); } else { android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(SLoginActivity.this); final android.app.AlertDialog dialog = builder.create(); final View dialogView = View.inflate(SLoginActivity.this, R.layout.dialog_forget, null); //设置对话框布局 dialog.setView(dialogView); dialog.show(); dialog.getWindow().setBackgroundDrawable(null); final Button btnCancel = (Button) dialogView.findViewById(R.id.btn_Login_cancel); final Button btnForget = (Button) dialogView.findViewById(R.id.btn_Login_forget); btnForget.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); Intent intent = new Intent(SLoginActivity.this, SForgetActivity.class); //跳转至忘记密码界面 startActivity(intent); finish(); } }); btnCancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(SLoginActivity.this, "您选择了重新输入,输入要细心哈", Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }); } } } } }); //注册 btn_Save2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(SLoginActivity.this, SRegisterActivity.class); //跳转至注册页面 startActivity(intent); finish(); } }); //忘记密码 btn_forget2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(SLoginActivity.this, SForgetActivity.class); //跳转至学生端忘记密码页面 startActivity(intent); finish(); } }); } private StudentBean getStudentData(String key) { StudentBean bean = DBUtils.getInstance(this).getStudentInfo(key); return bean; } }
— — — — — — — — — — — — — — — — — — — — — —
③ 注册页面:
教师端和学生端的注册页面有所区别,教师通过手机号注册(发送短信验证码)、学生通过学号注册:
同样的,为了很好地适应不同的手机,我给该页面也加上了ScrollView:<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="90dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!--输入学号--> <EditText android:id="@+id/editStudentNum" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="80dp" android:drawableLeft="@drawable/user_name_icon" android:drawablePadding="3dp" android:ems="12" android:hint=" 输入您的学号" android:inputType="number" android:textColor="#060A00" /> <!--输入注册账号--> <EditText android:id="@+id/add_username2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="35dp" android:ems="12" android:hint="请输入账号" android:inputType="text" android:textColor="#060A00" /> <!--输入密码--> <EditText android:id="@+id/add_password2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="30dp" android:ems="12" android:hint="请输入密码" android:inputType="textPassword" /> <!--确认密码--> <EditText android:id="@+id/confirm_password2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="30dp" android:ems="12" android:hint="请确认密码" android:inputType="textPassword" android:maxLength="16" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="45dp" android:layout_marginTop="15dp" android:layout_marginRight="45dp" android:background="@drawable/translucent4" android:lineSpacingExtra="2sp" android:text="① 学号是管理员添加的,若无学号请先登录管理员身份添加学号n② 账号仅允许字母和数字以及-的组合且不超过10位;密码不能超过16位" android:textSize="14sp" android:textStyle="bold" /> <Button android:id="@+id/buttonRegister2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="20dp" android:layout_marginBottom="40dp" android:text="注册" android:textColor="#FF5722" android:textSize="16sp" /> </LinearLayout> </ScrollView> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:adjustViewBounds="true" android:src="@mipmap/welcomebottom" /> </RelativeLayout>
public class SRegisterActivity extends AppCompatActivity { private Button buttonRegister; private EditText editTextStudentNum, editTextUserName2, editTextPassword2, editConfirmPassword2; private String studentNum, userName2, password2, confirm2; private int counter1 = 0, counter2 = 0; //限制弹窗的次数,防止用户一直不符合规则,弹窗弹个不停 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sregister); buttonRegister = findViewById(R.id.buttonRegister2); editTextStudentNum = findViewById(R.id.editStudentNum); editTextUserName2 = findViewById(R.id.add_username2); editTextPassword2 = findViewById(R.id.add_password2); editConfirmPassword2 = findViewById(R.id.confirm_password2); //修改注册按钮的字体 Typeface customFont = Typeface.createFromAsset(this.getAssets(), "fonts/Coca-Cola.TTF"); buttonRegister.setTypeface(customFont); nameCntentListener(); passwordCntentListener(); /** * 注册 **/ buttonRegister.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { studentNum = editTextStudentNum.getText().toString().trim(); userName2 = editTextUserName2.getText().toString().trim(); password2 = editTextPassword2.getText().toString().trim(); confirm2 = editConfirmPassword2.getText().toString().trim(); StudentBean bean = null; //检查数据库中是否有该学号 bean = getStudentDataByStudentNum(studentNum); if (bean == null) { Toast.makeText(SRegisterActivity.this, "该学号不存在,请填写正确的学号!", Toast.LENGTH_SHORT).show(); } else { if (bean.userName.equals("暂无")) { if (userName2.equals("") || password2.equals("")) { //如果填写的用户名或密码为空时 Toast.makeText(SRegisterActivity.this, "账号或密码为空,请重新填写", Toast.LENGTH_SHORT).show(); } else { if (!LimitName.limitName(userName2)) { Toast.makeText(SRegisterActivity.this, "账号仅允许英文字母、数字和-的组合", Toast.LENGTH_SHORT).show(); } else { nameCntentListener(); passwordCntentListener(); if (!password2.equals(confirm2)) { //两次输入的密码不一致 Toast.makeText(SRegisterActivity.this, "两次输入的密码不一致,请重新输入", Toast.LENGTH_SHORT).show(); editConfirmPassword2.setText(""); //将输入框内容清除 editTextPassword2.setText(""); } else { bean.userName = userName2; bean.nickName = "快给自己起个昵称叭"; bean.sex = "保密"; bean.qq = "暂无"; bean.wechat = "暂无"; bean.motto = "有点懒吖,啥都没写~"; bean.password = MD5Utils.md5(password2); //保存到ID对应的表中 insertStudent(studentNum, bean); Toast.makeText(SRegisterActivity.this, "注册成功!", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(SRegisterActivity.this, SLoginActivity.class); startActivity(intent); finish(); //销毁本Activity } } } } else { Toast.makeText(SRegisterActivity.this, "该学号已绑定过用户!", Toast.LENGTH_SHORT).show(); editTextPassword2.setText(""); //将输入框内容清除 editConfirmPassword2.setText(""); } } } }); } private void insertStudent(String id, StudentBean bean) { DBUtils.getInstance(this).updateStudentInfoAll(id, bean); } private StudentBean getStudentDataByStudentNum(String id) { StudentBean bean = DBUtils.getInstance(this).getStudentInfoByID(id); return bean; }
nameCntentListener();
和passwordCntentListener();
方法同刚刚的展示的登录页面时的方法,稍做修改即可,比较占屏幕,这里就不展示了~
对了,这里关于教师注册时的手机号发送验证码和判断的部分我也做一个展示吧:eh = new EventHandler() { @Override public void afterEvent(int event, int result, Object data) { if (result == SMSSDK.RESULT_COMPLETE) { //回调完成 if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) { //提交验证码成功 runOnUiThread(new Runnable() { @Override public void run() { userName = editTextUserName.getText().toString().trim(); password = editTextPassword.getText().toString().trim(); phoneNum = editTextPhoneNum.getText().toString().trim(); confirm = editConfirmPassword.getText().toString().trim(); //如果填写的用户名或密码为空时 if (userName.equals("") || password.equals("")) { Toast.makeText(TRegisterActivity.this, "账号或密码为空,请重新填写", Toast.LENGTH_SHORT).show(); } else { if (!LimitName.limitName(userName)) { Toast.makeText(TRegisterActivity.this, "账号仅允许英文字母、数字和-的组合", Toast.LENGTH_SHORT).show(); } else { nameCntentListener(); passwordCntentListener(); if (!password.equals(confirm)) { //两次输入的密码不一致 Toast.makeText(TRegisterActivity.this, "两次输入的密码不一致,请重新输入", Toast.LENGTH_SHORT).show(); editConfirmPassword.setText(""); //将输入框内容清除 editTextPassword.setText(""); } else { TeacherBean bean = null; TeacherBean teacherBean = null; bean = getTeacherData(userName); if (bean != null) { //说明就不是暂无了 Toast.makeText(TRegisterActivity.this, "该用户已存在!", Toast.LENGTH_SHORT).show(); editTextUserName.setText(""); //将输入框内容清除 editTextPassword.setText(""); editConfirmPassword.setText(""); editTextCode.setText(""); } else { teacherBean = getTeacherDataByPhoneNum(phoneNum); //因为还未绑定用户名,所以只能用手机号获取到ID绑定用户 teacherBean.userName = userName; teacherBean.nickName = "快给自己起个昵称叭"; teacherBean.sex = "待定"; teacherBean.qq = "待定"; teacherBean.wechat = "待定"; teacherBean.motto = "有点懒吖,啥都没写~"; teacherBean.password = MD5Utils.md5(password); //保存到数据库 insertTeacher(teacherBean.ID, teacherBean); Toast.makeText(TRegisterActivity.this, "注册成功!", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(TRegisterActivity.this, TLoginActivity.class); startActivity(intent); finish(); //销毁本Activity } } } } } }); } else if (event == SMSSDK.EVENT_GET_VOICE_VERIFICATION_CODE) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(TRegisterActivity.this, "语音验证发送", Toast.LENGTH_SHORT).show(); } }); } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) { //获取验证码成功 runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(TRegisterActivity.this, "验证码已发送", Toast.LENGTH_SHORT).show(); } }); } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) { Log.i("test", "test"); } } else { ((Throwable) data).printStackTrace(); Throwable throwable = (Throwable) data; throwable.printStackTrace(); Log.i("1234", throwable.toString()); try { JSONObject obj = new JSONObject(throwable.getMessage()); final String des = obj.optString("detail"); if (!TextUtils.isEmpty(des)) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(TRegisterActivity.this, des, Toast.LENGTH_SHORT).show(); } }); } } catch (JSONException e) { e.printStackTrace(); } } } } ; //注册一个事件回调监听,用于处理SMSSDK接口请求的结果 SMSSDK.registerEventHandler(eh); buttonCode.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { phoneNum = editTextPhoneNum.getText().toString(); if (!phoneNum.isEmpty()) { if (PhoneNumberUtils.checkTel(phoneNum)) { //利用正则表达式检验手机号 TeacherBean bean = null; StudentBean bean2 = null; //检查数据库中该手机号是否已经被绑定 bean = getTeacherDataByPhoneNum(phoneNum); bean2 = getStudentDataByPhoneNum(phoneNum); if (bean == null && bean2 == null) { Toast.makeText(TRegisterActivity.this, "该手机号不可用,需要先以管理员身份添加", Toast.LENGTH_SHORT).show(); } else { if (bean != null) { if (!bean.userName.equals("暂无")) { Toast.makeText(TRegisterActivity.this, "该手机号已绑定过用户!", Toast.LENGTH_SHORT).show(); } else { if (bean2 != null) { if (!bean2.userName.equals("暂无")) { Toast.makeText(TRegisterActivity.this, "该手机号已绑定过用户!", Toast.LENGTH_SHORT).show(); } else { // 获取验证码 SMSSDK.getVerificationCode("86", phoneNum); } } else { //bean2 = null, 则肯定未绑定 // 获取验证码 SMSSDK.getVerificationCode("86", phoneNum); } } } else { if (!bean2.userName.equals("暂无")) { Toast.makeText(TRegisterActivity.this, "该手机号已绑定过用户!", Toast.LENGTH_SHORT).show(); } else { // 获取验证码 SMSSDK.getVerificationCode("86", phoneNum); } } } } else { Toast.makeText(getApplicationContext(), "请输入有效的手机号", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(getApplicationContext(), "请输入手机号", Toast.LENGTH_SHORT).show(); return; } phoneNum = editTextPhoneNum.getText().toString(); } }); buttonRegister.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { code = editTextCode.getText().toString(); if (!code.isEmpty()) { //提交验证码 SMSSDK.submitVerificationCode("86", phoneNum, code); } else { Toast.makeText(getApplicationContext(), "请输入验证码", Toast.LENGTH_SHORT).show(); return; } } });
— — — — — — — — — — — — — — — — — — — — — —
④ 忘记密码页面:
该页面可从【登录】页面直接点击跳转或者因为输入密码错误弹出我自定义的AlertDialog,如选择【忘记密码】则跳转至该页面
自定义AlertDialog一会儿会做介绍,这里的忘记密码页面是通过对用户的手机号发送短信验证码进行修改的,所以对于学生用户来说,需要先绑定手机号(可在登录后的个人信息页面绑定,或是在管理员管理学生的页面绑定),该页面用到的功能和方法实现与上面的教师注册页面很相似的,所以就不展示全部代码啦,挺多的,感兴趣的小伙伴可以直接去看源码部分~
⑤ 管理员登录成功的选择页面以及管理页面展示
对用户的管理页面,我采用的是ListView
和SimpleCursorAdapter
来实现的实时刷新和显示,使用这两个的时候很重要的就是表里一定要有"_id integer primary key autoincrement"
,不然的话它就会罢工给你看的😅,还有就是需要写一个用来刷新的方法,我一起在下面做展示吧:private void initView() { listView = (ListView) findViewById(R.id.studentListview); btn_insert = (Button) findViewById(R.id.btn_insert); btn_search = (Button) findViewById(R.id.btn_search); et_ID = (EditText) findViewById(R.id.et_studentID); et_name = (EditText) findViewById(R.id.et_studentName); et_phone = (EditText) findViewById(R.id.et_studentPhone); et_search = (EditText) findViewById(R.id.et_searchStudent); } private void initEvent() { btn_insert.setOnClickListener(this); btn_search.setOnClickListener(this); openHelper = new DBOpenHelper(this); mDbWriter = openHelper.getWritableDatabase(); mDbReader = openHelper.getReadableDatabase(); simpleCursorAdapter = new SimpleCursorAdapter(StudentActivity.this, R.layout.user_item, null, new String[]{"userID", "realName_u", "phoneNumber_u"}, new int[]{R.id.user_id, R.id.user_name, R.id.user_phone}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); listView.setAdapter(simpleCursorAdapter); //给ListView设置适配器 refreshListview(); //自定义的方法,用于当数据列表改变时刷新ListView } //刷新数据列表 public void refreshListview() { Cursor cursor = mDbWriter.query(DBOpenHelper.USER_INFO, null, "identity=?", new String[]{"1"}, null, null, "userID"); simpleCursorAdapter.changeCursor(cursor); }
⑥ 登录后的四个主页面:
这里切换的方式使用的是Fragment和BottomNavigationView以及NavController
来实现的,所以需要创建相应的menu目录和navigation目录以及相应的xml文件
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tmain); BottomNavigationView navView2 = findViewById(R.id.nav_view2); AppBarConfiguration appBarConfiguration2 = new AppBarConfiguration.Builder( R.id.navigation_tluntan, R.id.navigation_tstudy, R.id.navigation_tplan, R.id.navigation_tme) .build(); NavController navController2 = Navigation.findNavController(this, R.id.nav_host_fragment2); NavigationUI.setupActionBarWithNavController(this, navController2, appBarConfiguration2); NavigationUI.setupWithNavController(navView2, navController2); }
其他的部分大家可以戳b站视频链接前往查看~
【个人资料】页面:
布局上采用了中间的TextView居中,然后左右的组件向它对齐的方式,这样能很好的适应不同手机的屏幕,在长度上,我同样加上了ScrollView,如果屏幕较短可以上下滑动~
布局文件我只展示其中的一部分,因为模板是一样的全贴上太多了:<!--昵称--> <TextView android:layout_width="wrap_content" android:layout_height="21dp" android:layout_marginTop="44dp" android:layout_marginEnd="3dp" android:layout_marginRight="3dp" android:layout_toStartOf="@+id/sInfo_nickname" android:layout_toLeftOf="@+id/sInfo_nickname" android:text="昵称:" android:textSize="15sp" android:textStyle="bold" /> <TextView android:id="@+id/sInfo_nickname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/sInfo_realName" android:layout_centerHorizontal="true" android:layout_marginTop="5dp" android:background="@drawable/translucent8" android:ems="11" android:text="啦啦啦" android:textSize="16sp" /> <ImageButton android:id="@+id/sEditButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="35dp" android:layout_toRightOf="@+id/sInfo_nickname" android:background="@drawable/translucent2" android:src="@mipmap/edit" android:layout_marginStart="10dp" android:layout_toEndOf="@+id/sInfo_nickname" />
//设置监听 ib_nickName.setOnClickListener(new EditNickName()); //昵称编辑 ib_sex.setOnClickListener(new EditSex()); //性别编辑 ib_class.setOnClickListener(new EditClass()); //班级编辑 ib_qq.setOnClickListener(new EditQQ()); //qq编辑 ib_wechat.setOnClickListener(new EditWechat()); //微信编辑 ib_phoneNumber.setOnClickListener(new EditPhoneNumber()); //手机号编辑 ib_motto.setOnClickListener(new EditMotto()); //签名编辑
private class EditNickName implements View.OnClickListener { @Override public void onClick(View view) { android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(SInfoActivity.this); final android.app.AlertDialog dialog = builder.create(); final View dialogView = View.inflate(SInfoActivity.this, R.layout.dialog_et0_layout, null); //设置对话框布局 dialog.setView(dialogView); dialog.show(); dialog.getWindow().setBackgroundDrawable(null); final Button btnCommit = (Button) dialogView.findViewById(R.id.btn_info_commit); final Button btnCancel = (Button) dialogView.findViewById(R.id.btn_info_cancel); btnCommit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { et_content = (EditText) dialogView.findViewById(R.id.et_myInfo); String content = et_content.getText().toString().trim(); if (content.equals("")) { Toast.makeText(SInfoActivity.this, "信息不能为空呀,请重新填写", Toast.LENGTH_SHORT).show(); } else { updateStudent("nickName_s", et_content.getText().toString().trim(), CurrentUser.getuserID()); CurrentUser.setNickName(content); //更新全局变量 Toast.makeText(SInfoActivity.this, "修改成功!", Toast.LENGTH_SHORT).show(); dialog.dismiss(); tv_nickName.setText(content); } } }); btnCancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); } }
自定义的AlertDialog部分
/** * 自定义登录对话框 */ private void customClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(InitActivity.this); final AlertDialog dialog = builder.create(); final View dialogView = View.inflate(InitActivity.this, R.layout.dialog_admin_login, null); //设置对话框布局 dialog.setView(dialogView); dialog.show(); dialog.getWindow().setBackgroundDrawable(null); final Button btnLogin = (Button) dialogView.findViewById(R.id.btn_login); final Button btnCancel = (Button) dialogView.findViewById(R.id.btn_cancel); btnLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { et_name = (EditText) dialogView.findViewById(R.id.et_name); et_pwd = (EditText) dialogView.findViewById(R.id.et_pwd); String name = et_name.getText().toString().trim(); String pwd = et_pwd.getText().toString().trim(); if (name.equals("") || pwd.equals("")) { Toast.makeText(InitActivity.this, "账号和密码都不能为空呀,请重新填写", Toast.LENGTH_SHORT).show(); } else { if (name.equals("Henry")) { if (pwd.equals("666666")) { Intent intent = new Intent(); intent.setClass(InitActivity.this, AdminActivity.class); startActivity(intent); dialog.dismiss(); } else { Toast.makeText(InitActivity.this, "密码不正确,请重新填写", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(InitActivity.this, "账号不正确,请重新填写", Toast.LENGTH_SHORT).show(); } } } }); btnCancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <LinearLayout android:layout_width="280dp" android:layout_height="wrap_content" android:background="@drawable/custom_dialog_title" android:gravity="center" android:minWidth="210dp" android:orientation="horizontal"> <ImageView android:layout_width="28dp" android:layout_height="28dp" android:layout_marginRight="5dp" android:src="@drawable/vector_drawable_yz" android:layout_marginEnd="5dp" /> <TextView android:layout_width="wrap_content" android:layout_height="50dp" android:gravity="center" android:text="管理员验证" android:textColor="#fff" android:textSize="20sp" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/custom_dialog_content" android:orientation="vertical"> <EditText android:id="@+id/et_name" android:layout_width="280dp" android:layout_height="wrap_content" android:padding="15dp" android:hint="请输入您的账户" android:textSize="18sp" /> <EditText android:id="@+id/et_pwd" android:layout_width="280dp" android:layout_height="wrap_content" android:padding="15dp" android:hint="请输入您的密码" android:inputType="numberPassword" android:textSize="18sp" /> </LinearLayout> <LinearLayout android:layout_width="280dp" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:background="@drawable/custom_dialog_bottom" android:minWidth="210dp" android:orientation="horizontal" android:padding="12dp"> <Button android:id="@+id/btn_cancel" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="3dp" android:layout_marginLeft="3dp" android:layout_marginEnd="10dp" android:layout_marginRight="10dp" android:layout_weight="1" android:background="@drawable/custom_dialog_negative" android:text="取消" android:textColor="@android:color/white" android:textSize="16sp" /> <Button android:id="@+id/btn_login" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginEnd="3dp" android:layout_marginRight="3dp" android:layout_weight="1" android:background="@drawable/custom_dialog_positive" android:text="登录" android:textColor="@android:color/white" android:textSize="16sp" /> </LinearLayout> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="https://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <shape> <solid android:color="#009688"/> <corners android:radius="6dp"/> </shape> </item> <item android:state_pressed="false"> <shape> <solid android:color="#009688"/> <stroke android:width="1dp" android:color="#fff"/> <corners android:radius="6dp"/> </shape> </item> </selector>
其实这几个AlertDialog就是大同小异,框架是一样的,就是改一改相应的内容和样式就行啦~
这里以【身份选择】为例展示:/** * 自定义选择对话框 */ private void selectClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(AdminActivity.this); final AlertDialog dialog = builder.create(); final View dialogView = View.inflate(AdminActivity.this, R.layout.dialog_admin_teacher, null); //设置对话框布局 dialog.setView(dialogView); dialog.show(); dialog.getWindow().setBackgroundDrawable(null); final Button btnHead = (Button) dialogView.findViewById(R.id.btn_selectHead); final Button btnTeacher = (Button) dialogView.findViewById(R.id.btn_selectTeacher); btnHead.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(AdminActivity.this, TeacherActivity.class); startActivity(intent); dialog.dismiss(); } }); btnTeacher.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(AdminActivity.this, Teacher2Activity.class); startActivity(intent); dialog.dismiss(); } }); }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <LinearLayout android:layout_width="280dp" android:layout_height="wrap_content" android:background="@drawable/custom_dialog_title" android:gravity="center" android:minWidth="210dp" android:orientation="horizontal"> <ImageView android:layout_width="28dp" android:layout_height="28dp" android:layout_marginEnd="5dp" android:layout_marginRight="5dp" android:src="@drawable/vector_drawable_identity" /> <TextView android:layout_width="wrap_content" android:layout_height="50dp" android:gravity="center" android:text="身份选择" android:textColor="#fff" android:textSize="20sp" /> </LinearLayout> <LinearLayout android:layout_width="280dp" android:layout_height="wrap_content" android:background="@drawable/custom_dialog_content" android:gravity="center" android:minWidth="210dp" android:minHeight="80dp" android:orientation="vertical"> <TextView android:layout_width="280dp" android:layout_height="wrap_content" android:padding="10dp" android:text="请选择您的身份" android:textAlignment="center" android:textSize="16sp" /> </LinearLayout> <LinearLayout android:layout_width="280dp" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:background="@drawable/custom_dialog_bottom" android:minWidth="210dp" android:orientation="horizontal" android:padding="12dp"> <Button android:id="@+id/btn_selectHead" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="3dp" android:layout_marginLeft="3dp" android:layout_marginEnd="10dp" android:layout_marginRight="10dp" android:layout_weight="1" android:background="@drawable/custom_dialog_negative" android:text="班主任" android:textColor="@android:color/white" android:textSize="16sp" /> <Button android:id="@+id/btn_selectTeacher" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginEnd="3dp" android:layout_marginRight="3dp" android:layout_weight="1" android:background="@drawable/custom_dialog_positive" android:text="任课老师" android:textColor="@android:color/white" android:textSize="16sp" /> </LinearLayout> </LinearLayout>
数据库部分
① 首先是继承自SQLiteOpenHelpe
的DBOpenHelper
类:package com.henry.myschoolsystem.ui.login; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DBOpenHelper extends SQLiteOpenHelper { public static final String TEACHER_INFO = "teacherInfo"; public static final String STUDENT_INFO = "studentInfo"; public static final String CLASS = "class"; public static final String COURSE = "course"; public static final String SPLAN = "studentPlan"; public static final String SPLANFINISHED = "studentPlanFinished"; public static final String TPLAN = "teacherPlan"; public static final String TPLANFINISHED = "teacherPlanFinished"; public static final String USER_INFO = "userInfo"; public static final String LOG = "userLog"; // public DBOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { // super(context, name, null, version); //重写构造方法并设置factory为null // } public DBOpenHelper(Context context){ super(context, "mySchool0030.db", null, 1); } @Override public void onCreate(SQLiteDatabase db) { /** * 当该子类被实例化时会创建指定名的数据库,在onCreate中按以下的SQL语句创建相应的表 **/ db.execSQL("CREATE TABLE IF NOT EXISTS " + USER_INFO + "( " + "_id integer primary key autoincrement, " + "userID TEXT UNIQUE NOT NULL, " + "realName_u TEXT NOT NULL, " //真实姓名(教师或学生) + "phoneNumber_u TEXT, " + "identity TEXT NOT NULL," //用来标记该用户是教师还是学生(0:教师,1:学生) + "isHead_u TEXT," //添加的是教师时用来标记是否为班主任(0:不是,1:是) + "classID_u TEXT" //是班主任的话需填写班级ID + ")"); db.execSQL("CREATE TABLE IF NOT EXISTS " + TEACHER_INFO + "( " + "teacherID TEXT PRIMARY KEY, " + "userName_t TEXT NOT NULL, " + "realName_t TEXT NOT NULL, " //真实姓名 + "nickName_t TEXT, " //昵称 + "isHead_t TEXT NOT NULL, " //标记是否为班主任 (0:不是,1:是) + "sex_t TEXT, " + "qq_t TEXT, " + "wechat_t TEXT, " + "motto_t TEXT, " + "phoneNumber_t TEXT NOT NULL, " + "password_t TEXT NOT NULL" + ")"); db.execSQL("CREATE TABLE IF NOT EXISTS " + STUDENT_INFO + "( " + "studentID TEXT PRIMARY KEY, " + "userName_s TEXT NOT NULL, " + "realName_s TEXT NOT NULL, " //真实姓名 + "nickName_s TEXT, " //昵称 + "sex_s TEXT, " + "qq_s TEXT, " + "wechat_s TEXT, " + "motto_s TEXT, " + "phoneNumber_s TEXT, " + "password_s TEXT NOT NULL" + ")"); db.execSQL("CREATE TABLE IF NOT EXISTS " + CLASS + "( " + "_id integer primary key autoincrement, " + "classID TEXT NOT NULL, " + "className TEXT NOT NULL, " + "year TEXT NOT NULL, " //年级 + "studentID_c TEXT, " + "teacherID_c TEXT, " + "teacherName_c TEXT, " //班主任姓名 + "code TEXT NOT NULL " //学生加入班级需要的验证码 + ")"); db.execSQL("CREATE TABLE IF NOT EXISTS " + COURSE + "( " + "courseID TEXT PRIMARY KEY, " + "courseName TEXT NOT NULL, " + "studentID TEXT REFERENCES studentInfo(studentID), " + "teacherName TEXT REFERENCES teacherInfo(realName_t), " //任课教师姓名 + "grade TEXT, " //学生所得分数 + "credit TEXT NOT NULL " //该课程的学分 + ")"); db.execSQL("CREATE TABLE IF NOT EXISTS " + SPLAN + "( " + "_id integer primary key autoincrement, " + "planContent_s TEXT, " //计划的具体内容 + "studentID_p TEXT, " + "deadline_s TEXT" //计划的最终期限 + ")"); db.execSQL("CREATE TABLE IF NOT EXISTS " + SPLANFINISHED + "( " + "_id integer primary key autoincrement, " + "studentID_pf TEXT, " + "finishedTime_s TEXT, " //计划的完成时间 + "finishedContent_s TEXT " //完成的计划内容 + ")"); db.execSQL("CREATE TABLE IF NOT EXISTS " + TPLAN + "( " + "_id integer primary key autoincrement, " + "planContent_t TEXT, " //计划的具体内容 + "TeacherID_p TEXT, " + "deadline_t TEXT " //计划的最终期限 + ")"); db.execSQL("CREATE TABLE IF NOT EXISTS " + TPLANFINISHED + "( " + "_id integer primary key autoincrement, " + "teacherID_pf TEXT, " + "finishedTime_t TEXT, " //计划的完成时间 + "finishedContent_t TEXT " //完成的计划内容 + ")"); db.execSQL("CREATE TABLE IF NOT EXISTS " + LOG + "( " + "_id integer primary key autoincrement, " + "userID_l TEXT NOT NULL, " //登录的用户ID + "userName_l TEXT NOT NULL, " //登录的用户姓名 + "logContent TEXT NOT NULL " //日志的具体内容 + ")"); } @Override // 重写基类的onUpgrade()方法,以便数据库版本更新 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //提示版本更新并输出旧版本信息与新版本信息 System.out.println("---版本更新-----" + oldVersion + "--->" + newVersion); } }
② 数据库工具类DBUtils:package com.henry.myschoolsystem.utils; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.util.Log; import com.henry.myschoolsystem.bean.ClassBean; import com.henry.myschoolsystem.bean.LogBean; import com.henry.myschoolsystem.bean.StudentBean; import com.henry.myschoolsystem.bean.TeacherBean; import com.henry.myschoolsystem.bean.UserBean; import com.henry.myschoolsystem.ui.login.DBOpenHelper; public class DBUtils { private static DBUtils instance = null; private static DBOpenHelper dbHelper; private static SQLiteDatabase db; /** * 构造方法,只有当类被实例化时候调用 * 实例化DBOpenHelper类,从中得到一个可读写的数据库 **/ public DBUtils(Context context) { dbHelper = new DBOpenHelper(context); db = dbHelper.getWritableDatabase(); } /** * 得到这个类的实例 **/ public static DBUtils getInstance(Context context) { if (instance == null) { instance = new DBUtils(context); } return instance; } /** * 给班级添加学生信息(在class表中) **/ public void saveStudentInfo(ClassBean classBean) { ContentValues cv = new ContentValues(); cv.put("classID",classBean.classID); cv.put("className",classBean.className); cv.put("year",classBean.year); cv.put("studentID_c",classBean.studentID); cv.put("teacherID_c",classBean.teacherID); cv.put("teacherName_c",classBean.teacherName); cv.put("code",classBean.code); db.insert(DBOpenHelper.CLASS, null, cv); } /** * 保存日志信息 **/ public void savaLogInfo(LogBean logBean) { ContentValues cv = new ContentValues(); cv.put("userID_l",logBean.userID); cv.put("userName_l",logBean.userName); cv.put("logContent",logBean.time); db.insert(DBOpenHelper.LOG, null, cv); } // --------------------------------------------------------------------------------------------- /** * 通过学号获取班级信息(学生个人信息页面用到) **/ public ClassBean getClassInfoByStudentID(String id) { String sql = "SELECT * FROM " + DBOpenHelper.CLASS + " WHERE studentID_c=?"; Cursor cursor = db.rawQuery(sql, new String[]{id}); ClassBean classBean = null; while (cursor.moveToNext()) { classBean = new ClassBean(); classBean.classID = cursor.getString(cursor.getColumnIndex("classID")); classBean.className = cursor.getString(cursor.getColumnIndex("className")); classBean.year = cursor.getString(cursor.getColumnIndex("year")); classBean.studentID = cursor.getString(cursor.getColumnIndex("studentID_c")); classBean.teacherID = cursor.getString(cursor.getColumnIndex("teacherID_c")); classBean.teacherName = cursor.getString(cursor.getColumnIndex("teacherName_c")); classBean.code = cursor.getString(cursor.getColumnIndex("code")); } cursor.close(); return classBean; } /** * 通过工号获取班级信息(教师个人信息页面用到) **/ public ClassBean getClassDataByTeacherID(String id) { String sql = "SELECT * FROM " + DBOpenHelper.CLASS + " WHERE teacherID_c=?"; Cursor cursor = db.rawQuery(sql, new String[]{id}); ClassBean classBean = null; while (cursor.moveToNext()) { classBean = new ClassBean(); classBean.classID = cursor.getString(cursor.getColumnIndex("classID")); classBean.className = cursor.getString(cursor.getColumnIndex("className")); classBean.year = cursor.getString(cursor.getColumnIndex("year")); classBean.studentID = cursor.getString(cursor.getColumnIndex("studentID_c")); classBean.teacherID = cursor.getString(cursor.getColumnIndex("teacherID_c")); classBean.teacherName = cursor.getString(cursor.getColumnIndex("teacherName_c")); classBean.code = cursor.getString(cursor.getColumnIndex("code")); } cursor.close(); return classBean; } /** * 通过班级ID获取班级信息 **/ public ClassBean getClassInfoByID(String id) { String sql = "SELECT * FROM " + DBOpenHelper.CLASS + " WHERE classID=?"; Cursor cursor = db.rawQuery(sql, new String[]{id}); ClassBean classBean = null; while (cursor.moveToNext()) { classBean = new ClassBean(); classBean.classID = cursor.getString(cursor.getColumnIndex("classID")); classBean.className = cursor.getString(cursor.getColumnIndex("className")); classBean.year = cursor.getString(cursor.getColumnIndex("year")); classBean.studentID = cursor.getString(cursor.getColumnIndex("studentID_c")); classBean.teacherID = cursor.getString(cursor.getColumnIndex("teacherID_c")); classBean.teacherName = cursor.getString(cursor.getColumnIndex("teacherName_c")); classBean.code = cursor.getString(cursor.getColumnIndex("code")); } cursor.close(); return classBean; } /** * 通过工号获取教师资料信息 **/ public TeacherBean getTeacherInfoByID(String id) { String sql = "SELECT * FROM " + DBOpenHelper.TEACHER_INFO + " WHERE teacherID=?"; Cursor cursor = db.rawQuery(sql, new String[]{id}); TeacherBean teacherBean = null; while (cursor.moveToNext()) { teacherBean = new TeacherBean(); //将对应用户名的所有数据从表中动态赋值给bean teacherBean.userName = cursor.getString(cursor.getColumnIndex("userName_t")); teacherBean.realName = cursor.getString(cursor.getColumnIndex("realName_t")); teacherBean.nickName = cursor.getString(cursor.getColumnIndex("nickName_t")); teacherBean.ID = cursor.getString(cursor.getColumnIndex("teacherID")); teacherBean.sex = cursor.getString(cursor.getColumnIndex("sex_t")); teacherBean.qq = cursor.getString(cursor.getColumnIndex("qq_t")); teacherBean.wechat = cursor.getString(cursor.getColumnIndex("wechat_t")); teacherBean.motto = cursor.getString(cursor.getColumnIndex("motto_t")); teacherBean.phoneNumber = cursor.getString(cursor.getColumnIndex("phoneNumber_t")); teacherBean.password = cursor.getString(cursor.getColumnIndex("password_t")); teacherBean.isHead = cursor.getString(cursor.getColumnIndex("isHead_t")); } cursor.close(); return teacherBean; } /** * 通过账号获取教师资料信息 **/ public TeacherBean getTeacherInfo(String teacherName) { String sql = "SELECT * FROM " + DBOpenHelper.TEACHER_INFO + " WHERE userName_t=?"; Cursor cursor = db.rawQuery(sql, new String[]{teacherName}); TeacherBean teacherBean = null; while (cursor.moveToNext()) { teacherBean = new TeacherBean(); //将对应用户名的所有数据从表中动态赋值给bean teacherBean.userName = cursor.getString(cursor.getColumnIndex("userName_t")); teacherBean.realName = cursor.getString(cursor.getColumnIndex("realName_t")); teacherBean.nickName = cursor.getString(cursor.getColumnIndex("nickName_t")); teacherBean.ID = cursor.getString(cursor.getColumnIndex("teacherID")); teacherBean.sex = cursor.getString(cursor.getColumnIndex("sex_t")); teacherBean.qq = cursor.getString(cursor.getColumnIndex("qq_t")); teacherBean.wechat = cursor.getString(cursor.getColumnIndex("wechat_t")); teacherBean.motto = cursor.getString(cursor.getColumnIndex("motto_t")); teacherBean.phoneNumber = cursor.getString(cursor.getColumnIndex("phoneNumber_t")); teacherBean.password = cursor.getString(cursor.getColumnIndex("password_t")); teacherBean.isHead = cursor.getString(cursor.getColumnIndex("isHead_t")); } cursor.close(); return teacherBean; } /** * 通过手机号获取教师资料信息 **/ public TeacherBean getTeacherInfoByPhoneNum(String phone) { String sql = "SELECT * FROM " + DBOpenHelper.TEACHER_INFO + " WHERE phoneNumber_t=?"; Cursor cursor = db.rawQuery(sql, new String[]{phone}); TeacherBean teacherBean = null; while (cursor.moveToNext()) { teacherBean = new TeacherBean(); //将对应用户名的所有数据从表中动态赋值给bean teacherBean.userName = cursor.getString(cursor.getColumnIndex("userName_t")); teacherBean.realName = cursor.getString(cursor.getColumnIndex("realName_t")); teacherBean.nickName = cursor.getString(cursor.getColumnIndex("nickName_t")); teacherBean.ID = cursor.getString(cursor.getColumnIndex("teacherID")); teacherBean.sex = cursor.getString(cursor.getColumnIndex("sex_t")); teacherBean.qq = cursor.getString(cursor.getColumnIndex("qq_t")); teacherBean.wechat = cursor.getString(cursor.getColumnIndex("wechat_t")); teacherBean.motto = cursor.getString(cursor.getColumnIndex("motto_t")); teacherBean.phoneNumber = cursor.getString(cursor.getColumnIndex("phoneNumber_t")); teacherBean.password = cursor.getString(cursor.getColumnIndex("password_t")); teacherBean.isHead = cursor.getString(cursor.getColumnIndex("isHead_t")); } cursor.close(); return teacherBean; } /** * 通过账号获取学生资料信息 **/ public StudentBean getStudentInfo(String studentName) { String sql = "SELECT * FROM " + DBOpenHelper.STUDENT_INFO + " WHERE userName_s=?"; Cursor cursor = db.rawQuery(sql, new String[]{studentName}); StudentBean studentBean = null; while (cursor.moveToNext()) { studentBean = new StudentBean(); //将对应用户名的所有数据从表中动态赋值给bean studentBean.userName = cursor.getString(cursor.getColumnIndex("userName_s")); studentBean.realName = cursor.getString(cursor.getColumnIndex("realName_s")); studentBean.nickName = cursor.getString(cursor.getColumnIndex("nickName_s")); studentBean.sex = cursor.getString(cursor.getColumnIndex("sex_s")); studentBean.qq = cursor.getString(cursor.getColumnIndex("qq_s")); studentBean.wechat = cursor.getString(cursor.getColumnIndex("wechat_s")); studentBean.motto = cursor.getString(cursor.getColumnIndex("motto_s")); studentBean.ID = cursor.getString(cursor.getColumnIndex("studentID")); studentBean.password = cursor.getString(cursor.getColumnIndex("password_s")); studentBean.phoneNumber = cursor.getString(cursor.getColumnIndex("phoneNumber_s")); } cursor.close(); return studentBean; } /** * 通过学号获取学生资料信息 **/ public StudentBean getStudentInfoByID(String id) { String sql = "SELECT * FROM " + DBOpenHelper.STUDENT_INFO + " WHERE studentID=?"; Cursor cursor = db.rawQuery(sql, new String[]{id}); StudentBean studentBean = null; while (cursor.moveToNext()) { studentBean = new StudentBean(); //将对应用户名的所有数据从表中动态赋值给bean studentBean.userName = cursor.getString(cursor.getColumnIndex("userName_s")); studentBean.realName = cursor.getString(cursor.getColumnIndex("realName_s")); studentBean.nickName = cursor.getString(cursor.getColumnIndex("nickName_s")); studentBean.sex = cursor.getString(cursor.getColumnIndex("sex_s")); studentBean.qq = cursor.getString(cursor.getColumnIndex("qq_s")); studentBean.wechat = cursor.getString(cursor.getColumnIndex("wechat_s")); studentBean.motto = cursor.getString(cursor.getColumnIndex("motto_s")); studentBean.ID = cursor.getString(cursor.getColumnIndex("studentID")); studentBean.password = cursor.getString(cursor.getColumnIndex("password_s")); studentBean.phoneNumber = cursor.getString(cursor.getColumnIndex("phoneNumber_s")); } cursor.close(); return studentBean; } /** * 通过手机号获取学生资料信息 **/ public StudentBean getStudentInfoByPhoneNum(String phone) { String sql = "SELECT * FROM " + DBOpenHelper.STUDENT_INFO + " WHERE phoneNumber_s=?"; Cursor cursor = db.rawQuery(sql, new String[]{phone}); StudentBean studentBean = null; while (cursor.moveToNext()) { studentBean = new StudentBean(); //将对应用户名的所有数据从表中动态赋值给bean studentBean.userName = cursor.getString(cursor.getColumnIndex("userName_s")); studentBean.realName = cursor.getString(cursor.getColumnIndex("realName_s")); studentBean.nickName = cursor.getString(cursor.getColumnIndex("nickName_s")); studentBean.sex = cursor.getString(cursor.getColumnIndex("sex_s")); studentBean.qq = cursor.getString(cursor.getColumnIndex("qq_s")); studentBean.wechat = cursor.getString(cursor.getColumnIndex("wechat_s")); studentBean.motto = cursor.getString(cursor.getColumnIndex("motto_s")); studentBean.ID = cursor.getString(cursor.getColumnIndex("studentID")); studentBean.password = cursor.getString(cursor.getColumnIndex("password_s")); studentBean.phoneNumber = cursor.getString(cursor.getColumnIndex("phoneNumber_s")); } cursor.close(); return studentBean; } // --------------------------------------------------------------------------------------------- /** * 通过ID获取管理员资料信息 **/ public UserBean getUserInfoByID(String id) { String sql = "SELECT * FROM " + DBOpenHelper.USER_INFO + " WHERE userID=?"; Cursor cursor = db.rawQuery(sql, new String[]{id}); UserBean userBean = null; while (cursor.moveToNext()) { userBean = new UserBean(); //将对应用户名的所有数据从表中动态赋值给bean userBean.ID = cursor.getString(cursor.getColumnIndex("userID")); userBean.realName = cursor.getString(cursor.getColumnIndex("realName_u")); userBean.phoneNumber = cursor.getString(cursor.getColumnIndex("phoneNumber_u")); userBean.identity = cursor.getString(cursor.getColumnIndex("identity")); userBean.isHead = cursor.getString(cursor.getColumnIndex("isHead_u")); userBean.classID = cursor.getString(cursor.getColumnIndex("classID_u")); } cursor.close(); return userBean; } /** * 通过手机号获取管理员操作的资料信息 **/ public UserBean getUserInfoByPhoneNum(String phone) { String sql = "SELECT * FROM " + DBOpenHelper.USER_INFO + " WHERE phoneNumber_u=?"; Cursor cursor = db.rawQuery(sql, new String[]{phone}); UserBean userBean = null; while (cursor.moveToNext()) { userBean = new UserBean(); //将对应用户名的所有数据从表中动态赋值给bean userBean.ID = cursor.getString(cursor.getColumnIndex("userID")); userBean.realName = cursor.getString(cursor.getColumnIndex("realName_u")); userBean.phoneNumber = cursor.getString(cursor.getColumnIndex("phoneNumber_u")); userBean.identity = cursor.getString(cursor.getColumnIndex("identity")); userBean.isHead = cursor.getString(cursor.getColumnIndex("isHead_u")); userBean.classID = cursor.getString(cursor.getColumnIndex("classID_u")); } cursor.close(); return userBean; } // --------------------------------------------------------------------------------------------- /** * 根据用户名修改教师资料信息,这里的key指代表字段,value表示数值 **/ public void updateTeacherInfo(String key, String value, String userName) { ContentValues cv = new ContentValues(); cv.put(key, value); db.update(DBOpenHelper.TEACHER_INFO, cv, "userName_t=?", new String[]{userName}); } /** * 根据手机号修改教师资料信息,这里的key指代表字段,value表示数值 **/ public void updateTeacherInfoByPhone(String key, String value, String phone) { ContentValues cv = new ContentValues(); cv.put(key, value); db.update(DBOpenHelper.TEACHER_INFO, cv, "phoneNumber_t=?", new String[]{phone}); } /** * 根据ID修改学生资料信息,这里的key指代表字段,value表示数值 **/ public void updateStudentInfoByID(String key, String value, String id) { ContentValues cv = new ContentValues(); cv.put(key, value); db.update(DBOpenHelper.STUDENT_INFO, cv, "studentID=?", new String[]{id}); } /** * 根据手机号修改学生资料信息,这里的key指代表字段,value表示数值 **/ public void updateStudentInfoByPhone(String key, String value, String phone) { ContentValues cv = new ContentValues(); cv.put(key, value); db.update(DBOpenHelper.STUDENT_INFO, cv, "phoneNumber_s=?", new String[]{phone}); } // --------------------------------!!!很重要!!!------------------------------------------------ /** * 注册时的整体修改教师资料信息 **/ public void updateTeacherInfoAll(String id, TeacherBean teacherBean) { ContentValues cv = new ContentValues(); cv.put("userName_t", teacherBean.userName); cv.put("nickName_t", teacherBean.nickName); cv.put("sex_t", teacherBean.sex); cv.put("qq_t", teacherBean.qq); cv.put("wechat_t",teacherBean.wechat); cv.put("motto_t",teacherBean.motto); cv.put("password_t",teacherBean.password); db.update(DBOpenHelper.TEACHER_INFO, cv, "teacherID=?", new String[]{id}); } /** * 注册时的整体修改学生资料信息 **/ public void updateStudentInfoAll(String id, StudentBean studentBean) { ContentValues cv = new ContentValues(); cv.put("userName_s", studentBean.userName); cv.put("nickName_s", studentBean.nickName); cv.put("sex_s", studentBean.sex); cv.put("qq_s", studentBean.qq); cv.put("wechat_s",studentBean.wechat); cv.put("motto_s",studentBean.motto); cv.put("password_s",studentBean.password); db.update(DBOpenHelper.STUDENT_INFO, cv, "studentID=?", new String[]{id}); } /** * 注册时的修改学生与班级的绑定信息 **/ public void updateClassInfoAll(String id, ClassBean classBean) { ContentValues cv = new ContentValues(); cv.put("classID", classBean.classID); cv.put("className", classBean.className); cv.put("year", classBean.year); cv.put("code", classBean.code); cv.put("teacherID_c", classBean.teacherID); cv.put("teacherName_c", classBean.teacherName); db.update(DBOpenHelper.CLASS, cv, "studentID_c=?", new String[]{id}); } }
③ 同时,不可缺少的是bean文件夹:
这里展示一下StudentBean:package com.henry.myschoolsystem.bean; public class StudentBean { public String userName; //账号 public String realName; //真实姓名 public String nickName; //昵称 public String sex; //性别 public String qq; //QQ号 public String wechat; //微信号 public String motto; //个性签名 public String ID; //学生学号 public String password; //密码 public String phoneNumber; //手机号 }
B站项目视频讲解传送:https://www.bilibili.com/video/bv1fD4y1D7P6
github项目源代码: SchoolSystem
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算