当前位置:主页 > 移动开发 > Android代码 >

Android登录注册功能 数据库SQLite验证

时间:2020-12-03 17:52:53 | 栏目:Android代码 | 点击:

本文实例为大家分享了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>

谢谢观看,小编祝大家生活愉快!

您可能感兴趣的文章:

相关文章