public class DialogHelper {
/***
* 获取一个dialog对象,进行相关操作
*
* @param context
* @return
*/
public static AlertDialog.Builder getDialog(Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
return builder;
}
public static AlertDialog.Builder getMessageDialog(Context context, String message,
DialogInterface.OnClickListener onClickListener) {
AlertDialog.Builder builder = getDialog(context);
builder.setMessage(message);
builder.setPositiveButton("确定", onClickListener);
return builder;
}
/**
* 获取一个信息对话框
*
* @param context
* @param message
* @return
*/
public static AlertDialog.Builder getMessageDialog(Context context, String message) {
return getMessageDialog(context, message, null);
}
public static AlertDialog.Builder getSelectDialog(Context context, String title, String[] arrays, DialogInterface.OnClickListener onClickListener) {
AlertDialog.Builder builder = getDialog(context);
builder.setItems(arrays, onClickListener);
if (!TextUtils.isEmpty(title)) {
builder.setTitle(title);
}
builder.setPositiveButton("取消", null);
return builder;
}
/**
* 选择对话框
*
* @param context
* @param arrays
* @param onClickListener
* @return
*/
public static AlertDialog.Builder getSelectDialog(Context context, String[] arrays, DialogInterface.OnClickListener onClickListener) {
return getSelectDialog(context, "", arrays, onClickListener);
}
/**
* 可设置内容、确定监听对话框
*
* @param context
* @param message
* @param onClickListener
* @return
*/
public static AlertDialog.Builder getConfirmDialog(Context context, String message, DialogInterface.OnClickListener onClickListener) {
AlertDialog.Builder builder = getDialog(context);
builder.setMessage(Html.fromHtml(message));
builder.setPositiveButton("确定", onClickListener);
builder.setNegativeButton("取消", null);
return builder;
}
/**
* 可设置内容、两个按钮监听对话框
*
* @param context
* @param message
* @param onOkClickListener
* @param onCancleClickListener
* @return
*/
public static AlertDialog.Builder getConfirmDialog(Context context, String message, DialogInterface.OnClickListener onOkClickListener, DialogInterface.OnClickListener onCancleClickListener) {
AlertDialog.Builder builder = getDialog(context);
builder.setMessage(message);
builder.setPositiveButton("确定", onOkClickListener);
builder.setNegativeButton("取消", onCancleClickListener);
return builder;
}
/**
* 可设置自定义布局、两个按钮监听对话框
*
* @param context
* @param view
* @param onOkClickListener
* @param onCancleClickListener
* @return
*/
public static AlertDialog.Builder getConfirmDialoglayout(Context context,
String title,
View view, DialogInterface.OnClickListener onOkClickListener, DialogInterface.OnClickListener onCancleClickListener) {
AlertDialog.Builder builder = getDialog(context);
if (!TextUtils.isEmpty(title)) {
builder.setTitle(title);
}
builder.setView(view);
builder.setPositiveButton("确定", onOkClickListener);
builder.setNegativeButton("取消", onCancleClickListener);
return builder;
}
/**
* 可设置自定义布局、两个按钮监听对话框
*
* @param context
* @param view
* @param onOkClickListener
* @param onCancleClickListener
* @return
*/
public static AlertDialog.Builder getConfirmDialoglayoutCancle(Context context,
String title,
View view,
DialogInterface.OnClickListener onCancleClickListener) {
AlertDialog.Builder builder = getDialog(context);
if (!TextUtils.isEmpty(title)) {
builder.setTitle(title);
}
builder.setView(view);
builder.setNegativeButton("取消", onCancleClickListener);
return builder;
}
/**
* 可设置标题、内容、按钮文字,监听的对话框
*
* @param context
* @param title
* @param message
* @param okString
* @param cancleString
* @param onOkClickListener
* @param onCancleClickListener
* @return
*/
public static AlertDialog.Builder getConfirmDialog(Context context,
String title,
String message,
String okString,
String cancleString,
DialogInterface.OnClickListener onOkClickListener,
DialogInterface.OnClickListener onCancleClickListener) {
AlertDialog.Builder builder = getDialog(context);
if (!TextUtils.isEmpty(title)) {
builder.setTitle(title);
}
builder.setMessage(message);
builder.setPositiveButton(okString, onOkClickListener);
builder.setNegativeButton(cancleString, onCancleClickListener);
return builder;
}
/**
* 创建可自定义对话框内容和按钮文字的对话框
*
* @param context
* @param message 对话框内容
* @param okString 确定按钮文字
* @param cancleString 取消按钮文字
* @param onOkClickListener 确定监听
* @param onCancleClickListener 取消监听
* @return
*/
public static AlertDialog.Builder getConfirmDialog(Context context,
String message,
String okString,
String cancleString,
DialogInterface.OnClickListener onOkClickListener,
DialogInterface.OnClickListener onCancleClickListener) {
return getConfirmDialog(context, "", message, okString, cancleString, onOkClickListener, onCancleClickListener);
}
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;
}
/**
* 单选对话框
*
* @param context
* @param arrays 可供选择的数据
* @param selectIndex 默认选中的索引
* @param onClickListener
* @return
*/
public static AlertDialog.Builder getSingleChoiceDialog(Context context,
String[] arrays,
int selectIndex,
DialogInterface.OnClickListener onClickListener) {
return getSingleChoiceDialog(context, "", arrays,
selectIndex, onClickListener);
}
}
本文转自http://www.jianshu.com/p/9f3ffdea4c41,在它的基础上添加了一些对话框类型