Android自定义单例AlertDialog详解

  

当Android开发处理错误信息时,经常会以对话框的形式显示错误信息,但是每次都新的一个对话框,很麻烦,也增加程序的开销,所以今天就分享一种自定义单例AlertDialog

  

        公开课AlertDialog {   私有静态AlertDialog AlertDialog=零;   私人上下文语境;   私人对话框对话框;   私人LinearLayout lLayout_bg;   私人TextView txt_title;   私人TextView txt_msg;   私人按钮btn_neg;   私人按钮btn_pos;   私人ImageView img_line;   私人显示显示;   私人布尔showTitle=false;   私人布尔showMsg=false;   私人布尔showPosBtn=false;   私人布尔showNegBtn=false;      公共静态AlertDialog getInstance(上下文语境){   如果(alertDialog==null) {   同步(AlertDialog.class) {   如果(alertDialog==null) {   alertDialog=new alertDialog(上下文).builder ();   }   }   }   返回alertDialog;   }   公共AlertDialog(上下文语境){   这一点。上下文=上下文;   WindowManager WindowManager=(WindowManager)上下文   .getSystemService (Context.WINDOW_SERVICE);   显示=windowManager.getDefaultDisplay ();   }      公共AlertDialog builder () {//获取对话框布的局   视图视图=LayoutInflater.from(上下文).inflate (R.layout.view_alertdialog, null);//获取自定义对话框布局中的控件   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=(按钮)view.findViewById (R.id.btn_neg);   btn_neg.setVisibility (View.GONE);   btn_pos=(按钮)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);//定义对话框布局和参数   对话框=new对话框(上下文,R.style.AlertDialogStyle);   dialog.setContentView(查看);//调整对话背景大小   lLayout_bg。setLayoutParams(新FrameLayout.LayoutParams ((int)(显示   .getWidth () * 0.85), LayoutParams.WRAP_CONTENT));      返回;   }      公共AlertDialog setTitle(字符串标题){   showTitle=true;   如果“.equals(标题)){   txt_title.setText(“标题”);   其他}{   txt_title.setText(标题);   }   返回;   }      公共AlertDialog setMsg(字符串味精){   showMsg=true;   如果“.equals(味精)){   txt_msg.setText(“内容”);   其他}{   txt_msg.setText(味精);   }   返回;   }   公共AlertDialog setMsg (int掉){   showMsg=true;   txt_msg.setText(去掉);   返回;   }      公共AlertDialog setCancelable(布尔取消){   dialog.setCancelable(取消);   返回;   }      公共AlertDialog setPositiveButton(字符串文本,   最后>   & lt; 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:背景=" @drawable/alert_bg”   面向android:=按怪薄北?      & lt; TextView   android: id=癅 + id/txt_title”   android: layout_width=" match_parent "   android: layout_height=" wrap_content "   android: layout_marginLeft=" 15 dp "   android: layout_marginRight=" 15 dp "   android: layout_marginTop=" 15 dp "   android:重力="中心"   android:文本="提示”   android:输入textColor=" @color/黑”   android: textSize=" 18 dp/比;         & lt; TextView   android: id=癅 + id/txt_msg”   android: layout_width=" match_parent "   android: layout_height=" wrap_content "   android: layout_marginLeft=" 15 dp "   android: layout_marginRight=" 15 dp "   android: layout_marginTop=" 10 dp”   android: layout_marginBottom=" 10 dp”   android:重力="中心"   android:文本="您确定要退出吗?”   android:输入textColor=" @color/黑”   android: textSize=?6 dp/比;      & lt; ImageView   android: layout_width=" match_parent "   android: layout_height=" 0.5 dp "   android: layout_marginTop=" 10 dp”   android:背景=癅color/alertdialog_line”/比;      LinearLayout & lt;   android: layout_width=" match_parent "   android: layout_height=" wrap_content "   面向android:="水平"比;      & lt;按钮   android: id=癅 + id/btn_neg”   android: layout_width=" wrap_content "   android: layout_height=" 43 dp "   android: layout_weight=" 1 "   android:背景=" @drawable/alertdialog_left_selector”   android:重力="中心"   android:文本="确定”   android:输入textColor=" @color/bigtextcolor”   android: textSize=" 16 sp "/比;      & lt; ImageView   android: id=癅 + id/img_line”   android: layout_width=" 0.5 dp "   android: layout_height=" 43 dp "   android:背景=癅color/alertdialog_line”/比;      & lt;按钮   android: id=癅 + id/btn_pos”   android: layout_width=" wrap_content "   android: layout_height=" 43 dp "   android: layout_weight=" 1 "   android:背景=" @drawable/alertdialog_right_selector”   android:重力="中心"   android:文本="取消”   android:输入textColor=" @color/themecolor”   android: textSize=" 16 sp "/比;   & lt;/LinearLayout>      & lt;/LinearLayout>      

Android自定义单例AlertDialog详解