利用android代码实现用户登录注册功能
时间: 2023-07-30 10:04:27 浏览: 162
好的,以下是使用 Android Studio 编写的用户登录注册功能的代码实现。这里使用了 Firebase Authentication 来实现用户身份验证,具体步骤如下:
1. 在 Firebase 控制台中创建一个项目,启用 Firebase Authentication 并按照指示添加 Android 应用程序;
2. 在项目中添加以下依赖项:
```gradle
implementation 'com.google.firebase:firebase-auth:21.0.1'
implementation 'com.google.firebase:firebase-database:20.0.2'
```
3. 在布局文件中创建登录和注册页面的 UI 界面,比如 `activity_login.xml` 和 `activity_register.xml`;
4. 创建 `LoginActivity` 和 `RegisterActivity` 两个 Activity 来处理登录和注册逻辑,以下是两个 Activity 的代码实现:
LoginActivity.java
```java
public class LoginActivity extends AppCompatActivity {
private EditText emailEditText, passwordEditText;
private Button loginButton, registerButton;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mAuth = FirebaseAuth.getInstance();
emailEditText = findViewById(R.id.emailEditText);
passwordEditText = findViewById(R.id.passwordEditText);
loginButton = findViewById(R.id.loginButton);
registerButton = findViewById(R.id.registerButton);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = emailEditText.getText().toString().trim();
String password = passwordEditText.getText().toString().trim();
if (TextUtils.isEmpty(email)) {
Toast.makeText(LoginActivity.this, "请输入邮箱地址", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
return;
}
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
} else {
Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
}
}
});
}
});
registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(intent);
}
});
}
}
```
RegisterActivity.java
```java
public class RegisterActivity extends AppCompatActivity {
private EditText nameEditText, emailEditText, passwordEditText;
private Button registerButton;
private FirebaseAuth mAuth;
private DatabaseReference mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mAuth = FirebaseAuth.getInstance();
mDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
nameEditText = findViewById(R.id.nameEditText);
emailEditText = findViewById(R.id.emailEditText);
passwordEditText = findViewById(R.id.passwordEditText);
registerButton = findViewById(R.id.registerButton);
registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String name = nameEditText.getText().toString().trim();
String email = emailEditText.getText().toString().trim();
String password = passwordEditText.getText().toString().trim();
if (TextUtils.isEmpty(name)) {
Toast.makeText(RegisterActivity.this, "请输入姓名", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(email)) {
Toast.makeText(RegisterActivity.this, "请输入邮箱地址", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
return;
}
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
String user_id = mAuth.getCurrentUser().getUid();
DatabaseReference current_user_db = mDatabase.child(user_id);
current_user_db.child("name").setValue(name);
current_user_db.child("image").setValue("default");
Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
startActivity(intent);
finish();
} else {
Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
}
```
以上代码实现了用户的注册和登录功能。具体而言,`LoginActivity` 中的 `onCreate` 方法实现了登录逻辑,当用户点击登录按钮时,程序会检查邮箱地址和密码是否为空,然后调用 `signInWithEmailAndPassword` 方法进行登录操作。如果登录成功,程序就会跳转到 `MainActivity`。
`RegisterActivity` 中的 `onCreate` 方法实现了注册逻辑,当用户点击注册按钮时,程序会检查姓名、邮箱地址和密码是否为空,然后调用 `createUserWithEmailAndPassword` 方法进行注册操作。如果注册成功,程序就会将用户的姓名和默认头像保存到 Firebase 数据库中,并跳转到 `MainActivity`。
希望这个代码示例能够帮助到你!
阅读全文