Files
yusheng-android/BaseModule/src/main/java/com/xscm/moduleutil/dialog/ConfirmDialog.java
lzl 079d9ab1b1 1.签约抱麦添加拒绝逻辑。
2.爵位不返回userInfo 逻辑容错。
3.礼物动画优化。
2025-12-08 19:00:15 +08:00

184 lines
7.0 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.xscm.moduleutil.dialog;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.text.SpannableString;
import android.widget.TextView;
import androidx.constraintlayout.widget.ConstraintLayout;
import com.xscm.moduleutil.R;
import com.xscm.moduleutil.color.ThemeableDrawableUtils;
import com.xscm.moduleutil.utils.ColorManager;
/**
*@author qx
*@data 2025/6/16
*@description: 确认弹框,使用在动态删除评论
*/
public class ConfirmDialog extends Dialog {
private String title;
private String message;
private SpannableString spannableMessage;
private String positiveButtonText;
private String negativeButtonText;
private View.OnClickListener positiveButtonClickListener;
private View.OnClickListener negativeButtonClickListener;
private boolean isCountdownEnabled = false; // 是否启用倒计时
private int countdownSeconds = 0; // 倒计时秒数
private CountDownTimer countDownTimer; // 倒计时对象
public ConfirmDialog(Context context, String title, String message,
String positiveButtonText, String negativeButtonText,
View.OnClickListener positiveButtonClickListener,
View.OnClickListener negativeButtonClickListener,
boolean isCountdownEnabled, int countdownSeconds) {
super(context);
this.title = title;
this.message = message;
this.positiveButtonText = positiveButtonText;
this.negativeButtonText = negativeButtonText;
this.positiveButtonClickListener = positiveButtonClickListener;
this.negativeButtonClickListener = negativeButtonClickListener;
this.isCountdownEnabled = isCountdownEnabled;
this.countdownSeconds = countdownSeconds;
}
// 新增构造函数支持SpannableString
public ConfirmDialog(Context context, String title, SpannableString spannableMessage,
String positiveButtonText, String negativeButtonText,
View.OnClickListener positiveButtonClickListener,
View.OnClickListener negativeButtonClickListener,
boolean isCountdownEnabled, int countdownSeconds) {
super(context);
this.title = title;
this.spannableMessage = spannableMessage;
this.positiveButtonText = positiveButtonText;
this.negativeButtonText = negativeButtonText;
this.positiveButtonClickListener = positiveButtonClickListener;
this.negativeButtonClickListener = negativeButtonClickListener;
this.isCountdownEnabled = isCountdownEnabled;
this.countdownSeconds = countdownSeconds;
}
private void init() {
Window window = getWindow();
if (window != null) {
window.setGravity(Gravity.CENTER); // 居中显示
window.setBackgroundDrawableResource(R.drawable.bg_r16_fff); // 透明背景
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_confirm);
init();
// 动态设置背景
// getWindow().setBackgroundDrawableResource(R.drawable.bg_r16_fff);
// 初始化视图
TextView tvTitle = findViewById(R.id.tv_title);
TextView tvMessage = findViewById(R.id.tv_message);
Button btnPositive = findViewById(R.id.btn_positive);
Button btnNegative = findViewById(R.id.btn_negative);
@SuppressLint({"MissingInflatedId", "LocalSuppress"})
ConstraintLayout rootView =(ConstraintLayout) findViewById(R.id.root_view);
// 设置文本
tvTitle.setText(title);
// 根据是否有spannableMessage来设置不同的文本
if (spannableMessage != null) {
tvMessage.setText(spannableMessage);
} else {
tvMessage.setText(message);
}
btnPositive.setText(positiveButtonText);
btnNegative.setText(negativeButtonText);
// 设置点击监听器
btnPositive.setOnClickListener(v -> {
isCountdownCancelled = true; // 标记倒计时被取消
if (countDownTimer != null) {
countDownTimer.cancel(); // 取消倒计时
countDownTimer = null;
}
if (positiveButtonClickListener != null) {
positiveButtonClickListener.onClick(v);
}
dismiss();
});
// 判断是否需要显示取消按钮
if (negativeButtonText != null && !negativeButtonText.isEmpty()) {
btnNegative.setText(negativeButtonText);
btnNegative.setVisibility(View.VISIBLE);
btnNegative.setOnClickListener(v -> {
if (negativeButtonClickListener != null) {
negativeButtonClickListener.onClick(v);
}
dismiss();
});
} else {
// 隐藏取消按钮
btnNegative.setVisibility(View.GONE);
}
//判断是否需要显示确认按钮
if (positiveButtonText != null && !positiveButtonText.isEmpty()) {
btnPositive.setText(positiveButtonText);
btnPositive.setVisibility(View.VISIBLE);
}else {
// 隐藏确认按钮
btnPositive.setVisibility(View.GONE);
}
// 倒计时逻辑
if (isCountdownEnabled && countdownSeconds > 0) {
startCountdown(btnNegative);
}
ThemeableDrawableUtils.setThemeableRoundedBackground(btnPositive, ColorManager.getInstance().getPrimaryColorInt(), 53);
btnPositive.setTextColor(ColorManager.getInstance().getButtonColorInt());
}
private boolean isCountdownCancelled = false; // 添加标志位
private void startCountdown(Button btnNegative) {
countDownTimer = new CountDownTimer(countdownSeconds * 1000L, 1000) {
@Override
public void onTick(long millisUntilFinished) {
int secondsLeft = (int) (millisUntilFinished / 1000);
btnNegative.setText(negativeButtonText + " (" + secondsLeft + "s)");
}
@Override
public void onFinish() {
// 检查是否被主动取消
if (!isCountdownCancelled) {
btnNegative.setText(negativeButtonText);
if (negativeButtonClickListener != null) {
negativeButtonClickListener.onClick(btnNegative); // 自动触发取消
}
dismiss();
}
}
}.start();
}
@Override
public void dismiss() {
if (countDownTimer != null) {
countDownTimer.cancel(); // 取消倒计时
countDownTimer = null;
}
super.dismiss();
}
}