? ? ? ? 首先先了解AndroidStudio是什么:Android Studio是一個(gè)由谷歌開發(fā)的Android應(yīng)用程序開發(fā)環(huán)境,用于開發(fā)Android應(yīng)用程序。它基于JetBrains IntelliJIDEA軟件,并包含了許多定制化功能,包括易于使用的分析工具、內(nèi)存分析工具和代碼編輯器等,支持Java、Kotlin等多種編程語言。Android Studio還提供了模擬器和虛擬設(shè)備來測試應(yīng)用程序,可以幫助開發(fā)者更加高效地進(jìn)行Android應(yīng)用程序開發(fā)。
? ? ? ?作為一款A(yù)ndroid應(yīng)用程序開發(fā)環(huán)境,AndroidStudio具有以下優(yōu)點(diǎn):
- 界面友好:Android Studio提供了簡單直觀的界面,并且其操作流暢,易于使用和學(xué)習(xí)。
- 功能強(qiáng)大:Android Studio集成了許多強(qiáng)大的功能,如代碼自動(dòng)完成、代碼檢查和重構(gòu)等,方便開發(fā)者快速創(chuàng)建高質(zhì)量的Android應(yīng)用程序。
- 支持多種編程語言:Android Studio支持多種編程語言,包括Java、Kotlin等,開發(fā)者可以根據(jù)自己的需求選擇使用不同的編程語言進(jìn)行開發(fā)。
- 良好的集成環(huán)境:Android Studio能夠與其他工具集成,例如Git等版本控制系統(tǒng),方便開發(fā)者在項(xiàng)目開發(fā)過程中協(xié)作。
- 豐富的插件: Android Studio提供了許多插件來擴(kuò)展其功能,比如可以安裝插件來調(diào)試代碼、設(shè)計(jì)圖形用戶界面等。
?接下來我?guī)Т蠹矣肁ndroid Studio創(chuàng)建一個(gè)登陸注冊項(xiàng)目:
1.創(chuàng)建—個(gè)新的Android項(xiàng)目,名稱為"LoginRegisterDemo"。不需要代碼。
2.在布局文件中設(shè)計(jì)好登錄和注冊頁面的UI界面,可以使用EditText來輸入用戶名和密碼。
首先在login_activity.xml中添加登錄頁面的U元素:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editText_userName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請?zhí)顚懹脩裘?
android:layout_marginTop="50dp"
/>
<EditText
android:id="@+id/editText_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="請?zhí)顚懨艽a"
android:layout_below="@id/editText_userName"
/>
<Button
android:id="@+id/button_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登錄"
android:layout_below="@id/editText_password"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
然后在register_activity.xml中添加注冊頁面的UI元素:
?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editText_userName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請?zhí)顚懹脩裘?
android:layout_marginTop="50dp"
/>
<EditText
android:id="@+id/editText_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="請?zhí)顚懨艽a"
android:layout_below="@id/editText_userName"
/>
<Button
android:id="@+id/button_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注冊"
android:layout_below="@id/editText_password"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
3.在Java代碼中創(chuàng)建—個(gè)數(shù)據(jù)庫類,用于保存用戶信息,并添加相應(yīng)的表,如用戶表。
首先,在app目錄下的build.gradle文件中添加Room庫的依賴:
?
dependencies {
def room_version = "2.3.0"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
}
然后定義一個(gè)User實(shí)體類,用于描述用戶信息:
?
@Entity(tableName = "users")
public class User {
@PrimaryKey(autoGenerate = true)
public int id;
public String username;
public String password;
}
接著定義一個(gè)UserDao類,用于與數(shù)據(jù)庫進(jìn)行交互:
?
@Dao
public interface UserDao {
@Query("SELECT * FROM users WHERE username = :username AND password = :password")
User findUser(String username, String password);
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(User user);
}
最后定義一個(gè)AppDatabase類,作為數(shù)據(jù)庫的實(shí)體類:
?
@Database(entities = {User.class}, version = 1, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase sInstance;
public static synchronized AppDatabase getInstance(Context context) {
if (sInstance == null) {
sInstance = Room.databaseBuilder(context.getApplicationContext(),
AppDatabase.class, "loginregisterdemo.db")
.build();
}
return sInstance;
}
public abstract UserDao userDao();
}
4.創(chuàng)建—個(gè)登錄Activity和一個(gè)注冊Activity,并在其中分別添加對應(yīng)的UI元素與邏輯實(shí)現(xiàn)。
首先創(chuàng)建—個(gè)LoginActivity類,在其中添加UI元素以及登錄邏輯:
?
public class LoginActivity extends AppCompatActivity {
private EditText mEditTextUsername;
private EditText mEditTextPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mEditTextUsername = findViewById(R.id.editText_userName);
mEditTextPassword = findViewById(R.id.editText_password);
Button buttonLogin = findViewById(R.id.button_login);
buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = mEditTextUsername.getText().toString();
String password = mEditTextPassword.getText().toString();
User user = AppDatabase.getInstance(LoginActivity.this).userDao().findUser(username, password);
if (user == null) {
Toast.makeText(LoginActivity.this, "用戶名或密碼錯(cuò)誤", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(LoginActivity.this, "登錄成功", Toast.LENGTH_SHORT).show();
}
}
});
}
}
然后創(chuàng)建一個(gè)RegisterActivity類,在其中添加UI元素以及注冊邏輯:
?
public class RegisterActivity extends AppCompatActivity {
private EditText mEditTextUsername;
private EditText mEditTextPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mEditTextUsername = findViewById(R.id.editText_userName);
mEditTextPassword = findViewById(R.id.editText_password);
Button buttonRegister = findViewById(R.id.button_register);
buttonRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = mEditTextUsername.getText().toString();
String password = mEditTextPassword.getText().toString();
User user = new User();
user.username = username;
user.password = password;
AppDatabase.getInstance(RegisterActivity.this).userDao().insert(user);
Toast.makeText(RegisterActivity.this, "注冊成功", Toast.LENGTH_SHORT).show();
finish();
}
});
}
}
5.在登錄Activity的代碼中,通過輸入框獲取用戶輸入的用戶名和密碼,并根據(jù)這些信息從數(shù)據(jù)庫中查詢用戶信息來進(jìn)行登錄驗(yàn)證。
之前在LoginActivity類中已經(jīng)實(shí)現(xiàn)了登錄邏輯。如果用戶輸入的用戶名和密碼與數(shù)據(jù)庫中的用戶信息匹配,則登錄成功;否則提示用戶名或密碼錯(cuò)誤。
6.在注冊Activity的代碼中,通過輸入框獲取用戶的注冊信息,并將其保存到數(shù)據(jù)庫中。
之前在RegisterActivity類中已經(jīng)實(shí)現(xiàn)了注冊邏輯。當(dāng)用戶填寫完注冊信息后點(diǎn)擊注冊按鈕時(shí),該信息會(huì)被保存到數(shù)據(jù)庫中。
7.在登錄Activity和注冊Activity中添加相應(yīng)的按鈕監(jiān)聽事件,并在點(diǎn)擊按鈕時(shí)觸發(fā)對應(yīng)的事件邏輯。
? ? ? ? 之前已經(jīng)在LoginActivity和RegisterActivity類中分別添加了按鈕監(jiān)聽事件,當(dāng)用戶點(diǎn)擊登錄或注冊按鈕時(shí),對應(yīng)的邏輯代碼會(huì)被執(zhí)行。以上就是實(shí)現(xiàn)Android Studio中登錄注冊功能的完整步驟和代碼。需要注意的是,在實(shí)際開發(fā)過程中,還需要考慮一些細(xì)節(jié)問題,比如加密存儲(chǔ)密碼、輸入框輸入格式檢查等。
接下來是存儲(chǔ)用戶密碼跟加密儲(chǔ)存密碼的寫法又想要的可創(chuàng)建。選寫
存儲(chǔ)用戶密碼
?1.在用戶輸入密碼時(shí),通過如下代碼獲取密碼的哈希值:
public static String hashPassword(String password) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(password.getBytes(StandardCharsets.UTF_8));
return Base64.encodeToString(hash, Base64.DEFAULT);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
⒉.將此哈希值與用戶名一起存儲(chǔ)到數(shù)據(jù)庫中。
3.在登錄時(shí),通過如下代碼檢查用戶名和密碼是否匹配:
?
public static boolean checkPassword(String providedPassword, String storedPasswordHash) {
String hash = hashPassword(providedPassword);
return hash != null && hash.equals(storedPasswordHash);
}
? ? ? ?建議每次登錄時(shí)都重新生成一個(gè)哈希值進(jìn)行比對,以增加安全性。使用哈希值存儲(chǔ)密碼可以避免明文存儲(chǔ)密碼,即使數(shù)據(jù)庫泄露也不會(huì)直接導(dǎo)致用戶密碼被泄露。
加密儲(chǔ)存密碼
下面是使用SHA-256算法對密碼進(jìn)行哈希加密和存儲(chǔ)到數(shù)據(jù)庫中的完整代碼:
1.定義一個(gè)加密工具類:
public class EncryptionUtils {
public static String hashPassword(String password) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(password.getBytes(StandardCharsets.UTF_8));
return Base64.encodeToString(hash, Base64.DEFAULT);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
public static boolean checkPassword(String providedPassword, String storedPasswordHash) {
String hash = hashPassword(providedPassword);
return hash != null && hash.equals(storedPasswordHash);
}
}
?2.在注冊時(shí),通過如下代碼獲取用戶輸入的密碼并加密,將加密后的密碼保存到數(shù)據(jù)庫中:
String username = mEditTextUsername.getText().toString();
String password = mEditTextPassword.getText().toString();
String encryptedPassword = EncryptionUtils.hashPassword(password); // 哈希加密密碼
User user = new User();
user.username = username;
user.encryptedPassword = encryptedPassword; // 保存加密后的密碼
AppDatabase.getInstance(RegisterActivity.this).userDao().insert(user);
?3.在登錄時(shí),通過如下代碼從數(shù)據(jù)庫中讀取用戶信息,并檢查密碼是否匹配:
String username = mEditTextUsername.getText().toString();
String password = mEditTextPassword.getText().toString();
User user = AppDatabase.getInstance(LoginActivity.this)
.userDao()
.findByUsername(username);
if (user == null) {
Toast.makeText(LoginActivity.this, "用戶名不存在", Toast.LENGTH_SHORT).show();
} else if (EncryptionUtils.checkPassword(password, user.encryptedPassword)) { // 檢查密碼是否匹配
Toast.makeText(LoginActivity.this, "登錄成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(LoginActivity.this, "密碼錯(cuò)誤", Toast.LENGTH_SHORT).show();
}
? ? ? ?在上述代碼中,通過調(diào)用EncryptionUtils類的hashPassword()方法對密碼進(jìn)行哈希加密,并將加密后的密碼保存到數(shù)據(jù)庫中。而在登錄時(shí),則通過調(diào)用該類的checkPassword()方法檢查用戶輸入的密碼與數(shù)據(jù)庫中保存的加密后的密碼是否匹配。
? ? ?在實(shí)際應(yīng)用開發(fā)中處理用戶登錄注冊時(shí)需要注意的事項(xiàng)和建議:
- 密碼加密:對用戶密碼進(jìn)行哈希加密,并且不要使用明文存儲(chǔ)用戶密碼。
- 防止重復(fù)注冊:在用戶注冊時(shí)要保證用戶名/手機(jī)號(hào)/郵箱等唯—性。
- 用戶信息驗(yàn)證:在用戶登錄時(shí)要對用戶輸入的信息進(jìn)行驗(yàn)證,例如,檢查用戶名是否存在、密碼是否正確等。
- 記住登錄狀態(tài):在用戶登錄成功后,可以通過保存Token等方式來記錄用戶登錄狀態(tài),在用戶再次打開應(yīng)用或頁面時(shí)直接跳轉(zhuǎn)至已登錄的狀態(tài)。
- 安全退出:在用戶退出登錄時(shí),要及時(shí)清除緩存和Cookie等信息,并提示用戶已退出登錄。
- 隱私保護(hù):在處理用戶數(shù)據(jù)時(shí)要遵守相關(guān)法律法規(guī),保護(hù)用戶隱私不被泄露。
以上是登錄注冊處理中常見的一些注意事項(xiàng),此外也要結(jié)合具體業(yè)務(wù)場景進(jìn)行適當(dāng)?shù)恼{(diào)整和優(yōu)化。
額外:
要在 Android Studio 中實(shí)現(xiàn)點(diǎn)擊按鈕跳轉(zhuǎn)到另一個(gè) Activity,你需要按照以下步驟進(jìn)行操作:
-
首先,在你的項(xiàng)目中創(chuàng)建一個(gè)新的 Activity(比如
SecondActivity
),用于跳轉(zhuǎn)后顯示的頁面??梢酝ㄟ^右鍵點(diǎn)擊項(xiàng)目文件夾 -> New -> Activity -> Empty Activity 來創(chuàng)建。 -
在你的源 Activity(即當(dāng)前頁面)的 XML 布局文件中,添加一個(gè)按鈕控件,并為其設(shè)置一個(gè)唯一的
android:id
。例如:
<Button
android:id="@+id/nextButton"
android:text="跳轉(zhuǎn)到下一頁"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
?
? ?4. 打開源 Activity 的 Java 文件(即當(dāng)前頁面對應(yīng)的 Activity 類),在?onCreate
?方法中找到按鈕,并為其設(shè)置點(diǎn)擊事件監(jiān)聽器。在監(jiān)聽器中,使用?Intent
?實(shí)例來指定要跳轉(zhuǎn)到的目標(biāo) Activity。例如:
Button nextButton = findViewById(R.id.nextButton);
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(CurrentActivity.this, SecondActivity.class);
startActivity(intent);
}
});
確保將 CurrentActivity
替換為當(dāng)前頁面的類名,SecondActivity
替換為你創(chuàng)建的目標(biāo) Activity 的類名。
? ?5. 最后,在 AndroidManifest.xml 文件中,將第 3 步中創(chuàng)建的目標(biāo) Activity 添加為一個(gè)?<activity>
?標(biāo)簽。確保?android:name
?屬性的值與你創(chuàng)建的目標(biāo) Activity 類的名稱一致。例如:
?
<activity
android:name=".SecondActivity"
android:label="Second Activity" />
現(xiàn)在,當(dāng)用戶點(diǎn)擊源 Activity 的按鈕時(shí),就會(huì)啟動(dòng)目標(biāo) Activity,并跳轉(zhuǎn)到該頁面。?
學(xué)習(xí)使用Android Studio編寫登錄和注冊功能需要掌握以下幾個(gè)方面:
-
基礎(chǔ)知識(shí):熟悉Android開發(fā)基礎(chǔ)知識(shí),如布局、控件、事件處理等。
-
UI設(shè)計(jì):關(guān)注良好的UI設(shè)計(jì)和用戶體驗(yàn),設(shè)計(jì)簡潔明了的界面并考慮不同設(shè)備屏幕的適配。
-
表單驗(yàn)證和數(shù)據(jù)處理:對用戶輸入進(jìn)行合法性驗(yàn)證,并采取相應(yīng)的處理措施。
-
數(shù)據(jù)存儲(chǔ):學(xué)習(xí)如何使用數(shù)據(jù)庫或本地存儲(chǔ)技術(shù)來保存和檢索用戶信息。
-
安全性考慮:保護(hù)用戶隱私和賬號(hào)安全,采取相應(yīng)的加密和安全處理措施。
-
邏輯處理和錯(cuò)誤處理:處理登錄和注冊過程中的邏輯流程,并提示用戶合適的錯(cuò)誤信息。
-
前后端交互:與后端服務(wù)器進(jìn)行數(shù)據(jù)交互,包括發(fā)送請求、解析響應(yīng)和處理網(wǎng)絡(luò)連接問題。文章來源:http://www.zghlxwxcb.cn/news/detail-744707.html
總的來說,學(xué)習(xí)使用Android Studio編寫登錄和注冊功能需要深入理解Android開發(fā)知識(shí),同時(shí)關(guān)注UI設(shè)計(jì)、數(shù)據(jù)處理、安全性和網(wǎng)絡(luò)交互等方面。通過不斷實(shí)踐和積累經(jīng)驗(yàn),可以逐漸掌握這一技能?。文章來源地址http://www.zghlxwxcb.cn/news/detail-744707.html
到了這里,關(guān)于Android Studio心得-創(chuàng)建登錄注冊項(xiàng)目的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!