支付密码、验证码

This commit is contained in:
huangrui 2024-06-11 23:47:05 +08:00
parent d10f76a9d1
commit c6bafa88a3
11 changed files with 205 additions and 53 deletions

View File

@ -9,6 +9,7 @@ import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.gyf.immersionbar.ImmersionBar;
@ -22,8 +23,8 @@ public class AlterPhoneActivity extends AppCompatActivity {
ImageView back = findViewById(R.id.imageView13);
CardView next = findViewById(R.id.cardview_next);
EditText phone = findViewById(R.id.phone);
String phonenumber = phone.getText().toString();
Log.i("phonenumber",phonenumber);
EditText id = findViewById(R.id.id);
//todo 返回
back.setOnClickListener(new View.OnClickListener() {
@Override
@ -36,9 +37,15 @@ public class AlterPhoneActivity extends AppCompatActivity {
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(AlterPhoneActivity.this,Alterphone_sendmessage.class);
intent.putExtra("phone",phonenumber);
startActivity(intent);
if(!id.getText().toString().isEmpty()&&!phone.getText().toString().isEmpty()) {
Intent intent = new Intent(AlterPhoneActivity.this, Alterphone_sendmessage.class);
String phonenumber = phone.getText().toString();
Log.i("phonenumber", phonenumber);
intent.putExtra("phone", phonenumber);
startActivity(intent);
}else{
Toast.makeText(AlterPhoneActivity.this,"身份证号和手机号不能为空",Toast.LENGTH_SHORT);
}
}
});
}

View File

@ -2,18 +2,31 @@ package com.hnucm.c25;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import androidx.core.app.NotificationCompat;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.gyf.immersionbar.ImmersionBar;
public class Alterphone_sendmessage extends AppCompatActivity {
import java.util.Random;
public class Alterphone_sendmessage extends AppCompatActivity {
TextView verificationCodeMessageTextView;
NotificationManager manager;
String CHANNEL_ID = "VerificationCodeChannel";
boolean isLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -25,6 +38,21 @@ public class Alterphone_sendmessage extends AppCompatActivity {
TextView phonenumber = findViewById(R.id.textView62);
CardView next = findViewById(R.id.cardview_next);
phonenumber.setText(phone);
EditText yanzheng = findViewById(R.id.yanzheng);
verificationCodeMessageTextView = findViewById(R.id.textView148);
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);//获取系统服务
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Verification Code";//存储通知渠道的名称
String description = "Channel for verification code notifications";//存储通知渠道的描述
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
manager.createNotificationChannel(channel);
}
Random random = new Random();
int randomNumber = random.nextInt(900000) + 100000;
showVerificationCode(String.valueOf(randomNumber));
//todo 返回
back.setOnClickListener(new View.OnClickListener() {
@Override
@ -37,10 +65,34 @@ public class Alterphone_sendmessage extends AppCompatActivity {
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Alterphone_sendmessage.this, Shouye.class);
intent.putExtra("id",1);
startActivity(intent);
if(yanzheng.getText().toString().equals(String.valueOf(randomNumber))) {
Intent intent = new Intent(Alterphone_sendmessage.this, Shouye.class);
isLogin=false;
intent.putExtra("id", 1);
intent.putExtra("isLogin",isLogin);
startActivity(intent);
}else{
Toast.makeText(Alterphone_sendmessage.this,"验证码错误",Toast.LENGTH_SHORT);
}
}
});
}
private void showVerificationCode(String yanzhengma) {
String verificationCodeMessage = String.format(
"【易通行】验证码:%s 。您正在使用短信验证码登录功能,验证码提供他人可能导致账号被盗,请勿转发或泄漏。",
yanzhengma);
verificationCodeMessageTextView.setText(verificationCodeMessage);
verificationCodeMessageTextView.setVisibility(View.VISIBLE);
// 5秒后隐藏验证码消息
new Handler().postDelayed(() -> verificationCodeMessageTextView.setVisibility(View.GONE), 5000);
// 构建通知
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.infor) // 设置通知小图标
.setContentTitle("验证码通知") // 设置通知标题
.setContentText(verificationCodeMessage) // 设置通知文本内容
.setPriority(NotificationCompat.PRIORITY_HIGH); // 设置通知优先级
manager.notify(1, builder.build());
}
}

View File

@ -1,18 +1,15 @@
package com.hnucm.c25;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.InputFilter;
import android.util.AttributeSet;
import android.view.Gravity;
import androidx.appcompat.widget.AppCompatEditText;
public class BoxEditText extends AppCompatEditText {
private Paint paint;
private int boxSize; // 格子大小
private int gridSize; // 格子数量
@ -40,39 +37,55 @@ public class BoxEditText extends AppCompatEditText {
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
// 设置最大字符数为格子数量
setFilters(new InputFilter[]{new InputFilter.LengthFilter(gridSize)});
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 绘制分隔线
float cellWidth = getWidth() / gridSize; // 假设gridSize为3
float cellWidth = getWidth() / gridSize;
for (int i = 1; i < gridSize; i++) {
float x = i * cellWidth;
canvas.drawLine(x, 0, x, getHeight(), paint);
}
// 计算文本的宽度
// 绘制文本
drawText(canvas);
}
private void drawText(Canvas canvas) {
Paint textPaint = getPaint();
textPaint.setTextSize(getTextSize());
textPaint.setColor(Color.BLACK); // 设置文本颜色
textPaint.setTextAlign(Paint.Align.CENTER);
String textToDraw = getText().toString();
if (!textToDraw.isEmpty()) {
float charY = (getHeight() - textPaint.descent() - textPaint.ascent()) / 2; // 字符垂直居中
for (int i = 0; i < textToDraw.length(); i++) {
char c = textToDraw.charAt(i);
float charWidth = textPaint.measureText(String.valueOf(c));
float charY = (getHeight() - textPaint.descent() - textPaint.ascent()) / 2;
// 计算字符在小格子中的x坐标使其居中
float charX = (i - 1) * boxSize + (cellWidth - charWidth) / 2;
// 绘制字符
canvas.drawText(String.valueOf(c), charX, charY, textPaint);
}
for (int i = 0; i < Math.min(currentTextLength, textToDraw.length()); i++) {
char c = textToDraw.charAt(i);
float charWidth = textPaint.measureText(String.valueOf(c));
float charX = i * boxSize + (boxSize - charWidth) / 2;
// canvas.drawText(String.valueOf(c), charX, charY, textPaint);
canvas.drawText("·", charX, charY, textPaint);
}
// 如果当前文本长度大于currentTextLength绘制新增的数字
if (textToDraw.length() > currentTextLength) {
char newChar = textToDraw.charAt(currentTextLength);
float newCharWidth = textPaint.measureText(String.valueOf(newChar));
float newCharX = currentTextLength * boxSize + (boxSize - newCharWidth) / 2;
// canvas.drawText(String.valueOf(newChar), newCharX, charY, textPaint);
canvas.drawText("·", newCharX, charY, textPaint);
}
// 更新currentTextLength
currentTextLength = textToDraw.length();
}
@Override
public void setText(CharSequence text, BufferType type) {
super.setText(text, type);
currentTextLength = text.length(); // 在设置文本时更新当前文本长度
invalidate(); // 调用invalidate()触发重绘
}
}

View File

@ -12,6 +12,7 @@ import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
@ -123,7 +124,7 @@ public class ConfirmOrder extends AppCompatActivity {
final String number = numbers[i];
TextView shuzi = findViewById(layoutIds[i]);
// 为每个数字按钮设置点击事件
// 数字按钮设置点击事件
shuzi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
@ -144,24 +145,34 @@ public class ConfirmOrder extends AppCompatActivity {
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myEditText.post(new Runnable() {
@Override
public void run() {
Editable text = myEditText.getText();
int cursorPosition = myEditText.getSelectionStart();
if (cursorPosition > 0) {
text.delete(cursorPosition - 1, cursorPosition);
// 有时需要强制更新EditText的状态
myEditText.setText(text);
myEditText.setSelection(cursorPosition - 1);
}
}
});
}
});
}
private void appendNumberToEditText(String number) {
// 将数字添加到EditText
Editable editable = myEditText.getText();
editable.append(number);
myEditText.setSelection(editable.length()); // 移动光标到末尾
if (editable.length() >= 6) {
if (editable.length() > 6) {
// 密码长度已达到预期长度检查密码是否正确
checkPassword(editable.toString());
}
}
private void checkPassword(String inputPassword) {
// String inputPassword = myEditText.getText().toString();
if ("025802".equals(inputPassword)) {
// 密码正确跳转到购买成功界面
Intent intent = new Intent(ConfirmOrder.this, BuySuscuss.class);

View File

@ -22,6 +22,7 @@ import com.google.gson.Gson;
import java.io.IOException;
import java.util.List;
import java.util.Random;
import okhttp3.Call;
import okhttp3.Callback;
@ -42,6 +43,7 @@ public class Phone_enter extends AppCompatActivity {
String CHANNEL_ID = "VerificationCodeChannel";
boolean isLogin;
int randomNumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -134,16 +136,16 @@ public class Phone_enter extends AppCompatActivity {
String inputAccount = phonenumber.getText().toString();
String inputPassword = yanzhengma.getText().toString();
// 查找输入手机号对应的验证码
Phone_Account.PhoneDTO matchedPhoneDTO = null;
for (Phone_Account.PhoneDTO phoneDTO : phoneDTOS) {
if (inputAccount.equals(phoneDTO.phonenumber)) {
matchedPhoneDTO = phoneDTO;
break;
}
}
// // 查找输入手机号对应的验证码
// Phone_Account.PhoneDTO matchedPhoneDTO = null;
// for (Phone_Account.PhoneDTO phoneDTO : phoneDTOS) {
// if (inputAccount.equals(phoneDTO.phonenumber)) {
// matchedPhoneDTO = phoneDTO;
// break;
// }
// }
if (matchedPhoneDTO != null && inputPassword.equals(matchedPhoneDTO.yanzhengma)) {
if ( inputPassword.equals(String.valueOf(randomNumber))) {
// 手机号和验证码匹配
Log.i("Login", "Success");
isLogin=true;
@ -163,13 +165,9 @@ public class Phone_enter extends AppCompatActivity {
sendCode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String inputAccount = phonenumber.getText().toString();
for (Phone_Account.PhoneDTO phoneDTO : phoneDTOS) {
if (inputAccount.equals(phoneDTO.phonenumber)) {
showVerificationCode(phoneDTO.yanzhengma);
break;
}
}
Random random = new Random();
randomNumber = random.nextInt(900000) + 100000;
showVerificationCode(String.valueOf(randomNumber));
}
});
}

View File

@ -1,13 +1,16 @@
package com.hnucm.c25;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.cardview.widget.CardView;
import androidx.constraintlayout.widget.ConstraintLayout;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
@ -87,5 +90,19 @@ public class SetActivity extends AppCompatActivity {
queding.setVisibility(View.INVISIBLE);
}
});
//夜间模式
Switch night = findViewById(R.id.switch2);
night.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
toggleNightMode(isChecked);
}
});
}
private void toggleNightMode(boolean nightMode) {
AppCompatDelegate.setDefaultNightMode(
nightMode ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO);
// 重新应用主题
recreate();
}
}

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false">
<shape>
<stroke android:width="1dp" android:color="#8B9093"/>
</shape>
</item>
<item android:state_enabled="true" android:state_pressed="false">
<shape>
<stroke android:width="1dp" android:color="#F5F7F8"/>
</shape>
</item>
<item android:state_enabled="true" android:state_pressed="true">
<shape>
<solid android:color="#C0C4C7"/>
<stroke android:width="1dp" android:color="#8B9093"/>
</shape>
</item>
</selector>

View File

@ -61,6 +61,7 @@
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="100dp"

View File

@ -91,11 +91,12 @@
app:layout_constraintTop_toBottomOf="@+id/textView62" />
<EditText
android:id="@+id/yanzheng"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@null"
android:hint="输入验证码"
android:gravity="center"
android:hint="输入验证码"
android:textColor="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
@ -135,4 +136,17 @@
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<TextView
android:id="@+id/textView148"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="@color/white"
android:lineSpacingExtra="6dp"
android:textColor="@color/black"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -192,7 +192,7 @@
android:layout_height="500dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:visibility="invisible"
android:visibility="gone"
app:cardCornerRadius="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
@ -271,7 +271,7 @@
android:layout_height="120dp"
android:layout_marginStart="64dp"
android:layout_marginEnd="64dp"
android:visibility="invisible"
android:visibility="gone"
app:cardCornerRadius="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
@ -315,4 +315,19 @@
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
<Switch
android:id="@+id/switch2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="16dp"
android:background="@color/white"
android:paddingLeft="16dp"
android:text="夜间模式"
android:textColor="@color/black"
android:textSize="17sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/constraintLayout40" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
</resources>