Android登录注册功能 数据库SQLite验证
程序员文章站
2023-12-14 17:01:58
本文实例为大家分享了android登录注册功能的具体代码,供大家参考,具体内容如下
展示效果
代码区
mainactivity(登录方法)
pu...
本文实例为大家分享了android登录注册功能的具体代码,供大家参考,具体内容如下
展示效果
代码区
mainactivity(登录方法)
public class mainactivity extends appcompatactivity { @bindview(r.id.edittext) edittext edittext; @bindview(r.id.edittext2) edittext edittext2; @bindview(r.id.button) button button; @bindview(r.id.button2) button button2; @bindview(r.id.activity_main) relativelayout activitymain; public static final string regex_password = "^[a-za-z0-9]{6,16}$";//验证密码是否有特殊符号或长度不满6位 private sqlitedatabase w; private sqlitedatabase r; private mysqlist mys; private list<st> mdata; private string name; private string pass; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); butterknife.bind(this); mys = new mysqlist(this, "zhu_c", null, 1);//使用halper创建数据库 r=mys.getreadabledatabase(); w=mys.getwritabledatabase(); mdata=new arraylist<st>(); cursor query = r.rawquery("select * from user_mo", null); while(query.movetonext()){ int index1 = query.getcolumnindex("name"); int index2 = query.getcolumnindex("pass"); name = query.getstring(index1); pass = query.getstring(index2); mdata.add(new st(0, name, pass)); } } @onclick({r.id.button, r.id.button2}) public void onviewclicked(view view) { switch (view.getid()) { case r.id.button: string name1 = edittext.gettext().tostring().trim(); string pass1 = edittext2.gettext().tostring().trim(); if (name1.equals(name)&&pass1.equals(pass)){ toast.maketext(this,"登录成功",toast.length_short).show(); intent intent = new intent(this, useractivity.class); startactivity(intent); }else{ toast.maketext(this,"账号与密码输入不正确",toast.length_short).show(); } break; case r.id.button2: intent intent1 = new intent(this, zhuactivity.class); startactivity(intent1); break; } } }
mysqlist(创建数据库)
public class mysqlist extends sqliteopenhelper { private string sql="create table if not exists user_mo(_id integer primary key autoincrement,name varchar(30) not null,pass varchar(50) not null)"; public mysqlist(context context, string name, sqlitedatabase.cursorfactory factory, int version) { super(context, name, factory, version); } @override public void oncreate(sqlitedatabase db) { db.execsql(sql); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { // todo auto-generated method stub } }
zhuactivity(注册方法)
public class zhuactivity extends appcompatactivity { @bindview(r.id.edittext3) edittext edittext3; @bindview(r.id.edittext4) edittext edittext4; @bindview(r.id.button3) button button3; @bindview(r.id.activity_zhu) relativelayout activityzhu; public static final string regex_password = "^[a-za-z0-9]{6,16}$";//验证密码是否有特殊符号或长度不满6位 private sqlitedatabase sdb; private mysqlist mys; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_zhu); butterknife.bind(this); mys = new mysqlist(this, "zhu_c", null, 1);//使用halper创建数据库 sdb=mys.getwritabledatabase(); } @onclick(r.id.button3) public void onviewclicked() { //获得账号密码 string name = edittext3.gettext().tostring().trim(); string pass = edittext4.gettext().tostring().trim(); if (name == null || "".equals(name) || pass == null || "".equals(pass)) { toast.maketext(this, "账号与密码不能为空", toast.length_short).show(); } else { string number = edittext3.gettext().tostring(); boolean judge = ismobile(number); string pa = edittext4.gettext().tostring(); boolean judge1 = ispassword(pa); if (judge == true && judge1 == true) { toast.maketext(this, "注册成功", toast.length_short).show(); sdb.execsql("insert into user_mo(name,pass)values('"+name+"','"+pass+"')"); intent intent = new intent(this, mainactivity.class); startactivity(intent);//启动跳转 } else { toast.maketext(this, "手机号码不法与密码不能有特殊符号", toast.length_short).show(); } } } /** * 校验密码 * @param password * @return 校验通过返回true,否则返回false */ public static boolean ispassword(string password) { return pattern.matches(regex_password, password); } /** * 验证手机格式 */ public static boolean ismobile(string number) { /* 移动:134、135、136、137、138、139、150、151、157(td)、158、159、187、188 联通:130、131、132、152、155、156、185、186 电信:133、153、180、189、(1349卫通) 总结起来就是第一位必定为1,第二位必定为3或5或8,其他位置的可以为0-9 */ string num = "[1][358]\\d{9}";//"[1]"代表第1位为数字1,"[358]"代表第二位可以为3、5、8中的一个,"\\d{9}"代表后面是可以是0~9的数字,有9位。 if (textutils.isempty(number)) { return false; } else { //matches():字符串是否在给定的正则表达式匹配 return number.matches(num); } } }
st(工具类)
public class st { private int id; private string name; private string pass; public st(int id, string name, string pass) { this.id = id; this.name = name; this.pass = pass; } public int getid() { return id; } public void setid(int id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getpass() { return pass; } public void setpass(string pass) { this.pass = pass; } @override public string tostring() { return "st{" + "id=" + id + ", name='" + name + '\'' + ", pass='" + pass + '\'' + '}'; } }
activity_zhu(注册布局)
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_zhu" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="sizu.wxrkaoqianmomi.zhuactivity"> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputtype="textpersonname" android:hint="输入手机号" android:gravity="center" android:ems="10" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="57dp" android:id="@+id/edittext3" /> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputtype="textpersonname" android:hint="输入密码" android:gravity="center" android:ems="10" android:layout_below="@+id/edittext3" android:layout_alignleft="@+id/edittext3" android:layout_alignstart="@+id/edittext3" android:layout_margintop="44dp" android:id="@+id/edittext4" /> <button android:text="注册" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/edittext4" android:layout_centerhorizontal="true" android:layout_margintop="88dp" android:id="@+id/button3" /> </relativelayout>
activity_main(登录布局)
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="sizu.wxrkaoqianmomi.mainactivity"> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputtype="textpersonname" android:hint="输入手机号" android:gravity="center" android:ems="10" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="33dp" android:id="@+id/edittext" /> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputtype="textpassword" android:ems="10" android:hint="输入密码" android:gravity="center" android:layout_margintop="24dp" android:id="@+id/edittext2" android:layout_below="@+id/edittext" android:layout_alignleft="@+id/edittext" android:layout_alignstart="@+id/edittext" /> <button android:text="登录" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/edittext2" android:layout_alignleft="@+id/edittext2" android:layout_alignstart="@+id/edittext2" android:layout_margintop="85dp" android:id="@+id/button" /> <button android:text="注册" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_aligntop="@+id/button" android:layout_alignright="@+id/edittext2" android:layout_alignend="@+id/edittext2" android:id="@+id/button2" /> </relativelayout>
谢谢观看,小编祝大家生活愉快!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。