Skip to content

Commit

Permalink
新增:再次输入密码,验证验证码,邮箱功能(未完全实现)
Browse files Browse the repository at this point in the history
修改:
删除:
完成:
下一个目标:
  • Loading branch information
ACodeHX committed Jun 9, 2024
1 parent c9ef966 commit 91ece53
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 38 deletions.
15 changes: 0 additions & 15 deletions .gitignore

This file was deleted.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 80 additions & 9 deletions app/src/main/java/com/aa/takeout/RegisterActivity.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
package com.aa.takeout;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;

public class RegisterActivity extends AppCompatActivity {
private EditText newUser;
private EditText newPassword;
private EditText reEnterPasswd;
private EditText userEmail;
private EditText authCode;
private Button signUpLogin;
private Button register;
private Button sendCode;
private DatabaseHelper userDate;
private int authCodeValue;
private long startTime;


@Override
Expand All @@ -28,8 +38,12 @@ protected void onCreate(Bundle savedInstanceState) {

newUser = findViewById(R.id.newUser);
newPassword = findViewById(R.id.regPass);
reEnterPasswd = findViewById(R.id.reenterpasswd);
userEmail = findViewById(R.id.useremail);
authCode = findViewById(R.id.authcode);
register = findViewById(R.id.regSucceed);
signUpLogin = findViewById(R.id.returnLogin);
sendCode = findViewById(R.id.sendcode);
userDate = new DatabaseHelper(this);

//注册用户的按钮
Expand All @@ -38,22 +52,42 @@ protected void onCreate(Bundle savedInstanceState) {
public void onClick(View view) {
String userName = newUser.getText().toString();
String passVaule = newPassword.getText().toString();
String reEnterPasswdValue = reEnterPasswd.getText().toString();
String email = userEmail.getText().toString();
String authcode = authCode.getText().toString();

//判断账号和密码是否为空
if (userName.isEmpty() || passVaule.isEmpty()) {
Toast.makeText(RegisterActivity.this, "请输入账号或密码", Toast.LENGTH_SHORT).show();
String judgeValue = userName.isEmpty() ? "请输入账号" :
email.isEmpty() ? "请输入邮箱" :
passVaule.isEmpty() ? "请输入密码" :
reEnterPasswdValue.isEmpty() ? "请再次输入密码" :
authcode.isEmpty() ? "请输入验证码" :
null;


if (judgeValue != null) {
Toast.makeText(RegisterActivity.this, judgeValue, Toast.LENGTH_SHORT).show();
return;
}

if (userDate.insertUser(userName,passVaule)) {
saveToFile(userName, passVaule);
Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
Intent loginIntent = new Intent(RegisterActivity.this, MainActivity.class);
startActivity(loginIntent);
int changeAuthCode;
changeAuthCode = Integer.parseInt(authcode);
if (reEnterPasswdValue.equals(passVaule)) {
if (userDate.insertUser(userName, passVaule)) {
if (authCodeValue == changeAuthCode && isValidCode()) {
saveToFile(userName, passVaule);
Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
Intent loginIntent = new Intent(RegisterActivity.this, MainActivity.class);
startActivity(loginIntent);
} else {
Toast.makeText(RegisterActivity.this, "验证码不正确或验证码以过期", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(RegisterActivity.this, "用户已存在", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(RegisterActivity.this, "用户已存在",Toast.LENGTH_SHORT).show();
Toast.makeText(RegisterActivity.this, "密码不一致", Toast.LENGTH_SHORT).show();
}

}
});

Expand All @@ -65,8 +99,45 @@ public void onClick(View view) {
}
});

//发送短信逻辑
sendCode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String email = userEmail.getText().toString();
String Emailregex = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";
Boolean judgeEmail = false;
judgeEmail = email != null && email.matches(Emailregex);
if (judgeEmail) {
//设置验证码的逻辑
Random random = new Random();
authCodeValue = random.nextInt(100000);
AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
builder.setTitle("请在5分钟输入你的验证码")
.setMessage("您的验证码为: " + authCodeValue)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
}).show();
} else if(email.isEmpty()) {
Toast.makeText(RegisterActivity.this, "请输入邮箱", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(RegisterActivity.this, "请输入正确的邮箱", Toast.LENGTH_SHORT).show();
}
}
});


}
//验证时间
private boolean isValidCode() {
long currentTime = System.currentTimeMillis();
long elapsedTime = currentTime - startTime;
return elapsedTime <= 300000; // 300 seconds (5 minutes) in milliseconds
}


//保存密码和账号到指定文件
private void saveToFile(String userName, String password) {
String data = "账号:" + userName + ",密码" + password + "\n";
Expand Down
71 changes: 58 additions & 13 deletions app/src/main/res/layout/register.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@

<EditText
android:id="@+id/newUser"
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="100dp"
android:layout_marginTop="100dp"
android:layout_marginEnd="100dp"
android:layout_marginTop="30dp"
android:ems="10"
android:hint="@string/newuser"
android:inputType="textEmailAddress"
Expand All @@ -29,8 +27,8 @@
android:layout_width="200dp"
android:layout_height="65dp"
android:layout_marginTop="110dp"
android:text="注册"
android:gravity="center"
android:text="注册"
android:textColor="#808080"
android:textSize="50dp"
app:layout_constraintEnd_toEndOf="parent"
Expand All @@ -40,36 +38,83 @@

<EditText
android:id="@+id/regPass"
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="100dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="100dp"
android:layout_marginTop="30dp"
android:ems="10"
android:hint="@string/newPass"
android:inputType="textPassword"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/newUser" />
app:layout_constraintTop_toBottomOf="@+id/useremail" />

<Button
android:id="@+id/regSucceed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="90dp"
android:layout_marginTop="100dp"
android:layout_marginTop="20dp"
android:text="注册"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/regPass" />
app:layout_constraintTop_toBottomOf="@+id/authcode" />

<Button
android:id="@+id/returnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="90dp"
android:text="返回"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/authcode" />

<EditText
android:id="@+id/reenterpasswd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:ems="10"
android:hint="再次输入密码"
android:inputType="textPassword"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/regPass" />

<EditText
android:id="@+id/useremail"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="140dp"
android:ems="10"
android:hint="请输入邮箱"
android:inputType="textEmailAddress"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/newUser" />

<EditText
android:id="@+id/authcode"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:ems="10"
android:hint="请输入验证码"
android:inputType="number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/reenterpasswd" />

<Button
android:id="@+id/sendcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="27dp"
android:text="发送短信"
app:layout_constraintStart_toEndOf="@+id/useremail"
app:layout_constraintTop_toBottomOf="@+id/newUser" />

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 91ece53

Please sign in to comment.