Android安卓實(shí)戰(zhàn)項(xiàng)目---登陸注冊(cè)頁面(源碼在文末??????)
一.項(xiàng)目運(yùn)行介紹
效果圖:
二.具體實(shí)現(xiàn)
LoginActivity.java
LoginActivity.java package com.example.login; ? import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.Toast; ? import androidx.appcompat.app.AppCompatActivity; ? import com.example.angel.GuideActivity; import com.example.angel.R; ? import org.jetbrains.annotations.Nullable; ? public class LoginActivity extends AppCompatActivity { ? ? private static final String TAG = "tag"; ? public static final int REQUEST_CODE_REGISTER = 1; ? private Button btnLogin; ? private EditText etAccount,etPassword; ? private CheckBox cbRemember,cbAutoLogin; ? ? private String userName = "fxjzzyo"; ? private String pass = "123"; ? ? @Override ? protected void onCreate(Bundle savedInstanceState) { ? ? ? super.onCreate(savedInstanceState); ? ? ? setContentView(R.layout.activity_login); ? ? ? getSupportActionBar().setTitle("登錄"); ? ? ? ? initView(); ? ? ? initData(); ? ? ? ? btnLogin.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? @Override ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? String account = etAccount.getText().toString(); ? ? ? ? ? ? ? String password = etPassword.getText().toString(); ? ? ? ? ? ? ? ? Log.d(TAG, "onClick: -------------" + account); ? ? ? ? ? ? ? Log.d(TAG, "password: -------------" + password); ? ? ? ? ? ? ? if (TextUtils.isEmpty(userName)) { ? ? ? ? ? ? ? ? ? Toast.makeText(LoginActivity.this, "還沒有注冊(cè)賬號(hào)!", Toast.LENGTH_LONG).show(); ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? if (TextUtils.equals(account, userName)) { ? ? ? ? ? ? ? ? ? if (TextUtils.equals(password, pass)) { ? ? ? ? ? ? ? ? ? ? ? ? Toast.makeText(LoginActivity.this, "恭喜你,登錄成功!", Toast.LENGTH_LONG).show(); ? ? ? ? ? ? ? ? ? ? ? if (cbRemember.isChecked()) { ? ? ? ? ? ? ? ? ? ? ? ? ? SharedPreferences spf = getSharedPreferences("spfRecord", MODE_PRIVATE); ? ? ? ? ? ? ? ? ? ? ? ? ? SharedPreferences.Editor edit = spf.edit(); ? ? ? ? ? ? ? ? ? ? ? ? ? edit.putString("account", account); ? ? ? ? ? ? ? ? ? ? ? ? ? edit.putString("password", password); ? ? ? ? ? ? ? ? ? ? ? ? ? edit.putBoolean("isRemember", true); ? ? ? ? ? ? ? ? ? ? ? ? ? if (cbAutoLogin.isChecked()) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? edit.putBoolean("isLogin", true); ? ? ? ? ? ? ? ? ? ? ? ? ? }else { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? edit.putBoolean("isLogin", false); ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? edit.apply(); ? ? ? ? ? ? ? ? ? ? ? ? }else { ? ? ? ? ? ? ? ? ? ? ? ? ? SharedPreferences spf = getSharedPreferences("spfRecord", MODE_PRIVATE); ? ? ? ? ? ? ? ? ? ? ? ? ? SharedPreferences.Editor edit = spf.edit(); ? ? ? ? ? ? ? ? ? ? ? ? ? edit.putBoolean("isRemember", false); ? ? ? ? ? ? ? ? ? ? ? ? ? edit.apply(); ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? Intent intent = new Intent(LoginActivity.this, GuideActivity.class); ? ? ? ? ? ? ? ? ? ? ? intent.putExtra("account", account); ? ? ? ? ? ? ? ? ? ? ? startActivity(intent); ? ? ? ? ? ? ? ? ? ? ? LoginActivity.this.finish(); ? ? ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? ? Toast.makeText(LoginActivity.this, "密碼錯(cuò)誤!", Toast.LENGTH_LONG).show(); ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? Toast.makeText(LoginActivity.this, "用戶名錯(cuò)誤!", Toast.LENGTH_LONG).show(); ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? }); ? ? ? ? cbAutoLogin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { ? ? ? ? ? @Override ? ? ? ? ? public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { ? ? ? ? ? ? ? if (isChecked) { ? ? ? ? ? ? ? ? ? cbRemember.setChecked(true); ? ? ? ? ? ? ? } ? ? ? ? ? } ? ? ? }); ? ? ? ? cbRemember.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { ? ? ? ? ? @Override ? ? ? ? ? public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { ? ? ? ? ? ? ? if (!isChecked) { ? ? ? ? ? ? ? ? ? cbAutoLogin.setChecked(false); ? ? ? ? ? ? ? } ? ? ? ? ? } ? ? ? }); ? ? } ? ? ? ? private void initView() { ? ? ? btnLogin = findViewById(R.id.btn_login); ? ? ? etAccount = findViewById(R.id.et_account); ? ? ? etPassword = findViewById(R.id.et_password); ? ? ? cbRemember = findViewById(R.id.cb_remember); ? ? ? cbAutoLogin = findViewById(R.id.cb_auto_login); ? ? } ? ? private void initData() { ? ? ? SharedPreferences spf = getSharedPreferences("spfRecord", MODE_PRIVATE); ? ? ? boolean isRemember = spf.getBoolean("isRemember", false); ? ? ? boolean isLogin = spf.getBoolean("isLogin", false); ? ? ? String account = spf.getString("account", ""); ? ? ? String password = spf.getString("password", ""); ? ? ? ? ? if (isLogin) { ? ? ? ? ? Intent intent = new Intent(LoginActivity.this, GuideActivity.class); ? ? ? ? ? intent.putExtra("account", account); ? ? ? ? ? startActivity(intent); ? ? ? ? ? LoginActivity.this.finish(); ? ? ? } ? ? ? ? ? userName = account; ? ? ? pass = password; ? ? ? ? if (isRemember) { ? ? ? ? ? etAccount.setText(account); ? ? ? ? ? etPassword.setText(password); ? ? ? ? ? cbRemember.setChecked(true); ? ? ? } ? ? ? } ? ? public void toRegister(View view) { ? ? ? Intent intent = new Intent(this, RegisterActivity.class); ? ? ? ? startActivityForResult(intent, REQUEST_CODE_REGISTER); ? } ? ? @Override ? protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { ? ? ? super.onActivityResult(requestCode, resultCode, data); ? ? ? if (requestCode == REQUEST_CODE_REGISTER && resultCode == RegisterActivity.RESULT_CODE_REGISTER && data != null) { ? ? ? ? ? Bundle extras = data.getExtras(); ? ? ? ? ? ? String account = extras.getString("account", ""); ? ? ? ? ? String password = extras.getString("password", ""); ? ? ? ? ? ? etAccount.setText(account); ? ? ? ? ? etPassword.setText(password); ? ? ? ? ? ? userName = account; ? ? ? ? ? pass = password; ? ? ? } ? } }
這段代碼是一個(gè)簡(jiǎn)單的Android應(yīng)用中的登錄功能代碼,下面逐行解釋其功能和作用:
-
導(dǎo)入相關(guān)的類和包:
package com.example.userprofile23_1; ? import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.Toast; ? import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity;
這些導(dǎo)入語句引入了用于構(gòu)建Android應(yīng)用界面、處理用戶交互和數(shù)據(jù)存儲(chǔ)的必要類和包。
-
定義一個(gè)名為
LoginActivity
的類,它繼承自AppCompatActivity
,表示這是一個(gè)用于處理界面的活動(dòng)(Activity)。
public class LoginActivity extends AppCompatActivity {
-
聲明類中需要用到的一些變量和常量:
private static final String TAG = "tag"; public static final int REQUEST_CODE_REGISTER = 1; private Button btnLogin; private EditText etAccount, etPassword; private CheckBox cbRemember, cbAutoLogin; private String userName = "fxjzzyo"; private String pass = "123";
-
TAG
:用于在日志中標(biāo)記的標(biāo)簽。 -
REQUEST_CODE_REGISTER
:用于在startActivityForResult
中標(biāo)識(shí)注冊(cè)活動(dòng)的請(qǐng)求代碼。 -
btnLogin
:登錄按鈕。 -
etAccount
、etPassword
:輸入用戶名和密碼的文本框。 -
cbRemember
、cbAutoLogin
:復(fù)選框,分別表示是否記住密碼和是否自動(dòng)登錄。 -
userName
、pass
:默認(rèn)的用戶名和密碼。
-
在
onCreate
方法中初始化界面元素和處理點(diǎn)擊事件:
@Override protected void onCreate(Bundle savedInstanceState) { ? ?super.onCreate(savedInstanceState); ? ?setContentView(R.layout.activity_login); ? ?getSupportActionBar().setTitle("登錄"); ? ? ?initView(); ? ?initData(); ? ? ?// ... 省略了部分代碼 ? ? ?btnLogin.setOnClickListener(new View.OnClickListener() { ? ? ? ?@Override ? ? ? ?public void onClick(View v) { ? ? ? ? ? ?// ... 處理登錄邏輯 ? ? ? } ? }); ? ? ?cbAutoLogin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { ? ? ? ?@Override ? ? ? ?public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { ? ? ? ? ? ?// ... 處理自動(dòng)登錄復(fù)選框狀態(tài)改變的邏輯 ? ? ? } ? }); ? ? ?cbRemember.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { ? ? ? ?@Override ? ? ? ?public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { ? ? ? ? ? ?// ... 處理記住密碼復(fù)選框狀態(tài)改變的邏輯 ? ? ? } ? }); }
-
initView()
方法用于初始化界面元素,將 XML 布局中的控件與變量關(guān)聯(lián)起來:
private void initView() { ? ?btnLogin = findViewById(R.id.btn_login); ? ?etAccount = findViewById(R.id.et_account); ? ?etPassword = findViewById(R.id.et_password); ? ?cbRemember = findViewById(R.id.cb_remember); ? ?cbAutoLogin = findViewById(R.id.cb_auto_login); }
-
initData()
方法用于初始化數(shù)據(jù),根據(jù)用戶的記住密碼和自動(dòng)登錄的設(shè)置,更新界面元素和變量的狀態(tài):
private void initData() { ? ?SharedPreferences spf = getSharedPreferences("spfRecord", MODE_PRIVATE); ? ?boolean isRemember = spf.getBoolean("isRemember", false); ? ?boolean isLogin = spf.getBoolean("isLogin", false); ? ?String account = spf.getString("account", ""); ? ?String password = spf.getString("password", ""); ? ? ?// ... 處理自動(dòng)登錄邏輯 ? ? ?userName = account; ? ?pass = password; ? ? ?if (isRemember) { ? ? ? ?etAccount.setText(account); ? ? ? ?etPassword.setText(password); ? ? ? ?cbRemember.setChecked(true); ? } }
-
btnLogin
按鈕的點(diǎn)擊事件處理,用于驗(yàn)證用戶輸入的用戶名和密碼是否匹配,并根據(jù)復(fù)選框的狀態(tài)更新數(shù)據(jù)到SharedPreferences中:
btnLogin.setOnClickListener(new View.OnClickListener() { ? ?@Override ? ?public void onClick(View v) { ? ? ? ?// 獲取用戶輸入的用戶名和密碼 ? ? ? ?String account = etAccount.getText().toString(); ? ? ? ?String password = etPassword.getText().toString(); ? ? ? ? ?if (TextUtils.isEmpty(userName)) { ? ? ? ? ? ?// 用戶未注冊(cè) ? ? ? ? ? ?Toast.makeText(LoginActivity.this, "還沒有注冊(cè)賬號(hào)!", Toast.LENGTH_LONG).show(); ? ? ? ? ? ?return; ? ? ? } ? ? ? ? ?if (TextUtils.equals(account, userName)) { ? ? ? ? ? ?if (TextUtils.equals(password, pass)) { ? ? ? ? ? ? ? ?// 登錄成功 ? ? ? ? ? ? ? ?// ... 處理登錄成功邏輯 ? ? ? ? ? } else { ? ? ? ? ? ? ? ?// 密碼錯(cuò)誤 ? ? ? ? ? ? ? ?Toast.makeText(LoginActivity.this, "密碼錯(cuò)誤!", Toast.LENGTH_LONG).show(); ? ? ? ? ? } ? ? ? } else { ? ? ? ? ? ?// 用戶名錯(cuò)誤 ? ? ? ? ? ?Toast.makeText(LoginActivity.this, "用戶名錯(cuò)誤!", Toast.LENGTH_LONG).show(); ? ? ? } ? } });
-
cbAutoLogin
和cbRemember
復(fù)選框狀態(tài)改變的監(jiān)聽器,確保自動(dòng)登錄時(shí)同時(shí)記住密碼,取消記住密碼時(shí)取消自動(dòng)登錄:
cbAutoLogin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { ? ?@Override ? ?public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { ? ? ? ?if (isChecked) { ? ? ? ? ? ?cbRemember.setChecked(true); ? ? ? } ? } }); ? cbRemember.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { ? ?@Override ? ?public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { ? ? ? ?if (!isChecked) { ? ? ? ? ? ?cbAutoLogin.setChecked(false); ? ? ? } ? } });
-
toRegister(View view)
方法處理前往注冊(cè)界面的按鈕點(diǎn)擊事件,啟動(dòng)RegisterActivity
來進(jìn)行用戶注冊(cè):
public void toRegister(View view) { ? ?Intent intent = new Intent(this, RegisterActivity.class); ? ?startActivityForResult(intent, REQUEST_CODE_REGISTER); }
-
onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
方法處理從注冊(cè)界面返回的結(jié)果,獲取注冊(cè)的用戶名和密碼,并更新界面元素和數(shù)據(jù):
@Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { ? ?super.onActivityResult(requestCode, resultCode, data); ? ?if (requestCode == REQUEST_CODE_REGISTER && resultCode == RegisterActivity.RESULT_CODE_REGISTER && data != null) { ? ? ? ?Bundle extras = data.getExtras(); ? ? ? ?String account = extras.getString("account", ""); ? ? ? ?String password = extras.getString("password", ""); ? ? ? ? ?etAccount.setText(account); ? ? ? ?etPassword.setText(password); ? ? ? ? ?userName = account; ? ? ? ?pass = password; ? } }
這段代碼實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的登錄功能,包括記住密碼、自動(dòng)登錄、用戶名和密碼驗(yàn)證等
RegisterActivity.Java package com.example.login; ? import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; ? import androidx.appcompat.app.AppCompatActivity; ? import com.example.angel.R; ? public class RegisterActivity extends AppCompatActivity implements View.OnClickListener { ? ? public static final int RESULT_CODE_REGISTER = 0; ? private Button btnRegister; ? private EditText etAccount,etPass,etPassConfirm; ? private CheckBox cbAgree; ? ? @Override ? protected void onCreate(Bundle savedInstanceState) { ? ? ? super.onCreate(savedInstanceState); ? ? ? setContentView(R.layout.activity_register); ? ? ? getSupportActionBar().setTitle("注冊(cè)"); ? ? ? ? etAccount = findViewById(R.id.et_account); ? ? ? etPass = findViewById(R.id.et_password); ? ? ? etPassConfirm = findViewById(R.id.et_password_confirm); ? ? ? cbAgree = findViewById(R.id.cb_agree); ? ? ? btnRegister = findViewById(R.id.btn_register); ? ? ? ? btnRegister.setOnClickListener(this); ? ? } ? ? @Override ? public void onClick(View v) { ? ? ? String name = etAccount.getText().toString(); ? ? ? String pass = etPass.getText().toString(); ? ? ? String passConfirm = etPassConfirm.getText().toString(); ? ? ? ? if (TextUtils.isEmpty(name)) { ? ? ? ? ? Toast.makeText(RegisterActivity.this, "用戶名不能為空", Toast.LENGTH_LONG).show(); ? ? ? ? ? ? return; ? ? ? } ? ? ? ? if (TextUtils.isEmpty(pass)) { ? ? ? ? ? Toast.makeText(RegisterActivity.this, "密碼不能為空", Toast.LENGTH_LONG).show(); ? ? ? ? ? return; ? ? ? } ? ? ? ? if (!TextUtils.equals(pass,passConfirm)) { ? ? ? ? ? Toast.makeText(RegisterActivity.this, "密碼不一致", Toast.LENGTH_LONG).show(); ? ? ? ? ? return; ? ? ? } ? ? ? ? if (!cbAgree.isChecked()) { ? ? ? ? ? Toast.makeText(RegisterActivity.this, "請(qǐng)同意用戶協(xié)議", Toast.LENGTH_LONG).show(); ? ? ? ? ? return; ? ? ? } ? ? ? ? // 存儲(chǔ)注冊(cè)的用戶名 密碼 ? ? ? SharedPreferences spf = getSharedPreferences("spfRecord", MODE_PRIVATE); ? ? ? SharedPreferences.Editor edit = spf.edit(); ? ? ? edit.putString("account", name); ? ? ? edit.putString("password", pass); ? ? ? edit.apply(); ? ? ? ? // 數(shù)據(jù)回傳 ? ? ? Intent intent = new Intent(); ? ? ? Bundle bundle = new Bundle(); ? ? ? bundle.putString("account",name); ? ? ? bundle.putString("password",pass); ? ? ? intent.putExtras(bundle); ? ? ? setResult(RESULT_CODE_REGISTER,intent); ? ? ? ? ? Toast.makeText(RegisterActivity.this, "注冊(cè)成功!", Toast.LENGTH_LONG).show(); ? ? ? this.finish(); ? } }
這段代碼是一個(gè)用于注冊(cè)用戶的 Android 應(yīng)用程序的一部分。我會(huì)逐行解釋每個(gè)部分的作用和功能。
-
導(dǎo)入所需的類和包:
package com.example.userprofile23_1; ? import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; ? import androidx.appcompat.app.AppCompatActivity;
在這個(gè)部分,代碼導(dǎo)入了所需的 Android 類和包,以便后續(xù)使用。這些包括處理界面元素(如 EditText、Button、CheckBox)、處理數(shù)據(jù)存儲(chǔ)(SharedPreferences)以及處理界面操作(AppCompatActivity)等。
-
創(chuàng)建 RegisterActivity 類:
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
這段代碼定義了一個(gè)名為 RegisterActivity 的類,該類繼承自 AppCompatActivity 類,并實(shí)現(xiàn)了 View.OnClickListener 接口。這意味著該類可以作為一個(gè)活動(dòng)(Activity)來運(yùn)行,并且可以響應(yīng)視圖上的點(diǎn)擊事件。
-
聲明變量和常量:
public static final int RESULT_CODE_REGISTER = 0; private Button btnRegister; private EditText etAccount, etPass, etPassConfirm; private CheckBox cbAgree;
這部分聲明了一些變量和常量,用于處理注冊(cè)界面中的各個(gè)組件。其中:
-
RESULT_CODE_REGISTER
是一個(gè)常量,用于表示注冊(cè)操作的結(jié)果代碼。 -
btnRegister
是一個(gè) Button 對(duì)象,代表注冊(cè)按鈕。 -
etAccount
、etPass
、etPassConfirm
分別是三個(gè) EditText 對(duì)象,分別用于輸入用戶名、密碼和確認(rèn)密碼。 -
cbAgree
是一個(gè) CheckBox 對(duì)象,用于表示用戶是否同意用戶協(xié)議。
-
在 onCreate 方法中進(jìn)行初始化:
@Override protected void onCreate(Bundle savedInstanceState) { ? ?super.onCreate(savedInstanceState); ? ?setContentView(R.layout.activity_register); ? ?getSupportActionBar().setTitle("注冊(cè)"); ? ? ?etAccount = findViewById(R.id.et_account); ? ?etPass = findViewById(R.id.et_password); ? ?etPassConfirm = findViewById(R.id.et_password_confirm); ? ?cbAgree = findViewById(R.id.cb_agree); ? ?btnRegister = findViewById(R.id.btn_register); ? ? ?btnRegister.setOnClickListener(this); }
在這部分代碼中,onCreate 方法是 Android 活動(dòng)的一個(gè)生命周期方法,用于在活動(dòng)創(chuàng)建時(shí)進(jìn)行初始化操作。具體的初始化步驟包括:
-
設(shè)置活動(dòng)的布局為 "activity_register",即注冊(cè)界面的布局。
-
設(shè)置活動(dòng)的標(biāo)題為 "注冊(cè)"。
-
獲取用戶名、密碼、確認(rèn)密碼和同意協(xié)議的 EditText 和 CheckBox 對(duì)象。
-
獲取注冊(cè)按鈕,并設(shè)置點(diǎn)擊監(jiān)聽器為當(dāng)前活動(dòng)(實(shí)現(xiàn)了 View.OnClickListener 接口)。
-
實(shí)現(xiàn) onClick 方法:
@Override public void onClick(View v) { ? ?String name = etAccount.getText().toString(); ? ?String pass = etPass.getText().toString(); ? ?String passConfirm = etPassConfirm.getText().toString(); ? ? ?if (TextUtils.isEmpty(name)) { ? ? ? ?Toast.makeText(RegisterActivity.this, "用戶名不能為空", Toast.LENGTH_LONG).show(); ? ? ? ?return; ? } ? ? ?if (TextUtils.isEmpty(pass)) { ? ? ? ?Toast.makeText(RegisterActivity.this, "密碼不能為空", Toast.LENGTH_LONG).show(); ? ? ? ?return; ? } ? ? ?if (!TextUtils.equals(pass, passConfirm)) { ? ? ? ?Toast.makeText(RegisterActivity.this, "密碼不一致", Toast.LENGTH_LONG).show(); ? ? ? ?return; ? } ? ? ?if (!cbAgree.isChecked()) { ? ? ? ?Toast.makeText(RegisterActivity.this, "請(qǐng)同意用戶協(xié)議", Toast.LENGTH_LONG).show(); ? ? ? ?return; ? } ? ? ?SharedPreferences spf = getSharedPreferences("spfRecord", MODE_PRIVATE); ? ?SharedPreferences.Editor edit = spf.edit(); ? ?edit.putString("account", name); ? ?edit.putString("password", pass); ? ?edit.apply(); ? ? ?Intent intent = new Intent(); ? ?Bundle bundle = new Bundle(); ? ?bundle.putString("account", name); ? ?bundle.putString("password", pass); ? ?intent.putExtras(bundle); ? ?setResult(RESULT_CODE_REGISTER, intent); ? ? ?Toast.makeText(RegisterActivity.this, "注冊(cè)成功!", Toast.LENGTH_LONG).show(); ? ?this.finish(); }
這部分代碼實(shí)現(xiàn)了在按鈕點(diǎn)擊事件中執(zhí)行的操作,主要包括:
-
獲取用戶名、密碼和確認(rèn)密碼的字符串。
-
檢查用戶名、密碼是否為空,以及確認(rèn)密碼是否與密碼一致,以及是否勾選了用戶協(xié)議的 CheckBox。
-
如果輸入有誤,通過 Toast 顯示相應(yīng)的錯(cuò)誤消息,并返回。
-
如果輸入正確,使用 SharedPreferences 對(duì)象存儲(chǔ)用戶名和密碼。
-
創(chuàng)建一個(gè) Intent,將用戶名和密碼作為額外數(shù)據(jù)存入 Bundle 中,并將 Bundle 放入 Intent 中。
-
使用 setResult 方法設(shè)置活動(dòng)的返回結(jié)果為注冊(cè)成功,并將 Intent 傳遞給上一個(gè)活動(dòng)。
-
通過 Toast 顯示注冊(cè)成功的消息,并關(guān)閉當(dāng)前活動(dòng)。
總之,這段代碼實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的用戶注冊(cè)界面,用戶需要輸入用戶名、密碼、確認(rèn)密碼,勾選用戶協(xié)議后,點(diǎn)擊注冊(cè)按鈕即可完成注冊(cè)。注冊(cè)成功后,用戶名和密碼會(huì)被保存,并傳遞給調(diào)用該界面的上一個(gè)活動(dòng)。如果有任何輸入錯(cuò)誤或未勾選用戶協(xié)議,會(huì)通過 Toast 提示用戶相應(yīng)的錯(cuò)誤信息。
ic_login_background.png
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:layout_width="match_parent" ? ?android:layout_height="match_parent" ? ?android:orientation="vertical" ? ?tools:context="com.example.login.LoginActivity" ? ?android:background="@drawable/ic_login_background"> ? ? ?<LinearLayout ? ? ? ?android:layout_width="match_parent" ? ? ? ?android:layout_height="wrap_content" ? ? ? ?android:orientation="horizontal" ? ? ? ?android:layout_marginLeft="20dp" ? ? ? ?android:layout_marginRight="20dp" ? ? ? ?android:layout_marginTop="40dp" ? ? ? ?android:gravity="center_vertical" ? ? ? ?> ? ? ? ?<TextView ? ? ? ? ? ?android:layout_width="wrap_content" ? ? ? ? ? ?android:layout_height="wrap_content" ? ? ? ? ? ?android:text="賬號(hào):" ? ? ? ? ? ?android:textSize="25sp" ? ? ? ? ? ? ?/> ? ? ? ? ?<EditText ? ? ? ? ? ?android:id="@+id/et_account" ? ? ? ? ? ?android:layout_width="match_parent" ? ? ? ? ? ?android:hint="請(qǐng)輸入用戶名或手機(jī)號(hào)" ? ? ? ? ? ?android:layout_marginLeft="10dp" ? ? ? ? ? ?style="@style/MyEditStyle" ? ? ? ? ? ?android:inputType="text" ? ? ? ? ? ?/> ? ?</LinearLayout> ? ? ?<LinearLayout ? ? ? ?android:layout_width="match_parent" ? ? ? ?android:layout_height="wrap_content" ? ? ? ?android:orientation="horizontal" ? ? ? ?android:layout_marginLeft="20dp" ? ? ? ?android:layout_marginRight="20dp" ? ? ? ?android:layout_marginTop="20dp" ? ? ? ?android:gravity="center_vertical" ? ? ? ?> ? ? ? ?<TextView ? ? ? ? ? ?android:layout_width="wrap_content" ? ? ? ? ? ?android:layout_height="wrap_content" ? ? ? ? ? ?android:text="密碼:" ? ? ? ? ? ?android:textSize="25sp" ? ? ? ? ? ? ?/> ? ? ? ? ?<EditText ? ? ? ? ? ?android:id="@+id/et_password" ? ? ? ? ? ?android:layout_width="match_parent" ? ? ? ? ? ?android:layout_height="50dp" ? ? ? ? ? ?android:hint="請(qǐng)輸入密碼" ? ? ? ? ? ?android:textSize="18sp" ? ? ? ? ? ?android:layout_marginLeft="10dp" ? ? ? ? ? ?android:paddingLeft="5dp" ? ? ? ? ? ?android:inputType="numberPassword" ? ? ? ? ? ?android:background="@drawable/edit_text_bg" ? ? ? ? ? ?/> ? ?</LinearLayout> ? ? ?<LinearLayout ? ? ? ?android:layout_width="match_parent" ? ? ? ?android:layout_height="wrap_content" ? ? ? ?android:orientation="horizontal" ? ? ? ?android:gravity="center" ? ? ? ?android:layout_marginTop="20dp" ? ? ? ?android:layout_marginLeft="20dp" ? ? ? ?android:layout_marginRight="20dp" ? ? ? ?> ? ? ? ? ?<CheckBox ? ? ? ? ? ?android:id="@+id/cb_remember" ? ? ? ? ? ?android:layout_width="wrap_content" ? ? ? ? ? ?android:layout_height="wrap_content" ? ? ? ? ? ?android:text="記住密碼" ? ? ? ? ? ?/> ? ? ? ? ?<CheckBox ? ? ? ? ? ?android:id="@+id/cb_auto_login" ? ? ? ? ? ?android:layout_width="wrap_content" ? ? ? ? ? ?android:layout_height="wrap_content" ? ? ? ? ? ?android:layout_marginLeft="40dp" ? ? ? ? ? ?android:text="自動(dòng)登錄" ? ? ? ? ? ?android:visibility="visible" /> ? ? ?</LinearLayout> ? ? ?<Button ? ? ? ?android:id="@+id/btn_login" ? ? ? ?android:layout_width="match_parent" ? ? ? ?android:layout_height="wrap_content" ? ? ? ?android:text="登錄" ? ? ? ?style="@style/MyBtnStyle" ? ? ? ?/> ? ? ?<TextView ? ? ? ?android:layout_width="wrap_content" ? ? ? ?android:layout_height="wrap_content" ? ? ? ?android:textColor="@color/colorPrimary" ? ? ? ?android:text="還沒有賬號(hào)?" ? ? ? ?android:layout_gravity="right" ? ? ? ?android:layout_marginRight="20dp" ? ? ? ?android:layout_marginTop="10dp" ? ? ? ?android:onClick="toRegister" ? ? ? ?/> ? ? ? </LinearLayout>
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="com.example.login.RegisterActivity"> ? ? ?<LinearLayout ? ? ? ?android:layout_width="match_parent" ? ? ? ?android:layout_height="wrap_content" ? ? ? ?android:layout_marginLeft="20dp" ? ? ? ?android:layout_marginTop="40dp" ? ? ? ?android:layout_marginRight="20dp" ? ? ? ?android:gravity="center_vertical" ? ? ? ?android:orientation="horizontal"> ? ? ? ? ?<TextView ? ? ? ? ? ?android:layout_width="wrap_content" ? ? ? ? ? ?android:layout_height="wrap_content" ? ? ? ? ? ?android:text="賬  號(hào):" ? ? ? ? ? ?android:textSize="25sp" /> ? ? ? ? ?<EditText ? ? ? ? ? ?android:id="@+id/et_account" ? ? ? ? ? ?style="@style/MyEditStyle" ? ? ? ? ? ?android:layout_width="match_parent" ? ? ? ? ? ?android:layout_marginLeft="10dp" ? ? ? ? ? ?android:hint="請(qǐng)輸入用戶名或手機(jī)號(hào)" ? ? ? ? ? ?android:inputType="text" /> ? ?</LinearLayout> ? ? ?<LinearLayout ? ? ? ?android:layout_width="match_parent" ? ? ? ?android:layout_height="wrap_content" ? ? ? ?android:layout_marginLeft="20dp" ? ? ? ?android:layout_marginTop="20dp" ? ? ? ?android:layout_marginRight="20dp" ? ? ? ?android:gravity="center_vertical" ? ? ? ?android:orientation="horizontal"> ? ? ? ? ?<TextView ? ? ? ? ? ?android:layout_width="wrap_content" ? ? ? ? ? ?android:layout_height="wrap_content" ? ? ? ? ? ?android:text="密  碼:" ? ? ? ? ? ?android:textSize="25sp" ? ? ? ? ? ?/> ? ? ? ? ?<EditText ? ? ? ? ? ?android:id="@+id/et_password" ? ? ? ? ? ?style="@style/MyEditStyle" ? ? ? ? ? ?android:layout_width="match_parent" ? ? ? ? ? ?android:layout_marginLeft="10dp" ? ? ? ? ? ?android:hint="請(qǐng)輸入密碼" ? ? ? ? ? ?android:inputType="numberPassword" /> ? ?</LinearLayout> ? ? ?<LinearLayout ? ? ? ?android:layout_width="match_parent" ? ? ? ?android:layout_height="wrap_content" ? ? ? ?android:layout_marginLeft="20dp" ? ? ? ?android:layout_marginTop="20dp" ? ? ? ?android:layout_marginRight="20dp" ? ? ? ?android:gravity="center_vertical" ? ? ? ?android:orientation="horizontal"> ? ? ? ? ?<TextView ? ? ? ? ? ?android:layout_width="wrap_content" ? ? ? ? ? ?android:layout_height="wrap_content" ? ? ? ? ? ?android:text="確認(rèn)密碼:" ? ? ? ? ? ?android:textSize="25sp" ? ? ? ? ? ? ?/> ? ? ? ? ?<EditText ? ? ? ? ? ?android:id="@+id/et_password_confirm" ? ? ? ? ? ?style="@style/MyEditStyle" ? ? ? ? ? ?android:layout_width="match_parent" ? ? ? ? ? ?android:layout_marginLeft="10dp" ? ? ? ? ? ?android:hint="再次輸入密碼" ? ? ? ? ? ?android:inputType="numberPassword" /> ? ?</LinearLayout> ? ? ?<Button ? ? ? ?android:id="@+id/btn_register" ? ? ? ?style="@style/MyBtnStyle" ? ? ? ?android:layout_width="match_parent" ? ? ? ?android:layout_height="wrap_content" ? ? ? ?android:text="注冊(cè)" /> ? ? ?<CheckBox ? ? ? ?android:id="@+id/cb_agree" ? ? ? ?android:layout_width="wrap_content" ? ? ? ?android:layout_height="wrap_content" ? ? ? ?android:layout_gravity="left" ? ? ? ?android:layout_marginLeft="20dp" ? ? ? ?android:layout_marginTop="10dp" ? ? ? ?android:text="勾選即同意用戶協(xié)議" ? ? ? ?android:textColor="@color/colorPrimary" /> ? ? </LinearLayout>
三.項(xiàng)目源碼
鏈接:百度網(wǎng)盤 請(qǐng)輸入提取碼** 提取碼:jynl
創(chuàng)作不易(可做實(shí)驗(yàn)報(bào)告,代碼講解等)
請(qǐng)私信作者或文章來源:http://www.zghlxwxcb.cn/news/detail-637475.html
(v)15135757306文章來源地址http://www.zghlxwxcb.cn/news/detail-637475.html
到了這里,關(guān)于Android安卓實(shí)戰(zhàn)項(xiàng)目---登陸注冊(cè)頁面(源碼在文末)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!