android弹出框Dialog工具类

应用开发中一般都会将Dialog做成统一风格,基本上包含Title、Message、Button(单/双)三部分,大多数情况下都是对这三部分的不同组合,比如Message+Button(单/双)、Title+Button(单/双)、Message,个人针对这个需求做了个通用的工具类PopupDialog,方便使用,这里共享给有需要的童鞋:

预览图:





PopupDialog.java:

[java]  view plain  copy
  1. import android.app.AlertDialog;  
  2. import android.content.Context;  
  3. import android.content.DialogInterface;  
  4. import android.content.res.Resources;  
  5. import android.graphics.Point;  
  6. import android.os.Build;  
  7. import android.os.Bundle;  
  8. import android.text.method.ScrollingMovementMethod;  
  9. import android.util.DisplayMetrics;  
  10. import android.util.Log;  
  11. import android.view.Display;  
  12. import android.view.LayoutInflater;  
  13. import android.view.View;  
  14. import android.view.WindowManager;  
  15. import android.widget.ImageView;  
  16. import android.widget.LinearLayout;  
  17. import android.widget.RelativeLayout;  
  18. import android.widget.TextView;  
  19.   
  20. import java.lang.reflect.Method;  
  21.   
  22. /** 
  23.  * Created by weishj on 2018/1/11. 
  24.  */  
  25. public class PopupDialog extends AlertDialog {  
  26.     private static final String TAG = "PopupDialog";  
  27.     private View view;  
  28.     private Context context;  
  29.     private TextView title;  
  30.     private ImageView close;  
  31.     private TextView msg;  
  32.     private TextView confirm;  
  33.     private TextView cancel;  
  34.     private LinearLayout bottomLl;  
  35.     private RelativeLayout topRl;  
  36.     private View verticalLine;  
  37.     private int width;  
  38.   
  39.     protected PopupDialog(Context context, boolean cancelable, boolean canceledOnTouchOutside) {  
  40.         super(context, R.style.Dialog_Common);  
  41.         this.context = context;  
  42.         double deviceWidth = getScreenWidth(this.context);  
  43.         width = (int) (deviceWidth * 0.7);  
  44.         setCancelable(cancelable);  
  45.         setCanceledOnTouchOutside(canceledOnTouchOutside);  
  46.         LayoutInflater inflater = LayoutInflater.from(this.context);  
  47.         view = inflater.inflate(R.layout.popup_dialog, null);  
  48.         initView();  
  49.     }  
  50.   
  51.     @Override  
  52.     protected void onCreate(Bundle savedInstanceState) {  
  53.         super.onCreate(savedInstanceState);  
  54.         LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, LinearLayout.LayoutParams  
  55.                 .WRAP_CONTENT, 0);  
  56.         setContentView(view, params);  
  57.     }  
  58.   
  59.     public void setDialogTitle(CharSequence title, boolean closeBtn) {  
  60.         if (title == null || "".equals(title)) {  
  61.             if (this.title != null) {  
  62.                 this.title.setVisibility(View.GONE);  
  63.             }  
  64.         } else {  
  65.             if (this.title != null) {  
  66.                 this.title.setText(title);  
  67.             }  
  68.         }  
  69.         if (closeBtn && this.close != null) {  
  70.             this.close.setVisibility(View.VISIBLE);  
  71.         }  
  72.     }  
  73.   
  74.     public void setDialogMessage(CharSequence msg) {  
  75.         if (msg == null || "".equals(msg)) {  
  76.             if (this.msg != null) {  
  77.                 this.msg.setVisibility(View.GONE);  
  78.             }  
  79.         } else {  
  80.             if (this.msg != null) {  
  81.                 this.msg.setText(msg);  
  82.             }  
  83.         }  
  84.     }  
  85.   
  86.     protected void setDialogButton(int whichButton, CharSequence text, final View.OnClickListener listener) {  
  87.         if (text == null || "".equals(text)) {  
  88.             switch (whichButton) {  
  89.                 case DialogInterface.BUTTON_POSITIVE: {  
  90.                     if (this.confirm != null) {  
  91.                         this.confirm.setVisibility(View.GONE);  
  92.                     }  
  93.                     break;  
  94.                 }  
  95.                 case DialogInterface.BUTTON_NEGATIVE: {  
  96.                     if (this.cancel != null) {  
  97.                         this.cancel.setVisibility(View.GONE);  
  98.                     }  
  99.                     break;  
  100.                 }  
  101.                 default: {  
  102.                     Log.e(TAG, "Button can not be found. whichButton=" + whichButton);  
  103.                 }  
  104.             }  
  105.         } else {  
  106.             switch (whichButton) {  
  107.                 case DialogInterface.BUTTON_POSITIVE: {  
  108.                     if (this.confirm != null) {  
  109.                         this.confirm.setText(text);  
  110.                         this.confirm.setOnClickListener(new View.OnClickListener() {  
  111.                             @Override  
  112.                             public void onClick(View v) {  
  113.                                 if (listener != null) {  
  114.                                     listener.onClick(v);  
  115.                                 }  
  116.                                 dismiss();  
  117.                             }  
  118.                         });  
  119.                     }  
  120.                     break;  
  121.                 }  
  122.                 case DialogInterface.BUTTON_NEGATIVE: {  
  123.                     if (this.cancel != null) {  
  124.                         this.cancel.setText(text);  
  125.                         this.cancel.setOnClickListener(new View.OnClickListener() {  
  126.                             @Override  
  127.                             public void onClick(View v) {  
  128.                                 if (listener != null) {  
  129.                                     listener.onClick(v);  
  130.                                 }  
  131.                                 dismiss();  
  132.                             }  
  133.                         });  
  134.                     }  
  135.                     break;  
  136.                 }  
  137.                 default: {  
  138.                     Log.e(TAG, "Button can not be found. whichButton=" + whichButton);  
  139.                 }  
  140.             }  
  141.         }  
  142.     }  
  143.   
  144.     public void setDialogButton(String confirm, View.OnClickListener positiveClickListener, String cancel, View  
  145.             .OnClickListener negativeClickListener) {  
  146.         if ((confirm == null || "".equals(confirm)) && (cancel == null || "".equals(cancel))) {  
  147.             if (this.bottomLl != null) {  
  148.                 this.bottomLl.setVisibility(View.GONE);  
  149.             }  
  150.         } else if ((confirm != null && !"".equals(confirm)) && (cancel != null && !"".equals(cancel))) {  
  151.             setDialogButton(DialogInterface.BUTTON_POSITIVE, confirm, positiveClickListener);  
  152.             setDialogButton(DialogInterface.BUTTON_NEGATIVE, cancel, negativeClickListener);  
  153.         } else {  
  154.             // Hide vertical line  
  155.             this.verticalLine.setVisibility(View.GONE);  
  156.             // Hide positive button  
  157.             setDialogButton(DialogInterface.BUTTON_POSITIVE, nullnull);  
  158.             if (confirm == null || "".equals(confirm)) {  
  159.                 setDialogButton(DialogInterface.BUTTON_NEGATIVE, cancel, negativeClickListener);  
  160.             } else {  
  161.                 // confirm is not null and cancel is null  
  162.                 setDialogButton(DialogInterface.BUTTON_NEGATIVE, confirm, positiveClickListener);  
  163.             }  
  164.   
  165.         }  
  166.     }  
  167.   
  168.     private void initView() {  
  169.         this.title = (TextView) view.findViewById(R.id.common_dialog_title_tv);  
  170.         this.close = (ImageView) view.findViewById(R.id.common_dialog_close_iv);  
  171.         this.msg = (TextView) view.findViewById(R.id.common_dialog_message_tv);  
  172.         // Set Scrollable  
  173.         this.msg.setMovementMethod(ScrollingMovementMethod.getInstance());  
  174.         this.confirm = (TextView) view.findViewById(R.id.common_dialog_confirm_tv);  
  175.         this.cancel = (TextView) view.findViewById(R.id.common_dialog_cancel_tv);  
  176.         this.bottomLl = (LinearLayout) view.findViewById(R.id.common_dialog_bottom_ll);  
  177.         this.topRl = (RelativeLayout) view.findViewById(R.id.common_dialog_top_rl);  
  178.         this.verticalLine = view.findViewById(R.id.common_dialog_vertical_line);  
  179.   
  180.         if (this.close != null) {  
  181.             this.close.setOnClickListener(new View.OnClickListener() {  
  182.                 @Override  
  183.                 public void onClick(View v) {  
  184.                     dismiss();  
  185.                 }  
  186.             });  
  187.         }  
  188.     }  
  189.   
  190.     /** 
  191.      * Obtain a confirm dialog instance 
  192.      * 
  193.      * @param context                context 
  194.      * @param title                  title of the dialog, pass null or "" if no title is needed 
  195.      * @param message                message to show 
  196.      * @param confirm                name of confirm button, pass null or "" if no confirm button is needed 
  197.      * @param positiveClickListener  click listener of confirm button 
  198.      * @param cancel                 name of cancel button, pass null or "" if no confirm button is needed 
  199.      * @param negativeClickListener  click listener of cancel button 
  200.      * @param cancelable             cancelable when press back 
  201.      * @param canceledOnTouchOutside canceled on touch outside 
  202.      * @param closeBtn               whether to show close button 
  203.      * 
  204.      * @return PopupDialog 
  205.      */  
  206.     public static PopupDialog create(Context context, String title, String message, String confirm, View.OnClickListener  
  207.             positiveClickListener, String cancel, View.OnClickListener negativeClickListener, boolean cancelable,  
  208.                                      boolean canceledOnTouchOutside, boolean closeBtn) {  
  209.         PopupDialog dialog = new PopupDialog(context, cancelable, canceledOnTouchOutside);  
  210.         dialog.setDialogTitle(title, closeBtn);  
  211.         dialog.setDialogMessage(message);  
  212.         dialog.setDialogButton(confirm, positiveClickListener, cancel, negativeClickListener);  
  213.   
  214.         return dialog;  
  215.     }  
  216.   
  217.     /** 
  218.      * Obtain a confirm dialog instance 
  219.      * 
  220.      * @param context                context 
  221.      * @param titleRes               resource id of the dialog's title, pass 0 if no title is needed 
  222.      * @param messageRes             resource id of the dialog's message, pass 0 if no title is needed 
  223.      * @param confirmRes             resource id of the dialog's confirm button, pass 0 if no title is needed 
  224.      * @param positiveClickListener  click listener of confirm button 
  225.      * @param cancelRes              resource id of the dialog's cancel button, pass 0 if no title is needed 
  226.      * @param negativeClickListener  click listener of cancel button 
  227.      * @param cancelable             cancelable when press back 
  228.      * @param canceledOnTouchOutside canceled on touch outside 
  229.      * @param closeBtn               whether to show close button 
  230.      * 
  231.      * @return PopupDialog 
  232.      */  
  233.     public static PopupDialog create(Context context, int titleRes, int messageRes, int confirmRes, View.OnClickListener  
  234.             positiveClickListener, int cancelRes, View.OnClickListener negativeClickListener, boolean cancelable,  
  235.                                   boolean canceledOnTouchOutside, boolean closeBtn) {  
  236.         return create(context, titleRes, messageRes, confirmRes, positiveClickListener, cancelRes, negativeClickListener,  
  237.                 cancelable, canceledOnTouchOutside, closeBtn, null);  
  238.     }  
  239.   
  240.     public static PopupDialog create(Context context, int titleRes, int messageRes, int confirmRes, View.OnClickListener  
  241.             positiveClickListener, int cancelRes, View.OnClickListener negativeClickListener, boolean cancelable,  
  242.                                   boolean canceledOnTouchOutside, boolean closeBtn, OnDismissListener listener) {  
  243.         PopupDialog dialog = new PopupDialog(context, cancelable, canceledOnTouchOutside);  
  244.         if (listener != null) {  
  245.             dialog.setOnDismissListener(listener);  
  246.         }  
  247.         String title = null;  
  248.         try {  
  249.             title = titleRes > 0 ? context.getResources().getString(titleRes) : null;  
  250.         } catch (Resources.NotFoundException e) {  
  251.             Log.w(TAG, "Resource not found. resId=" + titleRes, e);  
  252.         }  
  253.         dialog.setDialogTitle(title, closeBtn);  
  254.         String msg = null;  
  255.         try {  
  256.             msg = messageRes > 0 ? context.getResources().getString(messageRes) : null;  
  257.         } catch (Resources.NotFoundException e) {  
  258.             Log.w(TAG, "Resource not found. resId=" + messageRes, e);  
  259.         }  
  260.         dialog.setDialogMessage(msg);  
  261.         String confirm = null;  
  262.         String cancel = null;  
  263.         try {  
  264.             confirm = confirmRes > 0 ? context.getResources().getString(confirmRes) : null;  
  265.             cancel = cancelRes > 0 ? context.getResources().getString(cancelRes) : null;  
  266.         } catch (Resources.NotFoundException e) {  
  267.             Log.w(TAG, "Resource not found.", e);  
  268.         }  
  269.         dialog.setDialogButton(confirm, positiveClickListener, cancel, negativeClickListener);  
  270.           
  271.         return dialog;  
  272.     }  
  273.   
  274.     public void setCancel(int cancelRes) {  
  275.         cancel.setText(context.getResources().getString(cancelRes));  
  276.     }  
  277.   
  278.     private int getScreenWidth(Context context) {  
  279.         return getScreenSize(context)[0];  
  280.     }  
  281.   
  282.     private int getScreenHeight(Context context) {  
  283.         return getScreenSize(context)[1];  
  284.     }  
  285.   
  286.     private int[] getScreenSize(Context context) {  
  287.         WindowManager windowManager;  
  288.         try {  
  289.             windowManager = (WindowManager)context.getSystemService("window");  
  290.         } catch (Throwable var6) {  
  291.             Log.w(TAG, var6);  
  292.             windowManager = null;  
  293.         }  
  294.   
  295.         if(windowManager == null) {  
  296.             return new int[]{00};  
  297.         } else {  
  298.             Display display = windowManager.getDefaultDisplay();  
  299.             if(Build.VERSION.SDK_INT < 13) {  
  300.                 DisplayMetrics t1 = new DisplayMetrics();  
  301.                 display.getMetrics(t1);  
  302.                 return new int[]{t1.widthPixels, t1.heightPixels};  
  303.             } else {  
  304.                 try {  
  305.                     Point t = new Point();  
  306.                     Method method = display.getClass().getMethod("getRealSize"new Class[]{Point.class});  
  307.                     method.setAccessible(true);  
  308.                     method.invoke(display, new Object[]{t});  
  309.                     return new int[]{t.x, t.y};  
  310.                 } catch (Throwable var5) {  
  311.                     Log.w(TAG, var5);  
  312.                     return new int[]{00};  
  313.                 }  
  314.             }  
  315.         }  
  316.     }  
  317. }  

popup_dialog.xml:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.               xmlns:tools="http://schemas.android.com/tools"  
  4.               android:orientation="vertical"  
  5.               android:layout_width="match_parent"  
  6.               android:layout_height="wrap_content"  
  7.               android:background="@drawable/popup_dialog_bg">  
  8.     <RelativeLayout  
  9.         android:id="@+id/common_dialog_top_rl"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:paddingLeft="15dp"  
  13.         android:paddingRight="15dp"  
  14.         android:layout_marginBottom="24dp"  
  15.         android:gravity="center_vertical">  
  16.         <TextView  
  17.             android:id="@+id/common_dialog_title_tv"  
  18.             android:layout_width="match_parent"  
  19.             android:layout_height="wrap_content"  
  20.             android:gravity="center_horizontal"  
  21.             android:layout_marginTop="20dp"  
  22.             android:textSize="18dp"  
  23.             android:textColor="#555555"  
  24.             tools:text="标题"/>  
  25.         <ImageView  
  26.             android:id="@+id/common_dialog_close_iv"  
  27.             android:layout_width="20dp"  
  28.             android:layout_height="20dp"  
  29.             android:layout_marginTop="15dp"  
  30.             android:layout_alignParentRight="true"  
  31.             android:src="@drawable/ic_popup_dialog_close"  
  32.             android:visibility="gone"/>  
  33.     </RelativeLayout>  
  34.     <TextView  
  35.         android:id="@+id/common_dialog_message_tv"  
  36.         android:layout_width="match_parent"  
  37.         android:layout_height="wrap_content"  
  38.         android:gravity="center"  
  39.         android:layout_marginLeft="15dp"  
  40.         android:layout_marginRight="15dp"  
  41.         android:layout_marginBottom="17dp"  
  42.         android:maxLines="5"  
  43.         android:minHeight="25dp"  
  44.         android:textSize="14dp"  
  45.         android:textColor="#555555"  
  46.         tools:text="信息"/>  
  47.     <View  
  48.         android:layout_width="match_parent"  
  49.         android:layout_height="1dp"  
  50.         android:background="#F5F5F5"/>  
  51.     <LinearLayout  
  52.         android:id="@+id/common_dialog_bottom_ll"  
  53.         android:layout_width="match_parent"  
  54.         android:layout_height="50dp"  
  55.         android:orientation="horizontal">  
  56.         <TextView  
  57.             android:id="@+id/common_dialog_cancel_tv"  
  58.             android:layout_width="0dp"  
  59.             android:layout_height="match_parent"  
  60.             android:layout_weight="1"  
  61.             android:gravity="center"  
  62.             android:clickable="true"  
  63.             android:textColor="#0679FE"  
  64.             android:textSize="18dp"  
  65.             tools:text="取消"/>  
  66.         <View  
  67.             android:id="@+id/common_dialog_vertical_line"  
  68.             android:layout_width="1dp"  
  69.             android:layout_height="match_parent"  
  70.             android:background="#F5F5F5"/>  
  71.         <TextView  
  72.             android:id="@+id/common_dialog_confirm_tv"  
  73.             android:layout_width="0dp"  
  74.             android:layout_height="match_parent"  
  75.             android:layout_weight="1"  
  76.             android:gravity="center"  
  77.             android:clickable="true"  
  78.             android:textColor="#0679FE"  
  79.             android:textSize="18dp"  
  80.             tools:text="确定"/>  
  81.     </LinearLayout>  
  82. </LinearLayout>  

popup_dialog_bg.xml:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <corners android:radius="8dp"/>  
  4.     <solid android:color="#FFFFFF"/>  
  5.     <stroke  
  6.         android:width="1px"  
  7.         android:color="#2E000000"/>  
  8. </shape>  

ic_popup_dialog_close.png:


使用方法:

[java]  view plain  copy
  1. PopupDialog.create(...).show();  
点击按钮时会自动关闭dialog
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值