Android自定义Dialog(美化界面)

简介:

前言:在做项目的时候,发现dialog界面太丑陋,从csdn上下载了一份自定义dialog的源码,在他的基础上对界面进行美化...有需要的朋友可以直接拿走


效果图如下:



主要代码:

  1. /** 
  2.  * 自定义dialog 
  3.  * @author ansen 
  4.  */  
  5. public class CustomDialog extends Dialog {  
  6.     public CustomDialog(Context context) {  
  7.         super(context);  
  8.     }  
  9.   
  10.     public CustomDialog(Context context, int theme) {  
  11.         super(context, theme);  
  12.     }  
  13.   
  14.     public static class Builder {  
  15.         private Context context;  
  16.         private String title;  
  17.         private String message;  
  18.         private String positiveButtonText;  
  19.         private String negativeButtonText;  
  20.         private View contentView;  
  21.         private DialogInterface.OnClickListener positiveButtonClickListener;  
  22.         private DialogInterface.OnClickListener negativeButtonClickListener;  
  23.   
  24.         public Builder(Context context) {  
  25.             this.context = context;  
  26.         }  
  27.   
  28.         public Builder setMessage(String message) {  
  29.             this.message = message;  
  30.             return this;  
  31.         }  
  32.   
  33.         /** 
  34.          * Set the Dialog message from resource 
  35.          * @param title 
  36.          * @return 
  37.          */  
  38.         public Builder setMessage(int message) {  
  39.             this.message = (String) context.getText(message);  
  40.             return this;  
  41.         }  
  42.   
  43.         /** 
  44.          * Set the Dialog title from resource 
  45.          * @param title 
  46.          * @return 
  47.          */  
  48.         public Builder setTitle(int title) {  
  49.             this.title = (String) context.getText(title);  
  50.             return this;  
  51.         }  
  52.   
  53.         /** 
  54.          * Set the Dialog title from String 
  55.          * @param title 
  56.          * @return 
  57.          */  
  58.   
  59.         public Builder setTitle(String title) {  
  60.             this.title = title;  
  61.             return this;  
  62.         }  
  63.   
  64.         public Builder setContentView(View v) {  
  65.             this.contentView = v;  
  66.             return this;  
  67.         }  
  68.   
  69.         /** 
  70.          * Set the positive button resource and it's listener 
  71.          * @param positiveButtonText 
  72.          * @return 
  73.          */  
  74.         public Builder setPositiveButton(int positiveButtonText,  
  75.                 DialogInterface.OnClickListener listener) {  
  76.             this.positiveButtonText = (String) context  
  77.                     .getText(positiveButtonText);  
  78.             this.positiveButtonClickListener = listener;  
  79.             return this;  
  80.         }  
  81.   
  82.         public Builder setPositiveButton(String positiveButtonText,  
  83.                 DialogInterface.OnClickListener listener) {  
  84.             this.positiveButtonText = positiveButtonText;  
  85.             this.positiveButtonClickListener = listener;  
  86.             return this;  
  87.         }  
  88.   
  89.         public Builder setNegativeButton(int negativeButtonText,  
  90.                 DialogInterface.OnClickListener listener) {  
  91.             this.negativeButtonText = (String) context  
  92.                     .getText(negativeButtonText);  
  93.             this.negativeButtonClickListener = listener;  
  94.             return this;  
  95.         }  
  96.   
  97.         public Builder setNegativeButton(String negativeButtonText,  
  98.                 DialogInterface.OnClickListener listener) {  
  99.             this.negativeButtonText = negativeButtonText;  
  100.             this.negativeButtonClickListener = listener;  
  101.             return this;  
  102.         }  
  103.           
  104.         //创建dialog对象   主要就是这个方法,加载自定义布局文件  
  105.         public CustomDialog create() {  
  106.             LayoutInflater inflater = (LayoutInflater) context  
  107.                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  108.             // instantiate the dialog with the custom Theme  
  109.             final CustomDialog dialog = new CustomDialog(context,  
  110.                     R.style.Dialog);  
  111.             View layout = inflater.inflate(R.layout.dialog_normal_layout, null);  
  112.             dialog.addContentView(layout, new LayoutParams(  
  113.                     LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));  
  114.             // set the dialog title  
  115.             ((TextView) layout.findViewById(R.id.title)).setText(title);  
  116.             // set the confirm button  
  117.             if (positiveButtonText != null) {  
  118.                 ((Button) layout.findViewById(R.id.positiveButton))  
  119.                         .setText(positiveButtonText);  
  120.                 if (positiveButtonClickListener != null) {  
  121.                     ((Button) layout.findViewById(R.id.positiveButton))  
  122.                             .setOnClickListener(new View.OnClickListener() {  
  123.                                 public void onClick(View v) {  
  124.                                     positiveButtonClickListener.onClick(dialog,  
  125.                                             DialogInterface.BUTTON_POSITIVE);  
  126.                                 }  
  127.                             });  
  128.                 }  
  129.             } else {  
  130.                 // if no confirm button just set the visibility to GONE  
  131.                 layout.findViewById(R.id.positiveButton).setVisibility(  
  132.                         View.GONE);  
  133.             }  
  134.             // set the cancel button  
  135.             if (negativeButtonText != null) {  
  136.                 ((Button) layout.findViewById(R.id.negativeButton))  
  137.                         .setText(negativeButtonText);  
  138.                 if (negativeButtonClickListener != null) {  
  139.                     ((Button) layout.findViewById(R.id.negativeButton))  
  140.                             .setOnClickListener(new View.OnClickListener() {  
  141.                                 public void onClick(View v) {  
  142.                                     negativeButtonClickListener.onClick(dialog,  
  143.                                             DialogInterface.BUTTON_NEGATIVE);  
  144.                                 }  
  145.                             });  
  146.                 }  
  147.             } else {  
  148.                 // if no confirm button just set the visibility to GONE  
  149.                 layout.findViewById(R.id.negativeButton).setVisibility(  
  150.                         View.GONE);  
  151.             }  
  152.             // set the content message  
  153.             if (message != null) {  
  154.                 ((TextView) layout.findViewById(R.id.message)).setText(message);  
  155.             } else if (contentView != null) {  
  156.                 ((LinearLayout) layout.findViewById(R.id.content))  
  157.                         .removeAllViews();  
  158.                 ((LinearLayout) layout.findViewById(R.id.content)).addView(  
  159.                         contentView, new LayoutParams(LayoutParams.FILL_PARENT,  
  160.                                 LayoutParams.FILL_PARENT));  
  161.             }  
  162.             dialog.setContentView(layout);  
  163.             return dialog;  
  164.         }  
  165.   
  166.     }  
  167. }  


4.Activity中如何调用:

  1. CustomDialog.Builder builder = new CustomDialog.Builder(this);  
  2. builder.setMessage("这个就是自定义的提示框");  
  3. builder.setTitle("提示");  
  4. builder.setPositiveButton("确定"new DialogInterface.OnClickListener() {  
  5.     public void onClick(DialogInterface dialog, int which) {  
  6.         dialog.dismiss();  
  7.         //设置你的操作事项  
  8.     }  
  9. });  
  10.   
  11. builder.setNegativeButton("取消",  
  12.         new android.content.DialogInterface.OnClickListener() {  
  13.             public void onClick(DialogInterface dialog, int which) {  
  14.                 dialog.dismiss();  
  15.             }  
  16.         });  
  17.   
  18. builder.create().show();  
目录
相关文章
|
24天前
|
XML Java Android开发
Android实现自定义进度条(源码+解析)
Android实现自定义进度条(源码+解析)
51 1
|
4月前
|
XML Android开发 数据安全/隐私保护
Android 自定义开源库 EasyView
Android 自定义开源库 EasyView
|
4月前
|
XML Java Android开发
Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)
Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)
42 1
|
4月前
|
XML 搜索推荐 Java
Android App开发之自定义图形中位图与图形互转、剪裁图形内部区域、给图形添加部件的讲解及实战(附源码 简单易懂)
Android App开发之自定义图形中位图与图形互转、剪裁图形内部区域、给图形添加部件的讲解及实战(附源码 简单易懂)
33 0
|
3天前
|
移动开发 Java Unix
Android系统 自动加载自定义JAR文件
Android系统 自动加载自定义JAR文件
21 1
|
3天前
|
Shell Android开发 开发者
Android系统 自定义动态修改init.custom.rc
Android系统 自定义动态修改init.custom.rc
23 0
|
3天前
|
存储 安全 Android开发
Android系统 自定义系统和应用权限
Android系统 自定义系统和应用权限
18 0
|
4月前
|
XML API Android开发
Android 自定义View 之 圆环进度条
Android 自定义View 之 圆环进度条
|
28天前
|
Android开发
Android 开发 pickerview 自定义选择器
Android 开发 pickerview 自定义选择器
12 0
|
2月前
|
Android开发 数据安全/隐私保护
【Android Studio】简单的QQ登录界面
【Android Studio】简单的QQ登录界面