Android使用SqLite實(shí)現(xiàn)登錄注冊功能流程詳解
一、了解什么是Android Studio
Android Studio 是開發(fā) Android 應(yīng)用程序的官方 IDE,基于 Intellij IDEA。你可以從官網(wǎng)Android Studio下載最新版本的 Android Studio。在項目開始之前,請確認(rèn)已經(jīng)安裝好Android Studio和完成相關(guān)的環(huán)境配置。
二、了解什么是sqlite
SQLite 是一個軟件庫,實(shí)現(xiàn)了自給自足的、無服務(wù)器的、零配置的、事務(wù)性的 SQL 數(shù)據(jù)庫引擎。SQLite 是在世界上最廣泛部署的 SQL 數(shù)據(jù)庫引擎。他有諸多的優(yōu)點(diǎn)。
- 輕量級:SQLite是一個嵌入式數(shù)據(jù)庫,它以一個獨(dú)立的、自給自足的文件形式存在,不需要額外的服務(wù)器進(jìn)程或配置。它的庫文件大小較小,占用的系統(tǒng)資源較少,適合在資源有限的環(huán)境中使用。
- 易于使用:SQLite提供了簡單的API和易于理解的SQL查詢語言,使得開發(fā)人員可以輕松地進(jìn)行數(shù)據(jù)庫操作。它使用標(biāo)準(zhǔn)的SQL語法,并提供了廣泛的查詢、插入、更新和刪除功能。
- 高性能:由于SQLite是一個本地文件數(shù)據(jù)庫,它可以直接訪問數(shù)據(jù),而無需網(wǎng)絡(luò)連接。這使得數(shù)據(jù)庫操作的速度非???,并且可以在本地執(zhí)行復(fù)雜的查詢和事務(wù)。
- 跨平臺支持:SQLite數(shù)據(jù)庫可以在多個操作系統(tǒng)和平臺上運(yùn)行,包括Windows、Linux、Mac和移動設(shè)備平臺(如Android和iOS)。這使得開發(fā)人員可以使用相同的數(shù)據(jù)庫技術(shù)來開發(fā)跨平臺的應(yīng)用程序。
想要學(xué)習(xí)sqlite數(shù)據(jù)庫的可以點(diǎn)擊了解sqlite教程,本文不做過多介紹。
三、創(chuàng)建項目文件
打開Android Studio,創(chuàng)建一個新的空白項目。
接下來設(shè)置應(yīng)用程序的基本信息,如應(yīng)用程序名和包名,指定程序的安卓版本等。
設(shè)置完成后點(diǎn)擊完成,完成一個空項目的創(chuàng)建。
四、創(chuàng)建活動文件和布局文件
在項目工程結(jié)構(gòu)下的Java目錄下的包含你項目名的軟件包下新建活動文件。一般項目會自帶有一個默認(rèn)活動。(叫這個名字??MainActivity)
右擊軟件包,創(chuàng)建一個名為RegisterActivity的活動,勾選第一個選項,創(chuàng)建完成活動后Android Studio會自動幫你創(chuàng)建對應(yīng)的一個布局文件。
因為剛創(chuàng)建出來的布局文件是空白的,需要我們自己添加按鈕或者文本,以下是一個布局頁面的示例,可以根據(jù)自己需求更改。布局元素自行了解,不再過多敘述。
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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注冊" android:textSize="40dp" android:layout_gravity="center" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="50dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用戶名:" android:textSize="20dp" android:layout_marginRight="20dp" android:layout_marginTop="3dp" /> <EditText android:id="@+id/rusername" android:layout_width="200dp" android:layout_height="wrap_content" android:hint="請輸入你的用戶名"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="50dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密碼:" android:textSize="20dp" android:layout_marginRight="20dp" android:layout_marginTop="3dp" /> <EditText android:id="@+id/rpassword" android:layout_width="200dp" android:layout_height="wrap_content" android:hint="請入你的密碼"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="50dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="電話:" android:textSize="20dp" android:layout_marginRight="20dp" android:layout_marginTop="3dp" /> <EditText android:id="@+id/rphone" android:layout_width="200dp" android:layout_height="wrap_content" android:hint="請輸入你的電話"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="50dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="郵箱:" android:textSize="20dp" android:layout_marginRight="20dp" android:layout_marginTop="3dp" /> <EditText android:id="@+id/remil" android:layout_width="200dp" android:layout_height="wrap_content" android:hint="請輸入你的郵箱"/> </LinearLayout> <Button android:onClick="register" android:id="@+id/mregister" android:layout_gravity="center" android:layout_marginTop="40dp" android:layout_width="200dp" android:layout_height="wrap_content" android:textSize="20dp" android:text="注冊" /> <Button android:id="@+id/fh" android:onClick="gobak" android:layout_gravity="center" android:layout_marginTop="40dp" android:layout_width="200dp" android:layout_height="wrap_content" android:textSize="20dp" android:text="返回登錄" /> </LinearLayout>
注冊頁面有了當(dāng)然少不了登錄頁面,用上述的方法創(chuàng)建一個登錄頁面,同樣給出一個示例頁面。
login_activity.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=".LoginActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="登錄" android:textSize="50dp"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="50dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用戶名:" android:textSize="20dp" android:layout_marginRight="20dp" android:layout_marginTop="3dp" /> <EditText android:id="@+id/lusername" android:layout_width="200dp" android:layout_height="wrap_content" android:hint="請輸入你的用戶名"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="50dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密碼:" android:textSize="20dp" android:layout_marginRight="20dp" android:layout_marginTop="3dp" /> <EditText android:id="@+id/lpassword" android:layout_width="200dp" android:layout_height="wrap_content" android:hint="請輸入你的密碼"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="300dp" > <Button android:id="@+id/login" android:layout_width="150dp" android:layout_height="55dp" android:layout_marginLeft="30dp" android:textSize="20dp" android:text="登錄"></Button> <Button android:id="@+id/register" android:onClick="GoRegister" android:layout_width="150dp" android:layout_height="55dp" android:textSize="20dp" android:layout_marginLeft="60dp" android:text="注冊" > </Button> </LinearLayout> </LinearLayout>
五、創(chuàng)建數(shù)據(jù)庫連接數(shù)據(jù)庫
在活動目錄包下創(chuàng)建一個名為MySQLiteOpenHelper的java類,注意是創(chuàng)建Java類不是活動類。并且繼承SQLiteOpenHelper,后續(xù)我們在這個類中實(shí)現(xiàn)數(shù)據(jù)庫的相關(guān)操作。
MySQLiteOpenHelper.java
package com.example.androidword; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import androidx.annotation.Nullable; public class MySQLiteOpenHelper extends SQLiteOpenHelper { //數(shù)據(jù)庫名字 private static final String DB_NAME = "User.db"; //創(chuàng)建用戶表 private static final String CREATE_USER = "create table user(id integer primary key autoincrement," + "username varchar(30)," + "password varchar(30)," + "phone varchar(30)," + "emil varchar(30))"; //運(yùn)行程序時,Android Studio幫你創(chuàng)建數(shù)據(jù)庫,只會執(zhí)行一次 public MySQLiteOpenHelper(@Nullable Context context) { super(context,DB_NAME,null,1); } //創(chuàng)建數(shù)據(jù)表 @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { sqLiteDatabase.execSQL(CREATE_USER); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { } }
六、創(chuàng)建實(shí)體類實(shí)現(xiàn)注冊功能
在活動目錄包下新建User類,用它當(dāng)作我們此次項目的實(shí)體類,也就是數(shù)據(jù)庫表中的字段。
User.java
package com.example.androidword; //創(chuàng)建user類,并添加屬性和構(gòu)造方法、get、set方法 public class User { public String username; public String password; public String phone; public String emil; public User(){} public User(String username, String password, String phone, String emil) { this.username = username; this.password = password; this.phone = phone; this.emil = emil; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getEmil() { return emil; } public void setEmil(String emil) { this.emil = emil; } }
接著我們?nèi)ySQLiteOpenHelper類中添加注冊的方法,即把我們輸入的數(shù)據(jù)保存在數(shù)據(jù)庫中(插入)。
public long register(User u){ SQLiteDatabase db = getWritableDatabase(); ContentValues cv = new ContentValues(); cv.put("username ",u.getUsername()); cv.put("password",u.getPassword()); cv.put("phone",u.getPhone()); cv.put("emil",u.getEmil()); long users = db.insert("user",null,cv); return users; }
在RegisterActivity活動類中我們還要編寫相關(guān)的后臺代碼,如獲取輸入框,注冊按鈕等。
package com.example.androidword; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class RegisterActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { mySQLiteOpenHelper = new MySQLiteOpenHelper(this); super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); //初始化獲取的布局元素 find(); } //獲取注冊按鈕,預(yù)先定義,下方輸入框同理 private Button register; private EditText username,password,phone,emil; //使用MySQLiteOpenHelper類中的方法 private MySQLiteOpenHelper mySQLiteOpenHelper; //控制注冊按鈕點(diǎn)擊完后不給點(diǎn)擊了 boolean flag = false; //跳轉(zhuǎn)回登錄頁面 public void gobak(View view){ Intent gl = new Intent(RegisterActivity.this,LoginActivity.class); startActivity(gl); } //初始化獲取的布局元素 public void find(){ username = findViewById(R.id.rusername); password = findViewById(R.id.rpassword); phone = findViewById(R.id.rphone); emil = findViewById(R.id.remil); register = findViewById(R.id.mregister); register.setOnClickListener(this); } //注冊邏輯實(shí)現(xiàn) public void register(){ String ru = username.getText().toString(); String rps = password.getText().toString(); String rph = phone.getText().toString(); String rem = emil.getText().toString(); User user = new User(ru,rps,rph,rem); if(ru.equals("")){ Toast.makeText(this, "賬號不能為空!", Toast.LENGTH_SHORT).show(); } else if (rps.equals("")) { Toast.makeText(this, "密碼不能為空!", Toast.LENGTH_SHORT).show(); } else if (rph.equals("")) { Toast.makeText(this, "電話不能為空!", Toast.LENGTH_SHORT).show(); }else if(rem.equals("")){ Toast.makeText(this, "郵箱不能為空!", Toast.LENGTH_SHORT).show(); }else{ long r1 = mySQLiteOpenHelper.register(user); register.setEnabled(false); register.setTextColor(0xFFD0EFC6); if(r1!=-1){ Toast.makeText(this, "注冊成功", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(this, "注冊失??!", Toast.LENGTH_SHORT).show(); } } } //監(jiān)聽按鈕點(diǎn)擊事件 @Override public void onClick(View view) { switch (view.getId()){ case R.id.mregister: register(); break; } } @Override public void onPointerCaptureChanged(boolean hasCapture) { super.onPointerCaptureChanged(hasCapture); } }
運(yùn)行程序,觀察數(shù)據(jù)庫是否成功創(chuàng)建,注冊信息能否添加進(jìn)數(shù)據(jù)庫。
到此注冊功能已經(jīng)基本實(shí)現(xiàn),更多的邏輯判斷可以自行添加。接下來實(shí)現(xiàn)登錄功能。
七、實(shí)現(xiàn)登錄功能
MySQLiteOpenHelper類中添加登錄的方法和根據(jù)用戶名找到當(dāng)前用戶的方法。
//登錄方法實(shí)現(xiàn) public boolean login(String username,String password){ SQLiteDatabase db = getWritableDatabase(); boolean result = false; Cursor users = db.query("user", null, "username like?", new String[]{username}, null, null, null); if(users!=null){ while (users.moveToNext()){ @SuppressLint("Range") String username1 = users.getString(users.getColumnIndex("username")); Log.i("users", "login: "+username1); String password1 = users.getString(2); Log.i("users", "login: "+password1); result = password1.equals(password); return result; } } return false; } //根據(jù)用戶名查找當(dāng)前登錄用戶信息 public User select(String username){ SQLiteDatabase db = getWritableDatabase(); User SelectUser = new User(); Cursor user = db.query("user", new String[]{"username", "phone", "emil"}, "username=?", new String[]{username}, null, null, null); while(user.moveToNext()){ @SuppressLint("Range") String uname =user.getString(user.getColumnIndex("username")); @SuppressLint("Range") String phone = user.getString(user.getColumnIndex("phone")); @SuppressLint("Range") String emil = user.getString(user.getColumnIndex("emil")); SelectUser.setUsername(uname); SelectUser.setPhone(phone); SelectUser.setEmil(emil); } user.close(); return SelectUser; }
接下來同上一步,到LoginActivity活動類中找到登錄按鈕,輸入框等布局元素,并實(shí)現(xiàn)登錄功能。
package com.example.androidword; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class LoginActivity extends AppCompatActivity implements View.OnClickListener { private Button register,login; private EditText username,password; private MySQLiteOpenHelper mySQLiteOpenHelper; @Override protected void onCreate(Bundle savedInstanceState) { mySQLiteOpenHelper = new MySQLiteOpenHelper(this); super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); find(); } public void find(){ login = findViewById(R.id.login); register = findViewById(R.id.register); username = findViewById(R.id.lusername); password = findViewById(R.id.lpassword); login.setOnClickListener(this); register.setOnClickListener(this); } @Override public void onClick(View view) { int id = view.getId(); switch (id){ case R.id.login: String n = username.getText().toString(); String p = password.getText().toString(); if(n.equals("")){ Toast.makeText(this, "請輸入賬號", Toast.LENGTH_SHORT).show(); }else if(p.equals("")){ Toast.makeText(this, "請輸入密碼", Toast.LENGTH_SHORT).show(); }else{ boolean login = mySQLiteOpenHelper.login(n, p); if(login){ Toast.makeText(this, "登錄成功!", Toast.LENGTH_SHORT).show(); User select = mySQLiteOpenHelper.select(n); Intent home = new Intent(LoginActivity.this,MainActivity.class); Bundle bundle = new Bundle(); bundle.putString("username",select.username); bundle.putString("emil",select.emil); bundle.putString("phone",select.phone); home.putExtras(bundle); username.setText(""); password.setText(""); startActivity(home); }else { Toast.makeText(this, "賬號或密碼錯誤,請重新輸入", Toast.LENGTH_SHORT).show(); password.setText(""); } } break; case R.id.register: Intent re = new Intent(LoginActivity.this, RegisterActivity.class); startActivity(re); break; } } @Override public void onPointerCaptureChanged(boolean hasCapture) { super.onPointerCaptureChanged(hasCapture); } }
重新啟動應(yīng)用,觀察登錄功能是否已經(jīng)實(shí)現(xiàn),上述例子中我添加了一個校驗通過跳轉(zhuǎn)另一個頁面的方法來檢驗是否登錄成功。
到此,我們已經(jīng)實(shí)現(xiàn)了一個簡單的注冊和登錄功能。
八、總結(jié)
學(xué)習(xí)Android Studio是學(xué)習(xí)Android應(yīng)用開發(fā)的重要一步。下面是一個Android Studio課程學(xué)習(xí)的總結(jié):
1. 熟悉界面和基本功能:開始學(xué)習(xí)之前,熟悉Android Studio的用戶界面和各個功能區(qū)域。了解項目結(jié)構(gòu)、編輯器、調(diào)試工具等基本功能,掌握常用的快捷鍵和操作技巧。
2. 學(xué)習(xí)Java或Kotlin語言:Android Studio主要使用Java或Kotlin語言進(jìn)行開發(fā)。學(xué)習(xí)Java或Kotlin的基本語法、面向?qū)ο缶幊谈拍詈统S玫腁PI。
3. 掌握Android框架和組件:了解Android的基本組件和框架,如Activity、Fragment、Intent、布局、資源管理等。學(xué)習(xí)如何創(chuàng)建和管理這些組件,以及它們之間的交互。
4. 學(xué)習(xí)布局設(shè)計和UI開發(fā):掌握Android布局設(shè)計,使用XML和代碼創(chuàng)建用戶界面。了解常用的布局類型(如線性布局、相對布局、幀布局等),并學(xué)習(xí)如何使用UI控件(如TextView、Button、ImageView等)和樣式化組件。
5. 數(shù)據(jù)存儲和處理:學(xué)習(xí)如何在Android應(yīng)用中存儲和處理數(shù)據(jù)。了解SQLite數(shù)據(jù)庫數(shù)據(jù)存儲技術(shù),以及與網(wǎng)絡(luò)通信、數(shù)據(jù)解析和持久化相關(guān)的技術(shù)。
以上就是Android使用SqLite實(shí)現(xiàn)登錄注冊功能流程詳解的詳細(xì)內(nèi)容,更多關(guān)于Android SqLite登錄注冊的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
2021最新Android筆試題總結(jié)美團(tuán)Android崗職能要求
這篇文章主要介紹了2021最新Android筆試題總結(jié)以及美團(tuán)Android崗職能要求,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08Android實(shí)現(xiàn)動態(tài)向Gallery中添加圖片及倒影與3D效果示例
這篇文章主要介紹了Android實(shí)現(xiàn)動態(tài)向Gallery中添加圖片及倒影與3D效果的方法,涉及Android針對圖片的加載、顯示、翻轉(zhuǎn)、倒影等相關(guān)特效功能實(shí)現(xiàn)技巧2016-08-08Android自定義Camera實(shí)現(xiàn)拍照功能
這篇文章主要為大家詳細(xì)介紹了Android自定義Camera實(shí)現(xiàn)拍照功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05Android PopupWindow全屏詳細(xì)介紹及實(shí)例代碼
這篇文章主要介紹了 Android PopupWindow全屏詳細(xì)介紹及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-12-12Android 拍照并對照片進(jìn)行裁剪和壓縮實(shí)例詳解
這篇文章主要介紹了Android 拍照并對照片進(jìn)行裁剪和壓縮實(shí)例詳解的相關(guān)資料,這里提供實(shí)例代碼,需要的朋友可以參考下2017-07-07