1.在将数据加入数据库之前,我们要先进行判断,通常在网页中我们都是通过js来写控制语句的,
那么在android studio中,如何编写控制语句呢?
其实原理差不多,对于每一个输入框,设置EditText的响应事件,当输入满足情况后,给出提示,最后,
添加一个总的判断输入条件函数,对于整体进行判断,具体代码如下所示
editText1=findViewById(R.id.register_inoutusername); editText2=findViewById(R.id.register_inputclass); editText3=findViewById(R.id.register_inputidnumber); editText4=findViewById(R.id.register_inputpsd); editText5=findViewById(R.id.register_input_aginpsd); //判断姓名是否输入符合规范 editText1.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } public void afterTextChanged(Editable editable) { String ss="^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,15}$";//字母数字6到15位 Pattern pattern = Pattern.compile(ss); Matcher matcher = pattern.matcher(editable.toString()); //int len=editText1.toString().length(); if(matcher.matches()) Toast.makeText(register.this, "姓名符合规范!", Toast.LENGTH_SHORT).show(); } }); //判断班级输入是否符合规范 editText2.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void afterTextChanged(Editable editable) { int len=editable.toString().length(); if(len>4&&len<15) Toast.makeText(register.this, "班级输入符合规范!", Toast.LENGTH_SHORT).show(); } }); //判断学号输入是否规范 editText3.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void afterTextChanged(Editable editable) { String ss="^[0-9]{13}$"; Pattern pattern = Pattern.compile(ss); Matcher matcher = pattern.matcher(editable.toString()); //int len=editable.toString().length(); if(matcher.matches()) Toast.makeText(register.this, "学号输入符合规范!", Toast.LENGTH_SHORT).show(); } }); //判断密码输入是否规范 editText4.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void afterTextChanged(Editable editable) { String ss="^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,21}$";//字母数字6到15位 Pattern pattern = Pattern.compile(ss); Matcher matcher = pattern.matcher(editable.toString()); int len=editable.toString().length(); if (matcher.matches()) Toast.makeText(register.this, "密码格式正确!", Toast.LENGTH_SHORT).show(); } }); //判断两次密码是否输入一致 editText5.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void afterTextChanged(Editable editable) { String s1=editText4.getText().toString(); String s2=editText5.getText().toString(); if (s1.equals(s2)) Toast.makeText(register.this, "两次输入密码一致!", Toast.LENGTH_SHORT).show(); } });
对于五个文本输入框的判断,分别为姓名、班级、学号、密码、二次密码,密码和班级、学号要有相应的正则表达式
2.输入提示写完后,在点击注册按钮后,还要对于整体进行一个判断
代码如下:
private boolean checkall(String s1,String s2,String s3,String s4,String s5){ if(check_class(s2)&&check_idnumber(s3)&&check_password(s4,s5)&&check_username(s1)) { AlertDialog.Builder dialog=new AlertDialog.Builder(register.this); dialog.setTitle("提示"); dialog.setMessage("注册成功!"); dialog.setCancelable(false); dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Intent intent = new Intent(register.this, MainActivity.class); startActivity(intent); } }); dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Intent intent = new Intent(register.this, register.class); startActivity(intent); } }); dialog.show(); return true; } else { AlertDialog.Builder dialog=new AlertDialog.Builder(register.this); dialog.setTitle("提示"); dialog.setMessage("注册失败!"); dialog.setCancelable(false); dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Intent intent = new Intent(register.this, register.class); startActivity(intent); } }); dialog.show(); return false; } } private boolean check_username(String s){ editText1=findViewById(R.id.register_inoutusername); String ss="^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,15}$";//字母数字6到15位 Pattern pattern = Pattern.compile(ss); Matcher matcher = pattern.matcher(s); if(matcher.matches()) return true; else return false; } private boolean check_class(String s){ if(s.length()<4||s.length()>15) return false; else return true; } private boolean check_idnumber(String s){ String ss="^[0-9]{13}$"; Pattern pattern = Pattern.compile(ss); Matcher matcher = pattern.matcher(s); if (matcher.matches()) return true; else return false; } private boolean check_password(String s1,String s2){ String ss="^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,15}$";//字母数字6到15位 Pattern pattern = Pattern.compile(ss); Matcher matcher = pattern.matcher(s1); if(matcher.matches()&&s1.equals(s2)) return true; else return false; } }
成功则跳转至另一界面,失败重新填写
3.数据库的插入操作,在插入之前,要先判断数据库中是否存在此数据,这里我们学号为判断条件,没有时执行插入操作,此外,只要在所有输入判断成功的条件下才能完成插入操作,代码如下所示:
Cursor cursor=db.rawQuery("select* from user where id_number =?",new String[]{s3}); if (cursor.moveToFirst()) { { Toast.makeText(register.this, "已存在用戶! 请重新输入!", Toast.LENGTH_LONG).show(); Intent intent = new Intent(register.this, register.class); startActivity(intent); } cursor.close(); break; } if (checkall(s1,s2,s3,s4,s5)) { ContentValues values=new ContentValues(); values.put("name",s1); values.put("class",s2); values.put("id_number",s3); values.put("password",s4); values.put("type",0); values.put("sign_number",0); //插入表中 //db.insert("user",null,values); dbhelper.insert(db,"user",values); values.clear(); db.close();
4.app界面