本學(xué)期學(xué)習(xí)了Android Studio這門(mén)課程,本次使用Android Studio自帶的sqlite數(shù)據(jù)庫(kù)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的登錄注冊(cè)功能。
目錄
一、了解什么是Android Studio?
二、了解什么是sqlite?
三、創(chuàng)建項(xiàng)目文件
?四、創(chuàng)建活動(dòng)文件和布局文件。
五、創(chuàng)建數(shù)據(jù)庫(kù),連接數(shù)據(jù)庫(kù)
?六、創(chuàng)建實(shí)體類,實(shí)現(xiàn)注冊(cè)功能
七、實(shí)現(xiàn)登錄功能
八、總結(jié)
一、了解什么是Android Studio?
Android Studio 是開(kāi)發(fā) Android 應(yīng)用程序的官方 IDE,基于 Intellij IDEA。你可以從官網(wǎng)Android Studio下載下載最新版本的 Android Studio。在項(xiàng)目開(kāi)始之前,請(qǐng)確認(rèn)已經(jīng)安裝好Android Studio和完成相關(guān)的環(huán)境配置。
二、了解什么是sqlite?
SQLite 是一個(gè)軟件庫(kù),實(shí)現(xiàn)了自給自足的、無(wú)服務(wù)器的、零配置的、事務(wù)性的 SQL 數(shù)據(jù)庫(kù)引擎。SQLite 是在世界上最廣泛部署的 SQL 數(shù)據(jù)庫(kù)引擎。他有諸多的優(yōu)點(diǎn)。
-
輕量級(jí):SQLite是一個(gè)嵌入式數(shù)據(jù)庫(kù),它以一個(gè)獨(dú)立的、自給自足的文件形式存在,不需要額外的服務(wù)器進(jìn)程或配置。它的庫(kù)文件大小較小,占用的系統(tǒng)資源較少,適合在資源有限的環(huán)境中使用。
-
易于使用:SQLite提供了簡(jiǎn)單的API和易于理解的SQL查詢語(yǔ)言,使得開(kāi)發(fā)人員可以輕松地進(jìn)行數(shù)據(jù)庫(kù)操作。它使用標(biāo)準(zhǔn)的SQL語(yǔ)法,并提供了廣泛的查詢、插入、更新和刪除功能。
-
高性能:由于SQLite是一個(gè)本地文件數(shù)據(jù)庫(kù),它可以直接訪問(wèn)數(shù)據(jù),而無(wú)需網(wǎng)絡(luò)連接。這使得數(shù)據(jù)庫(kù)操作的速度非???,并且可以在本地執(zhí)行復(fù)雜的查詢和事務(wù)。
-
跨平臺(tái)支持:SQLite數(shù)據(jù)庫(kù)可以在多個(gè)操作系統(tǒng)和平臺(tái)上運(yùn)行,包括Windows、Linux、Mac和移動(dòng)設(shè)備平臺(tái)(如Android和iOS)。這使得開(kāi)發(fā)人員可以使用相同的數(shù)據(jù)庫(kù)技術(shù)來(lái)開(kāi)發(fā)跨平臺(tái)的應(yīng)用程序。
想要學(xué)習(xí)sqlite數(shù)據(jù)庫(kù)的可以點(diǎn)擊了解sqlite教程,本文不做過(guò)多介紹。
三、創(chuàng)建項(xiàng)目文件
打開(kāi)Android Studio,創(chuàng)建一個(gè)新的空白項(xiàng)目。
接下來(lái)設(shè)置應(yīng)用程序的基本信息,如應(yīng)用程序名和包名,指定程序的安卓版本等。
設(shè)置完成后點(diǎn)擊完成,完成一個(gè)空項(xiàng)目的創(chuàng)建。?
?四、創(chuàng)建活動(dòng)文件和布局文件。
在項(xiàng)目工程結(jié)構(gòu)下的Java目錄下的包含你項(xiàng)目名的軟件包下新建活動(dòng)文件。一般項(xiàng)目會(huì)自帶有一個(gè)默認(rèn)活動(dòng)。(叫這個(gè)名字??MainActivity)
?右擊軟件包,創(chuàng)建一個(gè)名為RegisterActivity的活動(dòng),勾選第一個(gè)選項(xiàng),創(chuàng)建完成活動(dòng)后Android Studio會(huì)自動(dòng)幫你創(chuàng)建對(duì)應(yīng)的一個(gè)布局文件。
?
?因?yàn)閯倓?chuàng)建出來(lái)的布局文件是空白的,需要我們自己添加按鈕或者文本,以下是一個(gè)布局頁(yè)面的示例,可以根據(jù)自己需求更改。布局元素自行了解,不再過(guò)多敘述。?
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="注冊(cè)"
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="請(qǐng)輸入你的用戶名"/>
</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="請(qǐng)入你的密碼"/>
</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="請(qǐng)輸入你的電話"/>
</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="請(qǐng)輸入你的郵箱"/>
</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="注冊(cè)"
/>
<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>
?注冊(cè)頁(yè)面有了當(dāng)然少不了登錄頁(yè)面,用上述的方法創(chuàng)建一個(gè)登錄頁(yè)面,同樣給出一個(gè)示例頁(yè)面。
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="請(qǐng)輸入你的用戶名"/>
</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="請(qǐng)輸入你的密碼"/>
</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="注冊(cè)"
>
</Button>
</LinearLayout>
</LinearLayout>
五、創(chuàng)建數(shù)據(jù)庫(kù),連接數(shù)據(jù)庫(kù)
在活動(dòng)目錄包下創(chuàng)建一個(gè)名為MySQLiteOpenHelper的java類,注意是創(chuàng)建Java類不是活動(dòng)類。并且繼承SQLiteOpenHelper,后續(xù)我們?cè)谶@個(gè)類中實(shí)現(xiàn)數(shù)據(jù)庫(kù)的相關(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ù)庫(kù)名字
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)行程序時(shí),Android Studio幫你創(chuàng)建數(shù)據(jù)庫(kù),只會(huì)執(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)注冊(cè)功能
在活動(dòng)目錄包下新建User類,用它當(dāng)作我們此次項(xiàng)目的實(shí)體類,也就是數(shù)據(jù)庫(kù)表中的字段。
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類中添加注冊(cè)的方法,即把我們輸入的數(shù)據(jù)保存在數(shù)據(jù)庫(kù)中(插入)。
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活動(dòng)類中我們還要編寫(xiě)相關(guān)的后臺(tái)代碼,如獲取輸入框,注冊(cè)按鈕等。
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();
}
//獲取注冊(cè)按鈕,預(yù)先定義,下方輸入框同理
private Button register;
private EditText username,password,phone,emil;
//使用MySQLiteOpenHelper類中的方法
private MySQLiteOpenHelper mySQLiteOpenHelper;
//控制注冊(cè)按鈕點(diǎn)擊完后不給點(diǎn)擊了
boolean flag = false;
//跳轉(zhuǎn)回登錄頁(yè)面
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);
}
//注冊(cè)邏輯實(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, "賬號(hào)不能為空!", 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, "注冊(cè)成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "注冊(cè)失??!", Toast.LENGTH_SHORT).show();
}
}
}
//監(jiān)聽(tīng)按鈕點(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ù)庫(kù)是否成功創(chuàng)建,注冊(cè)信息能否添加進(jìn)數(shù)據(jù)庫(kù)。
?
?到此注冊(cè)功能已經(jīng)基本實(shí)現(xiàn),更多的邏輯判斷可以自行添加。接下來(lái)實(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;
}
接下來(lái)同上一步,到LoginActivity活動(dòng)類中找到登錄按鈕,輸入框等布局元素,并實(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, "請(qǐng)輸入賬號(hào)", Toast.LENGTH_SHORT).show();
}else if(p.equals("")){
Toast.makeText(this, "請(qǐng)輸入密碼", 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, "賬號(hào)或密碼錯(cuò)誤,請(qǐng)重新輸入", 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);
}
}
重新啟動(dòng)應(yīng)用,觀察登錄功能是否已經(jīng)實(shí)現(xiàn),上述例子中我添加了一個(gè)校驗(yàn)通過(guò)跳轉(zhuǎn)另一個(gè)頁(yè)面的方法來(lái)檢驗(yàn)是否登錄成功。
?
?到此,我們已經(jīng)實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的注冊(cè)和登錄功能。
八、總結(jié)
學(xué)習(xí)Android Studio是學(xué)習(xí)Android應(yīng)用開(kāi)發(fā)的重要一步。下面是一個(gè)Android Studio課程學(xué)習(xí)的總結(jié):
1. 熟悉界面和基本功能:開(kāi)始學(xué)習(xí)之前,熟悉Android Studio的用戶界面和各個(gè)功能區(qū)域。了解項(xiàng)目結(jié)構(gòu)、編輯器、調(diào)試工具等基本功能,掌握常用的快捷鍵和操作技巧。
2. 學(xué)習(xí)Java或Kotlin語(yǔ)言:Android Studio主要使用Java或Kotlin語(yǔ)言進(jìn)行開(kāi)發(fā)。學(xué)習(xí)Java或Kotlin的基本語(yǔ)法、面向?qū)ο缶幊谈拍詈统S玫腁PI。
3. 掌握Android框架和組件:了解Android的基本組件和框架,如Activity、Fragment、Intent、布局、資源管理等。學(xué)習(xí)如何創(chuàng)建和管理這些組件,以及它們之間的交互。
4. 學(xué)習(xí)布局設(shè)計(jì)和UI開(kāi)發(fā):掌握Android布局設(shè)計(jì),使用XML和代碼創(chuàng)建用戶界面。了解常用的布局類型(如線性布局、相對(duì)布局、幀布局等),并學(xué)習(xí)如何使用UI控件(如TextView、Button、ImageView等)和樣式化組件。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-738920.html
5. 數(shù)據(jù)存儲(chǔ)和處理:學(xué)習(xí)如何在Android應(yīng)用中存儲(chǔ)和處理數(shù)據(jù)。了解SQLite數(shù)據(jù)庫(kù)數(shù)據(jù)存儲(chǔ)技術(shù),以及與網(wǎng)絡(luò)通信、數(shù)據(jù)解析和持久化相關(guān)的技術(shù)。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-738920.html
到了這里,關(guān)于Android Studio|使用SqLite實(shí)現(xiàn)一個(gè)簡(jiǎn)單的登錄注冊(cè)功能的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!