|
| 1 | +package com.ichtj.basetools.network; |
| 2 | + |
| 3 | +import android.app.AlertDialog; |
| 4 | +import android.content.Context; |
| 5 | +import android.graphics.Color; |
| 6 | +import android.graphics.Typeface; |
| 7 | +import android.graphics.drawable.ColorDrawable; |
| 8 | +import android.text.TextUtils; |
| 9 | +import android.view.Gravity; |
| 10 | +import android.view.View; |
| 11 | +import android.view.ViewGroup; |
| 12 | +import android.view.Window; |
| 13 | +import android.view.WindowManager; |
| 14 | +import android.widget.Button; |
| 15 | +import android.widget.EditText; |
| 16 | +import android.widget.LinearLayout; |
| 17 | +import android.widget.TextView; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import android.app.AlertDialog; |
| 23 | +import android.content.Context; |
| 24 | +import android.graphics.Typeface; |
| 25 | +import android.util.DisplayMetrics; |
| 26 | +import android.util.TypedValue; |
| 27 | +import android.view.Gravity; |
| 28 | +import android.view.View; |
| 29 | +import android.view.ViewGroup; |
| 30 | +import android.widget.*; |
| 31 | + |
| 32 | +import androidx.core.content.ContextCompat; |
| 33 | + |
| 34 | +import com.face_chtj.base_iotutils.DisplayUtils; |
| 35 | +import com.face_chtj.base_iotutils.RegularTools; |
| 36 | +import com.face_chtj.base_iotutils.ToastUtils; |
| 37 | +import com.ichtj.basetools.R; |
| 38 | + |
| 39 | +import java.util.ArrayList; |
| 40 | +import java.util.List; |
| 41 | + |
| 42 | +public class CustomDynamicDialog { |
| 43 | + private static int IP_COUNT=0; |
| 44 | + public interface OnConfirmListener { |
| 45 | + void onConfirm(List<String> inputs); |
| 46 | + } |
| 47 | + |
| 48 | + public static void showDialog(Context context,String titleStr, OnConfirmListener listener) { |
| 49 | + IP_COUNT=0; |
| 50 | + AlertDialog.Builder builder = new AlertDialog.Builder(context,R.style.CustomDialogTheme); |
| 51 | + // 根布局 |
| 52 | + LinearLayout rootLayout = new LinearLayout(context); |
| 53 | + rootLayout.setOrientation(LinearLayout.VERTICAL); |
| 54 | + rootLayout.setPadding(25, 25, 25, 25); |
| 55 | + |
| 56 | + // ===== 1. 标题 ===== |
| 57 | + TextView title = new TextView(context); |
| 58 | + title.setText(titleStr); |
| 59 | + title.setTextSize(18); |
| 60 | + title.setTypeface(null, Typeface.BOLD); |
| 61 | + title.setGravity(Gravity.START); |
| 62 | + LinearLayout.LayoutParams titleParams = new LinearLayout.LayoutParams( |
| 63 | + ViewGroup.LayoutParams.MATCH_PARENT, |
| 64 | + ViewGroup.LayoutParams.WRAP_CONTENT |
| 65 | + ); |
| 66 | + titleParams.bottomMargin = 30; |
| 67 | + rootLayout.addView(title, titleParams); |
| 68 | + |
| 69 | + // ===== 2. ScrollView + 输入框容器 ===== |
| 70 | + ScrollView scrollView = new ScrollView(context); |
| 71 | + LinearLayout.LayoutParams scrollParams = new LinearLayout.LayoutParams( |
| 72 | + ViewGroup.LayoutParams.MATCH_PARENT, |
| 73 | + ViewGroup.LayoutParams.WRAP_CONTENT |
| 74 | + ); |
| 75 | + |
| 76 | + // 设置最大高度(如 400dp) |
| 77 | + int maxHeightPx = (int) TypedValue.applyDimension( |
| 78 | + TypedValue.COMPLEX_UNIT_DIP, 400, |
| 79 | + context.getResources().getDisplayMetrics() |
| 80 | + ); |
| 81 | + scrollView.setLayoutParams(scrollParams); |
| 82 | + scrollView.setFillViewport(true); |
| 83 | + scrollView.setOverScrollMode(View.OVER_SCROLL_IF_CONTENT_SCROLLS); |
| 84 | + |
| 85 | + // 限制 ScrollView 的最大高度 |
| 86 | + scrollView.setVerticalScrollBarEnabled(true); |
| 87 | + scrollView.setLayoutParams(new LinearLayout.LayoutParams( |
| 88 | + ViewGroup.LayoutParams.MATCH_PARENT, |
| 89 | + maxHeightPx |
| 90 | + )); |
| 91 | + |
| 92 | + // 输入框容器 |
| 93 | + LinearLayout inputContainer = new LinearLayout(context); |
| 94 | + inputContainer.setOrientation(LinearLayout.VERTICAL); |
| 95 | + scrollView.addView(inputContainer); |
| 96 | + |
| 97 | + rootLayout.addView(scrollView); |
| 98 | + |
| 99 | + TextView tvCount = new TextView(context); |
| 100 | + tvCount.setTextSize(23); |
| 101 | + |
| 102 | + // 添加首个输入框 |
| 103 | + addInputRow(context,tvCount, inputContainer); |
| 104 | + tvCount.setText(context.getString(R.string.net_record_ip_count,IP_COUNT+"")); |
| 105 | + |
| 106 | + // ===== 3. 底部按钮区域 ===== |
| 107 | + LinearLayout buttonLayout = new LinearLayout(context); |
| 108 | + buttonLayout.setOrientation(LinearLayout.HORIZONTAL); |
| 109 | + buttonLayout.setGravity(Gravity.END); |
| 110 | + buttonLayout.setPadding(0, 40, 0, 0); |
| 111 | + |
| 112 | + Button cancelBtn = new Button(context); |
| 113 | + cancelBtn.setText(R.string.dialog_cancel); |
| 114 | + cancelBtn.setTextSize(23); |
| 115 | + |
| 116 | + Button confirmBtn = new Button(context); |
| 117 | + confirmBtn.setText(R.string.dialog_confirm); |
| 118 | + confirmBtn.setTextSize(23); |
| 119 | + |
| 120 | + buttonLayout.addView(tvCount); |
| 121 | + buttonLayout.addView(cancelBtn); |
| 122 | + buttonLayout.addView(confirmBtn); |
| 123 | + rootLayout.addView(buttonLayout); |
| 124 | + |
| 125 | + // ===== 4. 创建并显示 Dialog ===== |
| 126 | + AlertDialog dialog = builder.setView(rootLayout).create(); |
| 127 | + |
| 128 | + cancelBtn.setOnClickListener(v -> dialog.dismiss()); |
| 129 | + |
| 130 | + confirmBtn.setOnClickListener(v -> { |
| 131 | + List<String> inputs = new ArrayList<>(); |
| 132 | + for (int i = 0; i < inputContainer.getChildCount(); i++) { |
| 133 | + LinearLayout row = (LinearLayout) inputContainer.getChildAt(i); |
| 134 | + EditText et = (EditText) row.getChildAt(0); |
| 135 | + String etContent=et.getText().toString().trim(); |
| 136 | + if (!TextUtils.isEmpty(etContent)){ |
| 137 | + inputs.add(etContent); |
| 138 | + } |
| 139 | + } |
| 140 | + listener.onConfirm(inputs); |
| 141 | + dialog.dismiss(); |
| 142 | + }); |
| 143 | + dialog.show(); |
| 144 | + Window window = dialog.getWindow(); |
| 145 | + WindowManager.LayoutParams params = window.getAttributes(); |
| 146 | + int[] size = DisplayUtils.getScreenSize(context); |
| 147 | + params.width = (int) (size[0] / 2); |
| 148 | +// params.height = (int) (size[1] / 4); |
| 149 | + window.setAttributes(params); |
| 150 | + } |
| 151 | + |
| 152 | + // 添加一个输入框行(EditText + [+]按钮) |
| 153 | + private static void addInputRow(Context context,TextView tvCount, LinearLayout container) { |
| 154 | + LinearLayout row = new LinearLayout(context); |
| 155 | + row.setOrientation(LinearLayout.HORIZONTAL); |
| 156 | + row.setGravity(Gravity.CENTER_VERTICAL); |
| 157 | + row.setPadding(0, 20, 0, 0); |
| 158 | + |
| 159 | + EditText editText = new EditText(context); |
| 160 | + editText.setHint("请输入内容"); |
| 161 | + editText.setTextSize(28); |
| 162 | + editText.setLayoutParams(new LinearLayout.LayoutParams( |
| 163 | + 0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f |
| 164 | + )); |
| 165 | + |
| 166 | + Button addBtn = new Button(context); |
| 167 | + addBtn.setText("+"); |
| 168 | + addBtn.setTextSize(28); |
| 169 | + |
| 170 | + Button reduceBtn = new Button(context); |
| 171 | + reduceBtn.setText("-"); |
| 172 | + reduceBtn.setTextSize(28); |
| 173 | + |
| 174 | + addBtn.setOnClickListener(v -> { |
| 175 | + if (RegularTools.isValidIpOrUrl(editText.getText().toString())){ |
| 176 | + addBtn.setVisibility(View.GONE); // 隐藏当前按钮 |
| 177 | + reduceBtn.setVisibility(View.GONE); // 隐藏当前按钮 |
| 178 | + addInputRow(context,tvCount, container); // 添加新行 |
| 179 | + tvCount.setText(context.getString(R.string.net_record_ip_count,IP_COUNT+"")); |
| 180 | + }else{ |
| 181 | + ToastUtils.error("输入格式错误!"); |
| 182 | + } |
| 183 | + }); |
| 184 | + |
| 185 | + reduceBtn.setOnClickListener(v -> { |
| 186 | + if (IP_COUNT>1){ |
| 187 | + int index = container.indexOfChild(row); |
| 188 | + container.removeView(row); |
| 189 | + IP_COUNT--; |
| 190 | + tvCount.setText(context.getString(R.string.net_record_ip_count, IP_COUNT + "")); |
| 191 | + |
| 192 | + // 如果被删的是最后一行,恢复上一行的按钮 |
| 193 | + int childCount = container.getChildCount(); |
| 194 | + if (childCount > 0 && index - 1 >= 0 && index == childCount) { |
| 195 | + LinearLayout prevRow = (LinearLayout) container.getChildAt(index - 1); |
| 196 | + View prevAddBtn = prevRow.getChildAt(1); |
| 197 | + View prevReduceBtn = prevRow.getChildAt(2); |
| 198 | + prevAddBtn.setVisibility(View.VISIBLE); |
| 199 | + prevReduceBtn.setVisibility(View.VISIBLE); |
| 200 | + } |
| 201 | + } |
| 202 | + }); |
| 203 | + |
| 204 | + row.addView(editText); |
| 205 | + row.addView(addBtn); |
| 206 | + row.addView(reduceBtn); |
| 207 | + container.addView(row); |
| 208 | + IP_COUNT++; |
| 209 | + } |
| 210 | +} |
0 commit comments