-
Notifications
You must be signed in to change notification settings - Fork 0
/
DialogTool.java
188 lines (178 loc) · 8.94 KB
/
DialogTool.java
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
package com.ruptech.ai.utils;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.View;
/**
* Created by ls_gao on 2015/2/26.
*/
public class DialogTool {
public static final int NO_ICON = -1; //无图标
/*
* 创建消息对话框
* @param context 上下文 必填
* @param iconId 图标,如:R.drawable.icon 或 DialogTool.NO_ICON 必填
* @param title 标题 必填
* @param message 显示内容 必填
* @param btnName 按钮名称 必填
* @param listener 监听器,需实现android.content.DialogInterface.OnClickListener接口 必填
* @return
*/
public static Dialog createMessageDialog(Context context, String title, String message, String btnName, OnClickListener listener, int iconId) {
Dialog dialog = null;
AlertDialog.Builder builder = new AlertDialog.Builder(context);
if (iconId != NO_ICON) {
//设置对话框图标
builder.setIcon(iconId);
}
//设置对话框标题
builder.setTitle(title);
//设置对话框消息
builder.setMessage(message);
//设置按钮
builder.setPositiveButton(btnName, listener);
//创建一个消息对话框
dialog = builder.create();
return dialog;
}
/*
* 创建警示(确认、取消)对话框
* @param context 上下文 必填
* @param iconId 图标,如 R.drawable.icon 或 DialogTool.NO_ICON 必填
* @param title 标题 必填
* @param message 显示内容 必填
* @param positiveBtnName 确定按钮名称 必填
* @param negativeBtnName 取消按钮名称 必填
* @param positiveBtnListener 监听器,需实现 android.content.DialogInterface.OnClickListener 接口 必填
* @param negativeBtnListener 监听器,需实现 android.content.DialogInterface.OnClickListener 接口 必填
* @return
*/
public static Dialog createConfirmDialog(Context context, String title, String message, String positiveBtnName, String negativeBtnName, OnClickListener positiveBtnListener, OnClickListener negativeBtnListener, int iconId) {
Dialog dialog = null;
AlertDialog.Builder builder = new AlertDialog.Builder(context);
if(iconId != NO_ICON){
//设置对话框图标
builder.setIcon(iconId);
}
//设置对话框标题
builder.setTitle(title);
//设置对话框消息
builder.setMessage(message);
//设置确定按钮
builder.setPositiveButton(positiveBtnName, positiveBtnListener);
//设置取消按钮
builder.setNegativeButton(negativeBtnName, negativeBtnListener);
//创建一个消息对话框
dialog = builder.create();
return dialog;
}
/*
* 创建单选对话框
* @param context 上下文 必填
* @param iconId 图标,如:R.drawable.icon 或 DialogTool.NO_ICON 必填 * @param title 标题 必填
* @param itemsString 选择项 必填
* @param positiveBtnName 确定按钮名称 必填 * @param negativeBtnName 取消按钮名称 必填
* @param positiveBtnListener 监听器,需实现android.content.DialogInterface.OnClickListener接口 必填 * @param negativeBtnListener 监听器,需实现android.content.DialogInterface.OnClickListener接口 必填 * @param itemClickListener 监听器,需实现android.content.DialogInterface.OnClickListener接口 必填 * @return */
public static Dialog createSingleChoiceDialog(Context context, String title, String[] itemsString, String positiveBtnName, String negativeBtnName, OnClickListener positiveBtnListener, OnClickListener negativeBtnListener, OnClickListener itemClickListener, int iconId) {
Dialog dialog = null;
AlertDialog.Builder builder = new AlertDialog.Builder(context);
if (iconId != NO_ICON) {
//设置对话框图标
builder.setIcon(iconId);
}
//设置对话框标题
builder.setTitle(title);
//设置单选选项, 参数0: 默认第一个单选按钮被选中
builder.setSingleChoiceItems(itemsString, 0, itemClickListener); //设置确定按钮
builder.setPositiveButton(positiveBtnName, positiveBtnListener); //设置确定按钮
builder.setNegativeButton(negativeBtnName, negativeBtnListener);
//创建一个消息对话框
dialog = builder.create();
return dialog;
}
/*
* 创建复选对话框
* @param context 上下文 必填
* @param iconId 图标,如:R.drawable.icon 或 DialogTool.NO_ICON 必填 * @param title 标题 必填
* @param itemsString 选择项 必填
* @param positiveBtnName 确定按钮名称 必填 * @param negativeBtnName 取消按钮名称 必填
* @param positiveBtnListener 监听器,需实现android.content.DialogInterface.OnClickListener接口 必填
* @param negativeBtnListener 监听器,需实现android.content.DialogInterface.OnClickListener接口 必填
* @param itemClickListener 监听器,需实现android.content.DialogInterface.OnMultiChoiceClickListener;接口 必填
* @return
*/
public static Dialog createMultiChoiceDialog(Context context, String title, String[] itemsString, String positiveBtnName, String negativeBtnName, OnClickListener positiveBtnListener,
OnClickListener negativeBtnListener, DialogInterface.OnMultiChoiceClickListener itemClickListener, int iconId) {
Dialog dialog = null;
AlertDialog.Builder builder = new AlertDialog.Builder(context);
if (iconId != NO_ICON) {
//设置对话框图标
builder.setIcon(iconId);
}
//设置对话框标题
builder.setTitle(title);
//设置选项
builder.setMultiChoiceItems(itemsString, null, itemClickListener); //设置确定按钮
builder.setPositiveButton(positiveBtnName, positiveBtnListener); //设置确定按钮
builder.setNegativeButton(negativeBtnName, negativeBtnListener); //创建一个消息对话框
dialog = builder.create();
return dialog;
}
/*
* 创建列表对话框
* @param context 上下文 必填
* @param iconId 图标,如:R.drawable.icon 或 DialogTool.NO_ICON 必填
* @param title 标题 必填
* @param itemsString 列表项 必填
* @param negativeBtnName 取消按钮名称 必填
* @param negativeBtnListener 监听器,需实现android.content.DialogInterface.OnClickListener接口 必填
* @return
*/
public static Dialog createListDialog(Context context, String title, String[] itemsString, String negativeBtnName, OnClickListener negativeBtnListener, OnClickListener itemClickListener, int iconId) {
Dialog dialog = null;
AlertDialog.Builder builder = new AlertDialog.Builder(context);
if (iconId != NO_ICON) {
//设置对话框图标
builder.setIcon(iconId);
}
//设置对话框标题
builder.setTitle(title);
//设置列表选项
builder.setItems(itemsString, itemClickListener);
//设置确定按钮
builder.setNegativeButton(negativeBtnName, negativeBtnListener);
//创建一个消息对话框
dialog = builder.create();
return dialog;
}
/*
* 创建自定义(含确认、取消)对话框 *
* @param context 上下文 必填
* @param iconId 图标,如:R.drawable.icon 或 DialogTool.NO_ICON 必填 * @param title 标题 必填
* @param positiveBtnName 确定按钮名称 必填 * @param negativeBtnName 取消按钮名称 必填
* @param positiveBtnListener 监听器,需实现android.content.DialogInterface.OnClickListener接口 必填
* @param negativeBtnListener 监听器,需实现android.content.DialogInterface.OnClickListener接口 必填
* @param view 对话框中自定义视图 必填
* @return
*/
public static Dialog createRandomDialog(Context context, String title, String positiveBtnName, String negativeBtnName, OnClickListener positiveBtnListener, OnClickListener negativeBtnListener,View view, int iconId) {
Dialog dialog = null;
AlertDialog.Builder builder = new AlertDialog.Builder(context);
if (iconId != NO_ICON) {
//设置对话框图标
builder.setIcon(iconId);
}
//设置对话框标题
builder.setTitle(title);
builder.setView(view);
//设置确定按钮
builder.setPositiveButton(positiveBtnName, positiveBtnListener);
//设置确定按钮
builder.setNegativeButton(negativeBtnName, negativeBtnListener);
//创建一个消息对话框
dialog = builder.create();
return dialog;
}
}