實(shí)現(xiàn)登錄注冊(cè)頁面(創(chuàng)建本地?cái)?shù)據(jù)庫,對(duì)注冊(cè)的賬戶密碼進(jìn)行存儲(chǔ))
寫在前面:
本文實(shí)現(xiàn)了登錄注冊(cè)頁面的開發(fā),創(chuàng)建了本地?cái)?shù)據(jù)庫,存儲(chǔ)注冊(cè)的賬戶密碼。注冊(cè)賬戶為手機(jī)號(hào),對(duì)賬戶為手機(jī)號(hào)進(jìn)行了正則化驗(yàn)證。登錄成功跳轉(zhuǎn)至主頁面。
20221028-實(shí)現(xiàn)登錄注冊(cè)功能
登錄注冊(cè)的業(yè)務(wù)流程圖
頁面展示
源碼如下:
首先說一下,項(xiàng)目部署是在原有項(xiàng)目新建兩個(gè)activity(項(xiàng)目右鍵–new–activity–empty activity):LoginActivity(登錄頁面布局文件.xml文件和登錄.java文件)和RegisterActivity(注冊(cè)頁面布局文件.XML和注冊(cè).java文件)兩個(gè)活動(dòng),并新建DBOpenHelper.java(main文件夾右鍵–new–.java)類(這個(gè)類是封裝數(shù)據(jù)庫的類)。
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">
<!-- 不能輸入多行,要做數(shù)字判斷-->
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請(qǐng)輸入手機(jī)號(hào)"
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="請(qǐng)輸入密碼"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:textSize="20sp"
/>
<!-- 跳轉(zhuǎn)到mainactivity-->
<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="還沒有賬號(hào)?點(diǎn)擊注冊(cè)"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
/>
<!-- 測(cè)試框 顯示數(shù)據(jù)庫存儲(chǔ)的內(nèi)容-->
<!-- 登錄頁面的最下面,設(shè)置了個(gè)通過輸入的用戶名調(diào)用數(shù)據(jù)庫結(jié)果的測(cè)試的listview欄-->
<ListView
android:id="@+id/test_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
2.注冊(cè)頁面部署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="請(qǐng)輸入賬號(hào)"
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="請(qǐng)輸入密碼"
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="請(qǐng)?jiān)俅屋斎朊艽a進(jìn)行確認(rèn)"
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="點(diǎn)擊注冊(cè)"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
/>
</LinearLayout>
3.新建數(shù)據(jù)庫java文件,DBOpenHelper.java
新建數(shù)據(jù)庫,用來存儲(chǔ)注冊(cè)的用戶名和密碼
package com.domain.mainView;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBOpenHelper extends SQLiteOpenHelper {
//定義創(chuàng)建用戶數(shù)據(jù)表的SQL語句 主鍵user數(shù)據(jù)庫表 username和password字段
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
//數(shù)據(jù)庫第一次創(chuàng)建時(shí)被調(diào)用
public void onCreate(SQLiteDatabase db){
//創(chuàng)建用戶列表 execSQL執(zhí)行修改數(shù)據(jù)庫內(nèi)容的SQL語句
db.execSQL(CREATE_USER_SQL);
}
@Override
//版本號(hào)發(fā)生改變時(shí)使用
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//提示版本更新
System.out.println("---版本更新----"+oldVersion+"--->"+newVersion);
}
}
4.注冊(cè)頁面,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;
/*數(shù)據(jù)庫成員變量*/
private DBOpenHelper dbOpenHelper;
String et_name;
String et_password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
//注冊(cè)按鈕
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);
/*實(shí)例化數(shù)據(jù)庫變量dbOpenHelper*/
dbOpenHelper=new DBOpenHelper(RegisterActivity.this,"user.db",null,1);
btn_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//獲取三個(gè)編輯框的內(nèi)容
String et_name=et_register_username.getText().toString();
String et_password=et_register_password.getText().toString();
String et_confirm=et_again_password.getText().toString();
//判斷異常情況彈窗
//編輯框?yàn)榭?/span>
if(TextUtils.isEmpty(et_name)){
Toast.makeText(RegisterActivity.this,"用戶名不能為空!",Toast.LENGTH_SHORT).show();
//對(duì)用戶名進(jìn)行手機(jī)號(hào)正則化驗(yàn)證,調(diào)用下面寫的idTelPhoneNumber方法
}else if(!isTelPhoneNumber(et_name)){
Toast.makeText(RegisterActivity.this,"請(qǐng)輸入正確的手機(jī)號(hào)碼!",Toast.LENGTH_SHORT).show();
} else if(TextUtils.isEmpty(et_password)){
Toast.makeText(RegisterActivity.this,"密碼不能為空!",Toast.LENGTH_SHORT).show();
//兩次密碼框內(nèi)容不一致
}else if(!TextUtils.equals(et_password,et_confirm)){
Toast.makeText(RegisterActivity.this,"密碼不一致!",Toast.LENGTH_SHORT).show();
} else{
//存儲(chǔ)注冊(cè)的用戶名和密碼 把賬號(hào)密碼存儲(chǔ)進(jìn)數(shù)據(jù)庫
insertData(dbOpenHelper.getReadableDatabase(),et_name,et_password);
Toast.makeText(RegisterActivity.this,"注冊(cè)成功!",Toast.LENGTH_SHORT).show();
}
//關(guān)閉注冊(cè)頁面 跳轉(zhuǎn)到登錄頁面
RegisterActivity.this.finish();
}
});
}
/*正則化驗(yàn)證手機(jī)號(hào)碼方法*/
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;
}
}
//創(chuàng)建數(shù)據(jù)庫的insert方法 插入數(shù)據(jù)方法
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);
}
//重寫onDestroy()方法
@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 {
//定義登錄Button 編輯框
private Button btn_login;
private EditText et_password,et_userName;
/*定義數(shù)據(jù)庫所需成員變量 */
private DBOpenHelper dbOpenHelper;
//數(shù)據(jù)庫里存儲(chǔ)的password
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();
//注冊(cè)完之后更新
/*定義數(shù)據(jù)庫對(duì)象 */
dbOpenHelper=new DBOpenHelper(LoginActivity.this,"user.db",null,1);
/*點(diǎn)擊跳轉(zhuǎn)至注冊(cè)頁面 【還沒有賬號(hào)?點(diǎn)擊注冊(cè)】按鈕*/
Button btn_register1=(Button)findViewById(R.id.btn_register1);
btn_register1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//點(diǎn)擊按鈕跳轉(zhuǎn)到注冊(cè)頁面
Intent intent = new Intent(getApplicationContext(), RegisterActivity.class);
//注冊(cè)返回代碼為0時(shí),跳轉(zhuǎn)
startActivity(intent);
}
});
//登錄按鈕單擊事件
btn_login=(Button)findViewById(R.id.btn_login);
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//獲取輸入的密碼框內(nèi)容
String etpassword=et_password.getText().toString();
/*獲取數(shù)據(jù)庫里的數(shù)據(jù)*/
//登錄按鈕獲取要查詢的賬號(hào)
String key=et_userName.getText().toString();
Cursor cursor=dbOpenHelper.getReadableDatabase().query("user",null,"username = ?",new String[]{key},null,null,null);
//創(chuàng)建ArrayList對(duì)象,用于保存用戶數(shù)據(jù)結(jié)果
ArrayList<Map<String,String>> resultList =new ArrayList<Map<String,String>>();//不用測(cè)試的話,直接遍歷取值getstring(2)就行,創(chuàng)建數(shù)組可以省去。
while(cursor.moveToNext()){
//將結(jié)果集中的數(shù)據(jù)存入HashMap
Map<String,String> map=new HashMap<>();
//取出查詢結(jié)果第二列和第三列的值
//用戶名
map.put("username",cursor.getString(1));
//密碼
map.put("password",cursor.getString(2));
resultList.add(map);
//獲取數(shù)據(jù)庫中符合用戶名的對(duì)應(yīng)的密碼
dbpassword=map.get("password");
}
//正則化判斷輸入的賬號(hào)是否符合手機(jī)號(hào)格式
if(!isTelPhoneNumber(key)){
Toast.makeText(LoginActivity.this,"請(qǐng)輸入正確的手機(jī)號(hào)!",Toast.LENGTH_SHORT).show();
}else if (resultList == null || resultList.size() == 0) { //如果數(shù)據(jù)庫中沒有查詢的用戶數(shù)據(jù)
//顯示提示信息,沒有相關(guān)記錄
Toast.makeText(LoginActivity.this,
"該用戶名未注冊(cè),請(qǐng)先注冊(cè)", Toast.LENGTH_LONG).show();
} else {
/*R.layout.userdata_main、R.id.result_name, R.id.result_grade
這里的是我登錄頁面里面有個(gè)測(cè)試框用來顯示數(shù)據(jù)庫中符合輸入的用戶名的結(jié)果的
等會(huì)寫在下面把 只有一個(gè)userdata_main.xml */
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});
//將適配器和測(cè)試的listview關(guān)聯(lián),我這里的listview叫test_text
test_text.setAdapter(simpleAdapter);
//查到了用戶 對(duì)比輸入的密碼與數(shù)據(jù)庫的密碼是否一致 如果相等跳轉(zhuǎn)到主頁面去
if(etpassword.equals(dbpassword)){
Toast.makeText(LoginActivity.this,"登陸成功!",Toast.LENGTH_SHORT).show();
//跳轉(zhuǎn)到Mainactivity
Intent intent=new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);
//關(guān)閉登錄頁面
LoginActivity.this.finish();
}else{
Toast.makeText(LoginActivity.this,"密碼錯(cuò)誤!",Toast.LENGTH_SHORT).show();
}
};
}
});
}
/*正則化驗(yàn)證手機(jī)號(hào)碼*/
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);
}
// //重寫onDestroy()方法
@Override
protected void onDestroy() {
super.onDestroy();
if (dbOpenHelper != null) {
dbOpenHelper.close();
}
}
}
6.這里是我登錄頁面上最下面有個(gè)測(cè)試窗口布局調(diào)用的這個(gè)XML,
(這個(gè)窗口是用來顯示,輸入框輸入的用戶名與之匹配的數(shù)據(jù)庫的結(jié)果的)userdata_main.XML(res文件夾–右鍵–new–layoutxml)文章來源:http://www.zghlxwxcb.cn/news/detail-779643.html
<?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:要設(shè)置下點(diǎn)進(jìn)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>
/>
/*注意!?。∵@里同時(shí)要把mainactivity的exported屬性改為false 并刪除<intent-filter>屬性*/
<activity
android:name=".MainActivity"
android:exported="false"/>
登錄注冊(cè)的所有內(nèi)容都在這里啦!碼字不易,來過的點(diǎn)個(gè)贊吧!
還有還有,Android新手一枚,實(shí)現(xiàn)的功能比較簡(jiǎn)單,也有很多問題還沒考慮到,
有什么意見歡迎多多指教,有什么問題也歡迎留在評(píng)論區(qū)溝通交流哦!
下一篇:點(diǎn)擊查看商城主頁面底部導(dǎo)航欄的創(chuàng)建:使用Bottom Navigation Activity+Fragment+ViewPager實(shí)現(xiàn)底部導(dǎo)航欄
相關(guān)文章:
Android開發(fā)----實(shí)現(xiàn)應(yīng)用啟動(dòng)頁
Android開發(fā)----ViewPager實(shí)現(xiàn)廣告輪播圖
Android商城開發(fā)----點(diǎn)擊左側(cè)分類實(shí)現(xiàn)右側(cè)更新為對(duì)應(yīng)的商品列表
RecyclerView設(shè)置點(diǎn)擊選中更改背景顏色 這里是更改選中效果
Android商城開發(fā)----點(diǎn)擊加入購物車,購物車商品的增刪減文章來源地址http://www.zghlxwxcb.cn/news/detail-779643.html
到了這里,關(guān)于Android開發(fā)----實(shí)現(xiàn)登錄注冊(cè)頁面(創(chuàng)建本地?cái)?shù)據(jù)庫,對(duì)注冊(cè)的賬戶密碼進(jìn)行存儲(chǔ))的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!