当Android开发处理错误信息时,经常会以Dialog的形式显示错误信息,但是每次都new一个Dialog,很麻烦,也增加程序的开销,所以今天就分享一种自定义单例AlertDialog
public class AlertDialog {
private static AlertDialog alertDialog = null;
private Context context;
private Dialog dialog;
private LinearLayout lLayout_bg;
private TextView txt_title;
private TextView txt_msg;
private Button btn_neg;
private Button btn_pos;
private ImageView img_line;
private Display display;
private boolean showTitle = false;
private boolean showMsg = false;
private boolean showPosBtn = false;
private boolean showNegBtn = false;
public static AlertDialog getInstance(Context context){
if (alertDialog==null){
synchronized (AlertDialog.class) {
if (alertDialog == null) {
alertDialog = new AlertDialog(context).builder();
}
}
}
return alertDialog;
}
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.view_alertdialog, 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 = (Button) view.findViewById(R.id.btn_neg);
btn_neg.setVisibility(View.GONE);
btn_pos = (Button) view.findViewById(R.id.btn_pos);
btn_pos.setVisibility(View.GONE);
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);
// 调整dialog背景大小
lLayout_bg.setLayoutParams(new FrameLayout.LayoutParams((int) (display
.getWidth() * 0.85), LayoutParams.WRAP_CONTENT));
return this;
}
public AlertDialog setTitle(String title) {
showTitle = true;
if ("".equals(title)) {
txt_title.setText("标题");
} else {
txt_title.setText(title);
}
return this;
}
public AlertDialog setMsg(String msg) {
showMsg = true;
if ("".equals(msg)) {
txt_msg.setText("内容");
} else {
txt_msg.setText(msg);
}
return this;
}
public AlertDialog setMsg(int rId) {
showMsg = true;
txt_msg.setText(rId);
return this;
}
public AlertDialog setCancelable(boolean cancel) {
dialog.setCancelable(cancel);
return this;
}
public AlertDialog setPositiveButton(String text,
final OnClickListener listener) {
showPosBtn = true;
if ("".equals(text)) {
btn_pos.setText("确定");
} else {
btn_pos.setText(text);
}
btn_pos.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(listener!=null) {
listener.onClick(v);
}
dialog.dismiss();
}
});
return this;
}
public AlertDialog setNegativeButton(String text,
final OnClickListener listener) {
showNegBtn = true;
if ("".equals(text)) {
btn_neg.setText("取消");
} else {
btn_neg.setText(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("");
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("确定");
btn_pos.setVisibility(View.VISIBLE);
btn_pos.setBackgroundResource(R.drawable.alertdialog_single_selector);
btn_pos.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
if (showPosBtn && showNegBtn) {
btn_pos.setVisibility(View.VISIBLE);
btn_pos.setBackgroundResource(R.drawable.alertdialog_right_selector);
btn_neg.setVisibility(View.VISIBLE);
btn_neg.setBackgroundResource(R.drawable.alertdialog_left_selector);
img_line.setVisibility(View.VISIBLE);
}
if (showPosBtn && !showNegBtn) {
btn_pos.setVisibility(View.VISIBLE);
btn_pos.setBackgroundResource(R.drawable.alertdialog_single_selector);
}
if (!showPosBtn && showNegBtn) {
btn_neg.setVisibility(View.VISIBLE);
btn_neg.setBackgroundResource(R.drawable.alertdialog_single_selector);
}
}
public void show() {
setLayout();
dialog.show();
}
}
布局文件view_alertdialog.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lLayout_bg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/alert_bg"
android:orientation="vertical">
<TextView
android:id="@+id/txt_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:gravity="center"
android:text="提示"
android:textColor="@color/black"
android:textSize="18dp" />
<TextView
android:id="@+id/txt_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="您确定要退出吗?"
android:textColor="@color/black"
android:textSize="16dp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginTop="10dp"
android:background="@color/alertdialog_line" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_neg"
android:layout_width="wrap_content"
android:layout_height="43dp"
android:layout_weight="1"
android:background="@drawable/alertdialog_left_selector"
android:gravity="center"
android:text="确定"
android:textColor="@color/bigtextcolor"
android:textSize="16sp" />
<ImageView
android:id="@+id/img_line"
android:layout_width="0.5dp"
android:layout_height="43dp"
android:background="@color/alertdialog_line" />
<Button
android:id="@+id/btn_pos"
android:layout_width="wrap_content"
android:layout_height="43dp"
android:layout_weight="1"
android:background="@drawable/alertdialog_right_selector"
android:gravity="center"
android:text="取消"
android:textColor="@color/themecolor"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
效果显示