AlertDialog.java
8.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* Copyright (c) Wondertek Technologies Co., Ltd. 2019-2022. All rights reserved.
*/
package com.wd.common.dialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import androidx.core.content.ContextCompat;
import com.wd.fastcoding.base.R;
import com.wd.foundation.wdkitcore.tools.ResUtils;
/**
* @author yangchenggong
* @date 2022/6/28 15:29
*/
public class AlertDialog {
private Context context;
private Dialog dialog;
private LinearLayout lLayout_bg;
private TextView txt_title;
private TextView txt_msg;
private TextView btn_neg;
private TextView btn_pos;
private ImageView img_line;
private ImageView horizontal_line;
private Display display;
private boolean showTitle = false;
private boolean showMsg = false;
private boolean showPosBtn = false;
private boolean showNegBtn = false;
public AlertDialog(Context context) {
this.context = context;
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
display = windowManager.getDefaultDisplay();
}
public AlertDialog builder() {
// 获取Dialog布局
View view = LayoutInflater.from(context).inflate(R.layout.dialog_layout, null);
// 获取自定义Dialog布局中的控件
lLayout_bg = (LinearLayout) view.findViewById(R.id.lLayout_bg);
txt_title = (TextView) view.findViewById(R.id.txt_title);
txt_title.setVisibility(View.GONE);
txt_msg = (TextView) view.findViewById(R.id.txt_msg);
txt_msg.setVisibility(View.GONE);
btn_neg = view.findViewById(R.id.btn_neg);
btn_neg.setVisibility(View.GONE);
btn_pos = view.findViewById(R.id.btn_pos);
btn_pos.setVisibility(View.GONE);
horizontal_line = view.findViewById(R.id.horizontal_line);
img_line = (ImageView) view.findViewById(R.id.img_line);
img_line.setVisibility(View.GONE);
// 定义Dialog布局和参数
dialog = new Dialog(context, R.style.AlertDialogStyle);
dialog.setContentView(view);
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.dimAmount = 0.3f;
// lp.y = 250;
dialog.getWindow().setAttributes(lp);
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
// 调整dialog背景大小
lLayout_bg.setLayoutParams(
new FrameLayout.LayoutParams((int) (display.getWidth() * 0.75), LayoutParams.WRAP_CONTENT));
//设置按压效果
setButtonSelect();
return this;
}
public AlertDialog setNightKeep() {
if(lLayout_bg != null){
lLayout_bg.setBackground(ContextCompat.getDrawable(context,R.drawable.alert_bg_keep));
txt_title.setTextColor(ContextCompat.getColor(context,R.color.res_color_general_333333_keep));
txt_msg.setTextColor(ContextCompat.getColor(context,R.color.res_color_common_C3_keep));
horizontal_line.setBackground(ContextCompat.getDrawable(context,R.color.res_color_common_C7_keep));
img_line.setBackground(ContextCompat.getDrawable(context,R.color.res_color_common_C7_keep));
btn_pos.setTextColor(ContextCompat.getColor(context,R.color.res_color_general_333333_keep));
}
return this;
}
public AlertDialog setTitle(String title) {
showTitle = true;
txt_title.setText(TextUtils.isEmpty(title)?"Title":title);
return this;
}
public AlertDialog setMsg(String msg) {
showMsg = true;
txt_msg.setText(TextUtils.isEmpty(msg)?"Msg":msg);
return this;
}
public AlertDialog setCancelable(boolean cancel) {
dialog.setCancelable(cancel);
return this;
}
public AlertDialog setPositiveButton(String text, final OnClickListener listener) {
showPosBtn = true;
btn_pos.setText(TextUtils.isEmpty(text)? ResUtils.getString(R.string.yes_btn) :text);
btn_pos.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
if(listener != null){
listener.onClick(v);
}
}
});
return this;
}
public AlertDialog setNegativeButton(String text, final OnClickListener listener) {
showNegBtn = true;
btn_neg.setText(TextUtils.isEmpty(text)?ResUtils.getString(R.string.res_cancel) :text);
btn_neg.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(listener != null){
listener.onClick(v);
}
dialog.dismiss();
}
});
return this;
}
private void setLayout() {
if (!showTitle && !showMsg) {
txt_title.setText(ResUtils.getString(R.string.tips));
txt_title.setVisibility(View.VISIBLE);
}
if (showTitle) {
txt_title.setVisibility(View.VISIBLE);
}
if (showMsg) {
txt_msg.setVisibility(View.VISIBLE);
}
if (!showPosBtn && !showNegBtn) {
btn_pos.setText(ResUtils.getString(R.string.yes_btn));
btn_pos.setVisibility(View.VISIBLE);
btn_pos.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
if (showPosBtn && showNegBtn) {
btn_pos.setVisibility(View.VISIBLE);
btn_neg.setVisibility(View.VISIBLE);
img_line.setVisibility(View.VISIBLE);
}
if (showPosBtn && !showNegBtn) {
btn_pos.setVisibility(View.VISIBLE);
}
if (!showPosBtn && showNegBtn) {
btn_neg.setVisibility(View.VISIBLE);
}
}
public void show() {
if (dialog != null && !dialog.isShowing()) {
setLayout();
dialog.show();
}
}
/**
* 设置变粗
*/
private void setButtonSelect(){
btn_pos.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
// When the user clicks the Button
case MotionEvent.ACTION_DOWN:
btn_pos.setTypeface(Typeface.DEFAULT_BOLD);
break;
// When the user releases the Button
case MotionEvent.ACTION_UP:
btn_pos.setTypeface(Typeface.DEFAULT);
break;
default:
break;
}
return false;
}
});
btn_neg.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
// When the user clicks the Button
case MotionEvent.ACTION_DOWN:
btn_neg.setTypeface(Typeface.DEFAULT_BOLD);
break;
// When the user releases the Button
case MotionEvent.ACTION_UP:
btn_neg.setTypeface(Typeface.DEFAULT);
break;
default:
break;
}
return false;
}
});
}
/**
* 是否在展示
* @return
*/
public boolean isShow(){
if(dialog != null){
return dialog.isShowing();
}
return false;
}
/**
* 隐藏弹框
*/
public void dismissDialog(){
if(dialog != null && dialog.isShowing()){
dialog.dismiss();
}
}
}