From c6bafa88a36edeadfe4017cda57087275357c0d3 Mon Sep 17 00:00:00 2001 From: huangrui <2311465445@qq.com> Date: Tue, 11 Jun 2024 23:47:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E4=BB=98=E5=AF=86=E7=A0=81=E3=80=81?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/hnucm/c25/AlterPhoneActivity.java | 17 ++++-- .../com/hnucm/c25/Alterphone_sendmessage.java | 60 +++++++++++++++++-- .../main/java/com/hnucm/c25/BoxEditText.java | 53 +++++++++------- .../main/java/com/hnucm/c25/ConfirmOrder.java | 21 +++++-- .../main/java/com/hnucm/c25/Phone_enter.java | 30 +++++----- .../main/java/com/hnucm/c25/SetActivity.java | 17 ++++++ app/src/main/res/drawable/number_selector.xml | 20 +++++++ .../main/res/layout/activity_alter_phone.xml | 1 + .../activity_alterphone_sendmessage.xml | 16 ++++- app/src/main/res/layout/activity_set.xml | 19 +++++- app/src/main/res/values-night/colors.xml | 4 ++ 11 files changed, 205 insertions(+), 53 deletions(-) create mode 100644 app/src/main/res/drawable/number_selector.xml create mode 100644 app/src/main/res/values-night/colors.xml diff --git a/app/src/main/java/com/hnucm/c25/AlterPhoneActivity.java b/app/src/main/java/com/hnucm/c25/AlterPhoneActivity.java index ca72d2b..9b4fd5f 100644 --- a/app/src/main/java/com/hnucm/c25/AlterPhoneActivity.java +++ b/app/src/main/java/com/hnucm/c25/AlterPhoneActivity.java @@ -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); + } } }); } diff --git a/app/src/main/java/com/hnucm/c25/Alterphone_sendmessage.java b/app/src/main/java/com/hnucm/c25/Alterphone_sendmessage.java index a8cc433..f9815bc 100644 --- a/app/src/main/java/com/hnucm/c25/Alterphone_sendmessage.java +++ b/app/src/main/java/com/hnucm/c25/Alterphone_sendmessage.java @@ -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()); + } } \ No newline at end of file diff --git a/app/src/main/java/com/hnucm/c25/BoxEditText.java b/app/src/main/java/com/hnucm/c25/BoxEditText.java index d11dbe7..b094863 100644 --- a/app/src/main/java/com/hnucm/c25/BoxEditText.java +++ b/app/src/main/java/com/hnucm/c25/BoxEditText.java @@ -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()触发重绘 } } \ No newline at end of file diff --git a/app/src/main/java/com/hnucm/c25/ConfirmOrder.java b/app/src/main/java/com/hnucm/c25/ConfirmOrder.java index bb03fa2..db19dae 100644 --- a/app/src/main/java/com/hnucm/c25/ConfirmOrder.java +++ b/app/src/main/java/com/hnucm/c25/ConfirmOrder.java @@ -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); diff --git a/app/src/main/java/com/hnucm/c25/Phone_enter.java b/app/src/main/java/com/hnucm/c25/Phone_enter.java index c422f93..df70e2c 100644 --- a/app/src/main/java/com/hnucm/c25/Phone_enter.java +++ b/app/src/main/java/com/hnucm/c25/Phone_enter.java @@ -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)); } }); } diff --git a/app/src/main/java/com/hnucm/c25/SetActivity.java b/app/src/main/java/com/hnucm/c25/SetActivity.java index 631861f..a71d8f6 100644 --- a/app/src/main/java/com/hnucm/c25/SetActivity.java +++ b/app/src/main/java/com/hnucm/c25/SetActivity.java @@ -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(); } } \ No newline at end of file diff --git a/app/src/main/res/drawable/number_selector.xml b/app/src/main/res/drawable/number_selector.xml new file mode 100644 index 0000000..22154b2 --- /dev/null +++ b/app/src/main/res/drawable/number_selector.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_alter_phone.xml b/app/src/main/res/layout/activity_alter_phone.xml index 40e103b..304b035 100644 --- a/app/src/main/res/layout/activity_alter_phone.xml +++ b/app/src/main/res/layout/activity_alter_phone.xml @@ -61,6 +61,7 @@ app:layout_constraintTop_toTopOf="parent" /> + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_set.xml b/app/src/main/res/layout/activity_set.xml index 8124ee2..27d1c49 100644 --- a/app/src/main/res/layout/activity_set.xml +++ b/app/src/main/res/layout/activity_set.xml @@ -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" /> + + + \ No newline at end of file diff --git a/app/src/main/res/values-night/colors.xml b/app/src/main/res/values-night/colors.xml new file mode 100644 index 0000000..0d2c4cc --- /dev/null +++ b/app/src/main/res/values-night/colors.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file