Android自定义单例AlertDialog详解
程序员文章站
2023-12-16 11:18:28
当android开发处理错误信息时,经常会以dialog的形式显示错误信息,但是每次都new一个dialog,很麻烦,也增加程序的开销,所以今天就分享一种自定义单例aler...
当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>
效果显示
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。