Android安卓实战项目—登陆注册页面(源码在文末🐕🐕🐕)
一.项目运行介绍
效果图:
二.具体实现
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, "还没有注册账号!", 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, "密码错误!", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(LoginActivity.this, "用户名错误!", 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;
}
}
}
这段代码是一个简单的Android应用中的登录功能代码,下面逐行解释其功能和作用:
-
导入相关的类和包:
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;
这些导入语句引入了用于构建Android应用界面、处理用户交互和数据存储的必要类和包。
-
定义一个名为
LoginActivity
的类,它继承自AppCompatActivity
,表示这是一个用于处理界面的活动(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
:用于在日志中标记的标签。 -
REQUEST_CODE_REGISTER
:用于在startActivityForResult
中标识注册活动的请求代码。 -
btnLogin
:登录按钮。 -
etAccount
、etPassword
:输入用户名和密码的文本框。 -
cbRemember
、cbAutoLogin
:复选框,分别表示是否记住密码和是否自动登录。 -
userName
、pass
:默认的用户名和密码。
-
在
onCreate
方法中初始化界面元素和处理点击事件:
@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) {
// ... 处理自动登录复选框状态改变的逻辑
}
});
cbRemember.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// ... 处理记住密码复选框状态改变的逻辑
}
});
}
-
initView()
方法用于初始化界面元素,将 XML 布局中的控件与变量关联起来:
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()
方法用于初始化数据,根据用户的记住密码和自动登录的设置,更新界面元素和变量的状态:
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", "");
// ... 处理自动登录逻辑
userName = account;
pass = password;
if (isRemember) {
etAccount.setText(account);
etPassword.setText(password);
cbRemember.setChecked(true);
}
}
-
btnLogin
按钮的点击事件处理,用于验证用户输入的用户名和密码是否匹配,并根据复选框的状态更新数据到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)) {
// 用户未注册
Toast.makeText(LoginActivity.this, "还没有注册账号!", Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.equals(account, userName)) {
if (TextUtils.equals(password, pass)) {
// 登录成功
// ... 处理登录成功逻辑
} else {
// 密码错误
Toast.makeText(LoginActivity.this, "密码错误!", Toast.LENGTH_LONG).show();
}
} else {
// 用户名错误
Toast.makeText(LoginActivity.this, "用户名错误!", Toast.LENGTH_LONG).show();
}
}
});
-
cbAutoLogin
和cbRemember
复选框状态改变的监听器,确保自动登录时同时记住密码,取消记住密码时取消自动登录:
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)
方法处理前往注册界面的按钮点击事件,启动RegisterActivity
来进行用户注册:
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)
方法处理从注册界面返回的结果,获取注册的用户名和密码,并更新界面元素和数据:
@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;
}
}
这段代码实现了一个简单的登录功能,包括记住密码、自动登录、用户名和密码验证等
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("注册");
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, "请同意用户协议", 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, "注册成功!", Toast.LENGTH_LONG).show();
this.finish();
}
}
这段代码是一个用于注册用户的 Android 应用程序的一部分。我会逐行解释每个部分的作用和功能。
-
导入所需的类和包:
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;
在这个部分,代码导入了所需的 Android 类和包,以便后续使用。这些包括处理界面元素(如 EditText、Button、CheckBox)、处理数据存储(SharedPreferences)以及处理界面操作(AppCompatActivity)等。
-
创建 RegisterActivity 类:
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
这段代码定义了一个名为 RegisterActivity 的类,该类继承自 AppCompatActivity 类,并实现了 View.OnClickListener 接口。这意味着该类可以作为一个活动(Activity)来运行,并且可以响应视图上的点击事件。
-
声明变量和常量:
public static final int RESULT_CODE_REGISTER = 0;
private Button btnRegister;
private EditText etAccount, etPass, etPassConfirm;
private CheckBox cbAgree;
这部分声明了一些变量和常量,用于处理注册界面中的各个组件。其中:
-
RESULT_CODE_REGISTER
是一个常量,用于表示注册操作的结果代码。 -
btnRegister
是一个 Button 对象,代表注册按钮。 -
etAccount
、etPass
、etPassConfirm
分别是三个 EditText 对象,分别用于输入用户名、密码和确认密码。 -
cbAgree
是一个 CheckBox 对象,用于表示用户是否同意用户协议。
-
在 onCreate 方法中进行初始化:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
getSupportActionBar().setTitle("注册");
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 活动的一个生命周期方法,用于在活动创建时进行初始化操作。具体的初始化步骤包括:
-
设置活动的布局为 “activity_register”,即注册界面的布局。
-
设置活动的标题为 “注册”。
-
获取用户名、密码、确认密码和同意协议的 EditText 和 CheckBox 对象。
-
获取注册按钮,并设置点击监听器为当前活动(实现了 View.OnClickListener 接口)。
-
实现 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, "请同意用户协议", 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, "注册成功!", Toast.LENGTH_LONG).show();
this.finish();
}
这部分代码实现了在按钮点击事件中执行的操作,主要包括:
-
获取用户名、密码和确认密码的字符串。
-
检查用户名、密码是否为空,以及确认密码是否与密码一致,以及是否勾选了用户协议的 CheckBox。
-
如果输入有误,通过 Toast 显示相应的错误消息,并返回。
-
如果输入正确,使用 SharedPreferences 对象存储用户名和密码。
-
创建一个 Intent,将用户名和密码作为额外数据存入 Bundle 中,并将 Bundle 放入 Intent 中。
-
使用 setResult 方法设置活动的返回结果为注册成功,并将 Intent 传递给上一个活动。
-
通过 Toast 显示注册成功的消息,并关闭当前活动。
总之,这段代码实现了一个简单的用户注册界面,用户需要输入用户名、密码、确认密码,勾选用户协议后,点击注册按钮即可完成注册。注册成功后,用户名和密码会被保存,并传递给调用该界面的上一个活动。如果有任何输入错误或未勾选用户协议,会通过 Toast 提示用户相应的错误信息。
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="账号:"
android:textSize="25sp"
/>
<EditText
android:id="@+id/et_account"
android:layout_width="match_parent"
android:hint="请输入用户名或手机号"
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="请输入密码"
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="自动登录"
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="还没有账号?"
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="账  号:"
android:textSize="25sp" />
<EditText
android:id="@+id/et_account"
style="@style/MyEditStyle"
android:layout_width="match_parent"
android:layout_marginLeft="10dp"
android:hint="请输入用户名或手机号"
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="请输入密码"
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="确认密码:"
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="注册" />
<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="勾选即同意用户协议"
android:textColor="@color/colorPrimary" />
</LinearLayout>
三.项目源码
链接:百度网盘 请输入提取码** 提取码:jynl
创作不易(可做实验报告,代码讲解等)
请私信作者或
(v)15135757306
文章出处登录后可见!