接下来跟大家分享的是登陆界面中如何保存密码和用户名到数据库中,同时又如何识别不同的用户登陆情况处理。
首先,在主界面中有一个单选按钮组RadioGroup,它包含了三种情况,学生,老师,管理员,默认登陆用户是学生,利用RadioGroup的setOnCheckedChangeListener来监测是那个类型的用户登陆,这样就实现类对于不同用户登陆提供不同界面的功能。
其次,登陆的时候,需要进行数据的保存,情况有以下几种
1,用户首次登陆,数据库没有相关数据,这时候需要做的事情是判断用户名是否符合相关条件,比如我的是设置,用户名长度大于等于5,然后首次登陆是用户名与密码相同,符合条件则可以实现登陆到相关界面,然后数据保存到数据库
2,当用户以及存在的情况下,就是用户已经是第二次或以上登陆,这时候则利用用户名这个唯一标实去查询数据库中保存得到的密码与用户当前输入的密码是否相同,相同则可以登陆成功,不同则给用户提示。
这里为了方便需要建立一个domain类,用来方便把数据插入到数据库中,因为老师,学生,管理员这三个表结构类似,都是由一个id(是第几个),name(用户名),password(密码)来组成,所以只需要一个domain类来实现就好,里面提供了几个构造方向和类属性的getter和setter方法。
然后为了实现保存数据到数据库表中,需要提供一个service包,里面包含对相关数据库表的查询,添加(实际用到的就这两种功能,如若你又其他对数据库操作的需要可以自行添加),这样就完成了登陆界面的全部功能,代码如下:
domain类的:
public class UserMessage { private Integer id; private String userName;//分别对应密码表中的id,name,password private String passWord; public UserMessage(){}; public UserMessage (Integer id,String userName,String passWord) { super(); this.id = id; this.userName = userName; this.passWord = passWord; } public UserMessage (String userName,String passWord) { this.userName = userName; this.passWord = passWord; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; } }
然后是提供数据库操作的service类,包含对学生密码表,老师密码表,以及管理员密码表,他是根据传入的参数决定对那个表进行操作,大大减少代码量
public class StudentService { <span style="white-space:pre"> </span>public final static String TABLE_UESAMESSAGE = "userMessage"; <span style="white-space:pre"> </span>public final static String TABLE_MANAGERMESSAGE = "managerMessage"; <span style="white-space:pre"> </span>public final static String TABLE_TEACHERMESSAGE = "teacherMessage"; <span style="white-space:pre"> </span>private DataBaSEOpenHelper myDataBaSEOpenHelper; <span style="white-space:pre"> </span> <span style="white-space:pre"> </span>public StudentService (Context context) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>myDataBaSEOpenHelper = new DataBaSEOpenHelper(context); <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>/** <span style="white-space:pre"> </span> * 保存对应用户名的方法 <span style="white-space:pre"> </span> * @param message 包含用户名的信息 <span style="white-space:pre"> </span> * @param table 需要保存的表 <span style="white-space:pre"> </span> */ <span style="white-space:pre"> </span>public void save(UserMessage message,String table) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>sqliteDatabase db = myDataBaSEOpenHelper.getWritableDatabase(); <span style="white-space:pre"> </span>db.execsql("insert into " +table+" (name,password) values(?,?)",<span style="white-space:pre"> </span>new Object[] <span style="white-space:pre"> </span>{ message.getUserName(),message.getPassWord() }); <span style="white-space:pre"> </span>} /** * 查询对应用户名的密码 * @param userName 用户名 * @param table 对应的表 * @return */ <span style="white-space:pre"> </span>public UserMessage quary(String userName,String table) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>sqliteDatabase db = myDataBaSEOpenHelper.getReadableDatabase(); <span style="white-space:pre"> </span>Cursor cursor = db.rawQuery("select * from "+table +" where name=?",<span style="white-space:pre"> </span>new String[] <span style="white-space:pre"> </span>{ userName }); <span style="white-space:pre"> </span>if (cursor.moveToFirst()) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>int studentid = cursor.getInt(cursor.getColumnIndex("id")); <span style="white-space:pre"> </span>String password = cursor.getString(cursor <span style="white-space:pre"> </span>.getColumnIndex("password")); <span style="white-space:pre"> </span>UserMessage userMessage = new UserMessage(studentid,userName,<span style="white-space:pre"> </span>password); <span style="white-space:pre"> </span>return userMessage; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>cursor.close(); <span style="white-space:pre"> </span>return null; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>/** <span style="white-space:pre"> </span> * 更新对应用户名的密码 <span style="white-space:pre"> </span> * @param userName 用户名 <span style="white-space:pre"> </span> * @param table 对应的表 <span style="white-space:pre"> </span> * @return <span style="white-space:pre"> </span> */ <span style="white-space:pre"> </span>public void update(UserMessage message,String table) <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>sqliteDatabase db = myDataBaSEOpenHelper.getReadableDatabase(); <span style="white-space:pre"> </span>db.execsql("UPDATE "+table+" SET password=? WHERE name=?",<span style="white-space:pre"> </span>new Object[] <span style="white-space:pre"> </span>{ message.getPassWord(),message.getUserName() }); <span style="white-space:pre"> </span>} }MainActivity中的代码:
public class MainActivity extends Activity { private Intent intent; private RadioGroup openLoadFrame; private EditText editText_name;//用户名 private EditText editText_password;//密码 private String loader = "学生"; private UserMessage userMessage;// 用于存储各个密码表基本数据单位,之后便于写入表中 private StudentService studentService; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_main); openLoadFrame = (RadioGroup) findViewById(R.id.allFrame_rg); openLoadFrame .setOnCheckedChangeListener(new RadioGroupCheckedListener()); editText_name = (EditText) findViewById(R.id.allFrame_name); editText_password = (EditText) findViewById(R.id.allFrame_password); studentService = new StudentService(this); } private class RadioGroupCheckedListener implements OnCheckedChangeListener { public void onCheckedChanged(RadioGroup group,int checkedId) { if (checkedId == R.id.studentFrame) { loader = "学生"; } else if (checkedId == R.id.teacherFrame) { loader = "老师"; } else if (checkedId == R.id.managerFrame) { loader = "管理员"; } } } @SuppressLint("ShowToast") public void load_allFrame(View v) { String name = editText_name.getText().toString(); String password = editText_password.getText().toString(); userMessage = new UserMessage(name,password); if ("学生".equals(loader))//对学生密码表操作 { // 注釋與manager中的一樣 if (studentService.quary(name,StudentService.TABLE_UESAMESSAGE) == null && password.equals(name) && name.trim().length() >= 5) { studentService.save(userMessage,StudentService.TABLE_UESAMESSAGE); intent = new Intent(MainActivity.this,StudentMainFrame.class); intent.putExtra("name",name); editText_name.setText(""); editText_password.setText(""); startActivity(intent); } else if (studentService.quary(name,StudentService.TABLE_UESAMESSAGE) == null && !password.equals(name)) { intent = new Intent(MainActivity.this,MainActivity.class); startActivity(intent); finish(); Toast.makeText(getApplicationContext(),"注意首次登陆密码与学号一致哦",0) .show(); } else if (name.trim().length() >= 5 && password .equals(studentService.quary(name,StudentService.TABLE_UESAMESSAGE).getPassWord())) { intent = new Intent(MainActivity.this,name); editText_name.setText(""); editText_password.setText(""); startActivity(intent); } else { intent = new Intent(MainActivity.this,MainActivity.class); Toast.makeText(getApplicationContext(),R.string.error,Toast.LENGTH_SHORT).show(); startActivity(intent); finish(); } } else if ("老师".equals(loader))//对老师用户表操作 { if (studentService.quary(name,StudentService.TABLE_TEACHERMESSAGE) == null && password.equals(name) && name.trim().length() >= 5) { studentService.save(userMessage,StudentService.TABLE_TEACHERMESSAGE); intent = new Intent(MainActivity.this,TeacherMainFrame.class); intent.putExtra("teacherName",StudentService.TABLE_TEACHERMESSAGE) == null && !password.equals(name)) { intent = new Intent(MainActivity.this,MainActivity.class); startActivity(intent); Toast.makeText(getApplicationContext(),"注意首次登陆密码与教師号一致哦",0) .show(); finish(); } else if (name.trim().length() >= 5 && password .equals(studentService.quary(name,StudentService.TABLE_TEACHERMESSAGE).getPassWord())) { intent = new Intent(MainActivity.this,R.string.error_teacher,Toast.LENGTH_SHORT).show(); startActivity(intent); finish(); } } else//对管理员用户表操作 { if (studentService.quary(name,StudentService.TABLE_MANAGERMESSAGE) == null && password.equals(name) && password.trim().length() >= 5)// 查询表中是否存在这个账号,不存在而且長度不少于5則執行 { studentService.save(userMessage,StudentService.TABLE_MANAGERMESSAGE); intent = new Intent(MainActivity.this,ManagerMainFrame.class); intent.putExtra("manager_name",StudentService.TABLE_MANAGERMESSAGE) == null && !name.equals(password) && name.trim().length() >= 5)// 不存在而且輸入密碼不相等 { intent = new Intent(MainActivity.this,MainActivity.class); startActivity(intent); Toast.makeText(getApplicationContext(),"注意首次登陆密码与管理員編號一致哦",0) .show(); finish(); } else if (password.equals(studentService.quary(name,StudentService.TABLE_MANAGERMESSAGE) .getPassWord()) && name.trim().length() >= 5)// 存在而且相等 { intent = new Intent(MainActivity.this,name); editText_name.setText(""); editText_password.setText(""); startActivity(intent); } else // 存在不相等 { intent = new Intent(MainActivity.this,Toast.LENGTH_SHORT).show(); startActivity(intent); finish(); } } } }