实现登录注册页面(创建本地数据库,对注册的账户密码进行存储)
写在前面: 本文实现了登录注册页面的开发,创建了本地数据库,存储注册的账户密码。注册账户为手机号,对账户为手机号进行了正则化验证。登录成功跳转至主页面。
登录注册的业务流程图

页面展示
 
源码如下:
首先说一下,项目部署是在原有项目新建两个activity(项目右键–new–activity–empty activity):LoginActivity(登录页面布局文件.xml文件和登录.java文件)和RegisterActivity(注册页面布局文件.XML和注册.java文件)两个活动,并新建DBOpenHelper.java(main文件夹右键–new–.java)类(这个类是封装数据库的类)。
1.登录页面部署activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity">
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入手机号"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:gravity="center_vertical"
android:textSize="20sp"/>
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:textSize="20sp"
/>
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
/>
<Button
android:id="@+id/btn_register1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="还没有账号?点击注册"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
/>
<ListView
android:id="@+id/test_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
2.注册页面部署activity_register.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".RegisterActivity">
<EditText
android:id="@+id/et_register_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入账号"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"/>
<EditText
android:id="@+id/et_register_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"/>
<EditText
android:id="@+id/et_again_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请再次输入密码进行确认"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/btn_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击注册"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
/>
</LinearLayout>
3.新建数据库java文件,DBOpenHelper.java
新建数据库,用来存储注册的用户名和密码
package com.domain.mainView;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBOpenHelper extends SQLiteOpenHelper {
final String CREATE_USER_SQL=
"create table user(_id integer primary " + "key autoincrement , username, password)";
public DBOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){
super(context,name,null,version);
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL(CREATE_USER_SQL);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
System.out.println("---版本更新----"+oldVersion+"--->"+newVersion);
}
}
4.注册页面,RegisterActivity.java
package com.domain.mainView;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegisterActivity extends AppCompatActivity {
public static final int RESULT_CODE_REGISTER=0;
private Button btn_register;
private EditText et_register_username,et_register_password,et_again_password;
private DBOpenHelper dbOpenHelper;
String et_name;
String et_password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
btn_register=(Button) findViewById(R.id.btn_register);
et_register_username= findViewById(R.id.et_register_username);
et_register_password=findViewById(R.id.et_register_password);
et_again_password=findViewById(R.id.et_again_password);
dbOpenHelper=new DBOpenHelper(RegisterActivity.this,"user.db",null,1);
btn_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String et_name=et_register_username.getText().toString();
String et_password=et_register_password.getText().toString();
String et_confirm=et_again_password.getText().toString();
if(TextUtils.isEmpty(et_name)){
Toast.makeText(RegisterActivity.this,"用户名不能为空!",Toast.LENGTH_SHORT).show();
}else if(!isTelPhoneNumber(et_name)){
Toast.makeText(RegisterActivity.this,"请输入正确的手机号码!",Toast.LENGTH_SHORT).show();
} else if(TextUtils.isEmpty(et_password)){
Toast.makeText(RegisterActivity.this,"密码不能为空!",Toast.LENGTH_SHORT).show();
}else if(!TextUtils.equals(et_password,et_confirm)){
Toast.makeText(RegisterActivity.this,"密码不一致!",Toast.LENGTH_SHORT).show();
} else{
insertData(dbOpenHelper.getReadableDatabase(),et_name,et_password);
Toast.makeText(RegisterActivity.this,"注册成功!",Toast.LENGTH_SHORT).show();
}
RegisterActivity.this.finish();
}
});
}
public static boolean isTelPhoneNumber(String mobile) {
if (mobile != null && mobile.length() == 11) {
Pattern pattern = Pattern.compile("^1[3|4|5|6|7|8|9][0-9]\\d{8}$");
Matcher matcher = pattern.matcher(mobile);
return matcher.matches();
}else{
return false;
}
}
private void insertData(SQLiteDatabase readableDatabase, String username1, String password1){
ContentValues values=new ContentValues();
values.put("username",username1);
values.put("password",password1);
readableDatabase.insert("user",null,values);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (dbOpenHelper != null) {
dbOpenHelper.close();
}
}
}
5.登录页面,LoginActivity.java
package com.domain.mainView;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LoginActivity extends AppCompatActivity {
private Button btn_login;
private EditText et_password,et_userName;
private DBOpenHelper dbOpenHelper;
String dbpassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ListView test_text=(ListView)findViewById(R.id.test_text);
initView();
dbOpenHelper=new DBOpenHelper(LoginActivity.this,"user.db",null,1);
Button btn_register1=(Button)findViewById(R.id.btn_register1);
btn_register1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), RegisterActivity.class);
startActivity(intent);
}
});
btn_login=(Button)findViewById(R.id.btn_login);
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String etpassword=et_password.getText().toString();
String key=et_userName.getText().toString();
Cursor cursor=dbOpenHelper.getReadableDatabase().query("user",null,"username = ?",new String[]{key},null,null,null);
ArrayList<Map<String,String>> resultList =new ArrayList<Map<String,String>>();
while(cursor.moveToNext()){
Map<String,String> map=new HashMap<>();
map.put("username",cursor.getString(1));
map.put("password",cursor.getString(2));
resultList.add(map);
dbpassword=map.get("password");
}
if(!isTelPhoneNumber(key)){
Toast.makeText(LoginActivity.this,"请输入正确的手机号!",Toast.LENGTH_SHORT).show();
}else if (resultList == null || resultList.size() == 0) {
Toast.makeText(LoginActivity.this,
"该用户名未注册,请先注册", Toast.LENGTH_LONG).show();
} else {
SimpleAdapter simpleAdapter= new SimpleAdapter(LoginActivity.this, resultList,
R.layout.userdata_main, new String[]{"username", "password"}, new int[]{R.id.result_name, R.id.result_grade});
test_text.setAdapter(simpleAdapter);
if(etpassword.equals(dbpassword)){
Toast.makeText(LoginActivity.this,"登陆成功!",Toast.LENGTH_SHORT).show();
Intent intent=new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);
LoginActivity.this.finish();
}else{
Toast.makeText(LoginActivity.this,"密码错误!",Toast.LENGTH_SHORT).show();
}
};
}
});
}
public static boolean isTelPhoneNumber(String mobile) {
if (mobile != null && mobile.length() == 11) {
Pattern pattern = Pattern.compile("^1[3|4|5|6|7|8|9][0-9]\\d{8}$");
Matcher matcher = pattern.matcher(mobile);
return matcher.matches();
}else{
return false;
}
}
private void initView(){
btn_login=findViewById(R.id.btn_login);
et_userName=findViewById(R.id.et_username);
et_password=findViewById(R.id.et_password);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (dbOpenHelper != null) {
dbOpenHelper.close();
}
}
}
6.这里是我登录页面上最下面有个测试窗口布局调用的这个XML, (这个窗口是用来显示,输入框输入的用户名与之匹配的数据库的结果的)userdata_main.XML(res文件夹–右键–new–layoutxml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/result_name"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/result_grade"/>
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
tips:要设置下点进APP就是Login登录页面哦在AndroidManifest.XML里面
<activity
android:name=".LoginActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
/>
/*注意!!!这里同时要把mainactivity的exported属性改为false 并删除<intent-filter>属性*/
<activity
android:name=".MainActivity"
android:exported="false"/>
登录注册的所有内容都在这里啦!码字不易,来过的点个赞吧! 还有还有,Android新手一枚,实现的功能比较简单,也有很多问题还没考虑到, 有什么意见欢迎多多指教,有什么问题也欢迎留在评论区沟通交流哦! 点击查看商城主页面底部导航栏的创建:使用Bottom Navigation Activity+Fragment+ViewPager实现底部导航栏
|