Android中对话框的工具类,里面总结了比较好用的方法,直接调用即可
public class DialogUtils {
private DialogUtils() {
}
private static AlertDialog mTipsDialog;
private static ProgressDialog mProgressDialog = null;
private static final String DIALOG_UTIL_TAG = "DialogUtils ";
// 弹窗提示信息
public static void showHintDialog(Context context, String title, String tips) {
LinearLayout layout_dialog = new LinearLayout(context);
layout_dialog.setOrientation(LinearLayout.VERTICAL);
// 标题
TextView tv_title = new TextView(context);
LinearLayout.LayoutParams para_tv = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
tv_title.setLayoutParams(para_tv);
tv_title.setGravity(Gravity.CENTER);
tv_title.setTextSize(15);
tv_title.setPadding(ScaleUtils.dip2px(context, 10), ScaleUtils.dip2px(context, 10), ScaleUtils.dip2px(context, 10), ScaleUtils.dip2px(context, 10));
layout_dialog.setGravity(Gravity.CENTER_VERTICAL);
// 间隔线
int mWidth = DisplaySizeUtils.getDisplaySizeInfo(context).x;
TextView mDiv_Line_tip = new TextView(context);
mDiv_Line_tip.setWidth(mWidth - ScaleUtils.dip2px(context, 10));
mDiv_Line_tip.setHeight(ScaleUtils.dip2px(context, 0.75f));
mDiv_Line_tip.setBackgroundColor(Color.parseColor("#D1D3D5"));
tv_title.setText(title);
if (title != null) {
layout_dialog.addView(tv_title);
layout_dialog.addView(mDiv_Line_tip);
}
// 提示信息
TextView tv_content = new TextView(context);
tv_content.setLayoutParams(para_tv);
tv_content.setTextSize(13);
tv_content.setPadding(ScaleUtils.dip2px(context, 10), ScaleUtils.dip2px(context, 10), ScaleUtils.dip2px(context, 10), ScaleUtils.dip2px(context, 10));
tv_content.setText(tips);
layout_dialog.addView(tv_content);
layout_dialog.setGravity(Gravity.CENTER_VERTICAL);
//确认按钮
TextView tv_conferm = new TextView(context);
LinearLayout.LayoutParams para_tv1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, ScaleUtils.dip2px(context, 50));
tv_conferm.setText("知道了");
tv_conferm.setLayoutParams(para_tv1);
tv_conferm.setTextSize(17);
tv_conferm.setTextColor(Color.WHITE);
tv_conferm.setBackgroundColor(Color.parseColor("#398DEE"));
tv_conferm.setGravity(Gravity.CENTER);
tv_conferm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
mTipsDialog.dismiss();
}
});
layout_dialog.addView(tv_conferm);
mTipsDialog = new AlertDialog.Builder(context).setView(layout_dialog).create();
mTipsDialog.setCancelable(false);
mTipsDialog.setCanceledOnTouchOutside(false);
mTipsDialog.show();
}
/**
* 显示Dialog
* @param context 上下文
* @param isCCancel 是否点击Dialog边缘取消Dialog
*/
public static void showProgressDialog(Context context, boolean isCCancel) {
if (null == mProgressDialog) {
mProgressDialog = new ProgressDialog(context);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setIndeterminate(false); // 设置ProgressDialog 的进度条是否不明确
mProgressDialog.setMessage("正在加载...");
mProgressDialog.setCancelable(isCCancel); // 设置ProgressDialog是否可以按退回按键取消
mProgressDialog.setCanceledOnTouchOutside(isCCancel);//设置ProgressDialog是否可以点击非窗口区域取消
}
mProgressDialog.show();
} /**
* 显示Dialog
* @param context 上下文
* @param msg 弹框要显示的文本信息
* @param isCCancel 是否点击Dialog边缘取消Dialog
*/
public static void showProgressDialog(Context context,String msg, boolean isCCancel) {
if (null == mProgressDialog) {
mProgressDialog = new ProgressDialog(context);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setIndeterminate(false); // 设置ProgressDialog 的进度条是否不明确
mProgressDialog.setMessage(msg);
mProgressDialog.setCancelable(isCCancel); // 设置ProgressDialog是否可以按退回按键取消
mProgressDialog.setCanceledOnTouchOutside(isCCancel);//设置ProgressDialog是否可以点击非窗口区域取消
}
mProgressDialog.show();
}
/**
* 隐藏Dialog
* 由于统一管理Dialog,注意Activity/Fragment生命周期对它的影响
*/
public static void hideProgressDialog() {
if (null != mProgressDialog) {
if (mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
mProgressDialog = null;
}
}
}