Fork me on GitHub

Android Dialog 帮助类

快速生成Dialog的封装类,来自开源中国客户端

代码

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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;


import static android.view.View.OVER_SCROLL_NEVER;

/**
* 通用的对话框
* Created by haibin
* on 2016/11/2.
*/
@SuppressWarnings("all")
public final class DialogHelper {
public static AlertDialog.Builder getDialog(Context context) {
return new AlertDialog.Builder(context);
}

/**
* 获取一个普通的消息对话框,没有取消按钮
*/
public static AlertDialog.Builder getMessageDialog(
Context context,
String title,
String message,
boolean cancelable) {
return getDialog(context)
.setCancelable(cancelable)
.setTitle(title)
.setMessage(message)
.setPositiveButton("确定", null);
}

/**
* 获取一个普通的消息对话框,没有取消按钮
*/
public static AlertDialog.Builder getMessageDialog(
Context context,
String title,
String message) {
return getMessageDialog(context, title, message, true);
}

/**
* 获取一个普通的消息对话框,没有取消按钮
*/
public static AlertDialog.Builder getMessageDialog(Context context, String message) {
return getMessageDialog(context, "", message, true);
}

/**
* 获取一个普通的消息对话框,没有取消按钮
*/
public static AlertDialog.Builder getMessageDialog(
Context context,
String title,
String message,
String positiveText) {
return getDialog(context)
.setCancelable(true)
.setTitle(title)
.setMessage(message)
.setPositiveButton(positiveText, null);
}

public static AlertDialog.Builder getConfirmDialog(Context context,
String title,
View view,
DialogInterface.OnClickListener positiveListener) {
return getDialog(context)
.setTitle(title)
.setView(view)
.setPositiveButton("确定", positiveListener)
.setNegativeButton("取消", null);
}

/**
* 获取一个验证对话框
*/
public static AlertDialog.Builder getConfirmDialog(
Context context,
String title,
String message,
String positiveText,
String negativeText,
boolean cancelable,
DialogInterface.OnClickListener positiveListener,
DialogInterface.OnClickListener negativeListener) {
return getDialog(context)
.setCancelable(cancelable)
.setTitle(title)
.setMessage(message)
.setPositiveButton(positiveText, positiveListener)
.setNegativeButton(negativeText, negativeListener);
}

/**
* 获取一个验证对话框
*/
public static AlertDialog.Builder getConfirmDialog(
Context context, String message,
DialogInterface.OnClickListener positiveListener,
DialogInterface.OnClickListener negativeListener) {
return getDialog(context)
.setMessage(message)
.setPositiveButton("确定", positiveListener)
.setNegativeButton("取消", negativeListener);
}

public static AlertDialog.Builder getSingleChoiceDialog(
Context context,
String title,
String[] arrays,
int selectIndex,
DialogInterface.OnClickListener onClickListener) {
AlertDialog.Builder builder = getDialog(context);
builder.setSingleChoiceItems(arrays, selectIndex, onClickListener);
if (!TextUtils.isEmpty(title)) {
builder.setTitle(title);
}
builder.setNegativeButton("取消", null);
return builder;
}


/**
* 获取一个验证对话框,没有点击事件
*/
public static AlertDialog.Builder getConfirmDialog(
Context context,
String title,
String message,
String positiveText,
String negativeText,
boolean cancelable,
DialogInterface.OnClickListener positiveListener) {
return getConfirmDialog(
context, title, message, positiveText,
negativeText, cancelable, positiveListener, null);
}

/**
* 获取一个验证对话框,没有点击事件
*/
public static AlertDialog.Builder getConfirmDialog(
Context context,
String title,
String message,
String positiveText,
String negativeText,
DialogInterface.OnClickListener positiveListener) {
return getConfirmDialog(
context, title, message, positiveText, negativeText, true, positiveListener, null);
}


/**
* 获取一个验证对话框,没有点击事件
*/
public static AlertDialog.Builder getConfirmDialog(
Context context,
String title,
String message,
String positiveText,
String negativeText,
boolean cancelable) {
return getConfirmDialog(
context, title, message, positiveText, negativeText, cancelable, null, null);
}

/**
* 获取一个验证对话框,没有点击事件
*/
public static AlertDialog.Builder getConfirmDialog(
Context context,
String message,
String positiveText,
String negativeText,
boolean cancelable) {
return getConfirmDialog(context, "", message, positiveText, negativeText
, cancelable, null, null);
}

/**
* 获取一个验证对话框,没有点击事件,取消、确定
*/
public static AlertDialog.Builder getConfirmDialog(
Context context,
String title,
String message,
boolean cancelable) {
return getConfirmDialog(context, title, message, "确定", "取消", cancelable, null, null);
}

/**
* 获取一个验证对话框,没有点击事件,取消、确定
*/
public static AlertDialog.Builder getConfirmDialog(
Context context,
String message,
boolean cancelable,
DialogInterface.OnClickListener positiveListener) {
return getConfirmDialog(context, "", message, "确定", "取消", cancelable, positiveListener, null);
}

/**
* 获取一个验证对话框,没有点击事件,取消、确定
*/
public static AlertDialog.Builder getConfirmDialog(
Context context,
String message,
DialogInterface.OnClickListener positiveListener) {
return getConfirmDialog(context, "", message, "确定", "取消", positiveListener);
}

/**
* 获取一个验证对话框,没有点击事件,取消、确定
*/
public static AlertDialog.Builder getConfirmDialog(
Context context,
String title,
String message) {
return getConfirmDialog(context, title, message, "确定", "取消", false, null, null);
}

/**
* 获取一个输入对话框
*/
public static AlertDialog.Builder getInputDialog(
Context context,
String title,
AppCompatEditText editText,
String positiveText,
String negativeText,
boolean cancelable,
DialogInterface.OnClickListener positiveListener,
DialogInterface.OnClickListener negativeListener) {
return getDialog(context)
.setCancelable(cancelable)
.setTitle(title)
.setView(editText)
.setPositiveButton(positiveText, positiveListener)
.setNegativeButton(negativeText, negativeListener);
}

/**
* 获取一个输入对话框
*/
public static AlertDialog.Builder getInputDialog(
Context context, String title,
AppCompatEditText editText,
String positiveText,
String negativeText,
boolean cancelable,
DialogInterface.OnClickListener positiveListener) {
return getInputDialog(
context,
title,
editText,
positiveText,
negativeText,
cancelable,
positiveListener,
null);
}

/**
* 获取一个输入对话框
*/
public static AlertDialog.Builder getInputDialog(
Context context,
String title,
AppCompatEditText editText,
boolean cancelable,
DialogInterface.OnClickListener positiveListener) {
return getInputDialog(context, title, editText, "确定", "取消"
, cancelable, positiveListener, null);
}

/**
* 获取一个输入对话框
*/
public static AlertDialog.Builder getInputDialog(
Context context, String title, AppCompatEditText editText, String positiveText,
boolean cancelable,
DialogInterface.OnClickListener positiveListener,
DialogInterface.OnClickListener negativeListener) {
return getInputDialog(
context, title, editText, positiveText, "取消", cancelable
, positiveListener, negativeListener);
}

/**
* 获取一个输入对话框
*/
public static AlertDialog.Builder getInputDialog(
Context context, String title, AppCompatEditText editText,
boolean cancelable,
DialogInterface.OnClickListener positiveListener,
DialogInterface.OnClickListener negativeListener) {
return getInputDialog(
context, title, editText, "确定", "取消", cancelable
, positiveListener, negativeListener);
}


/**
* 获取一个等待对话框
*/
public static ProgressDialog getProgressDialog(Context context) {
return new ProgressDialog(context);
}

/**
* 获取一个等待对话框
*/
public static ProgressDialog getProgressDialog(Context context, boolean cancelable) {
ProgressDialog dialog = getProgressDialog(context);
dialog.setCancelable(cancelable);
return dialog;
}

/**
* 获取一个等待对话框
*/
public static ProgressDialog getProgressDialog(Context context, String message) {
ProgressDialog dialog = getProgressDialog(context);
dialog.setMessage(message);
return dialog;
}

/**
* 获取一个等待对话框
*/
public static ProgressDialog getProgressDialog(
Context context, String title, String message, boolean cancelable) {
ProgressDialog dialog = getProgressDialog(context);
dialog.setCancelable(cancelable);
dialog.setTitle(title);
dialog.setMessage(message);
return dialog;
}

/**
* 获取一个等待对话框
*/
public static ProgressDialog getProgressDialog(
Context context, String message, boolean cancelable) {
ProgressDialog dialog = getProgressDialog(context);
dialog.setCancelable(cancelable);
dialog.setMessage(message);
return dialog;
}

public static AlertDialog.Builder getSelectDialog(
Context context, String title, String[] items,
String positiveText,
DialogInterface.OnClickListener itemListener) {
return getDialog(context)
.setTitle(title)
.setItems(items, itemListener)
.setPositiveButton(positiveText, null);

}

public static AlertDialog.Builder getSelectDialog(
Context context, String[] items,
String positiveText,
DialogInterface.OnClickListener itemListener) {
return getDialog(context)
.setItems(items, itemListener)
.setPositiveButton(positiveText, null);

}

public static AlertDialog.Builder getSelectDialog(Context context, View view, String positiveText,
DialogInterface.OnClickListener itemListener) {
return getDialog(context)
.setView(view)
.setPositiveButton(positiveText, null);
}

// public static AlertDialog.Builder getRecyclerViewDialog(Context context, BaseRecyclerAdapter.OnItemClickListener listener) {
// RecyclerView recyclerView = new RecyclerView(context);
// RecyclerView.LayoutParams params =
// new GridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// recyclerView.setPadding(Util.dipTopx(context, 16), Util.dipTopx(context, 16),
// Util.dipTopx(context, 16), Util.dipTopx(context, 16));
// recyclerView.setLayoutParams(params);
// recyclerView.setLayoutManager(new GridLayoutManager(context, 3));
// CommentItemAdapter adapter = new CommentItemAdapter(context);
// adapter.setOnItemClickListener(listener);
// recyclerView.setAdapter(adapter);
// recyclerView.setOverScrollMode(OVER_SCROLL_NEVER);
// return getDialog(context)
// .setView(recyclerView)
// .setPositiveButton(null, null);
// }
}

使用

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
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatEditText;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mContext = MainActivity.this;
findViewById(R.id.btn_message_dialog).setOnClickListener(this);
findViewById(R.id.btn_confirm_dialog).setOnClickListener(this);
findViewById(R.id.btn_input_dialog).setOnClickListener(this);
findViewById(R.id.btn_progress_dialog).setOnClickListener(this);
findViewById(R.id.btn_select_dialog).setOnClickListener(this);


}

@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_message_dialog:
DialogHelper.getMessageDialog(mContext,"This is a msg dialog").show();
break;
case R.id.btn_confirm_dialog:
DialogHelper.getConfirmDialog(mContext,"This is Title","This is a msg dialog").show();
break;
case R.id.btn_input_dialog:
AppCompatEditText editText = new AppCompatEditText(mContext);
DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {

}
};
DialogHelper.getInputDialog(mContext,"This is Title",editText,false,listener).show();
break;
case R.id.btn_progress_dialog:
DialogHelper.getProgressDialog(mContext).show();
break;
case R.id.btn_select_dialog:
DialogHelper.getSelectDialog(mContext, "This is Title", new String[]{"item1", "item2"}, "positiveText",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {

}
}).show();
break;
default:;
}
}
}