对话框对于应用也是必不可少的一个组件,在Android中也不例外,对话框对于一些提示重要信息,或者一些需要用户额外交互的一些内容很有帮助。

自定义Dialog步骤: 
1、主要创建Java类,并继承Dialog 
2、创建布局文件来加载和一些样式文件

效果图: 

                   

            

DialogBox.java

Java代码
  1. public class DialogBox extends Dialog {  
  2.   
  3.     public DialogBox(Context context) {  
  4.         super(context);  
  5.     }  
  6.   
  7.     public DialogBox(Context context, int theme) {  
  8.         super(context, theme);  
  9.     }  
  10.   
  11.     public static class Builder {  
  12.         private Context context;  
  13.         private String title;  
  14.         private String message;  
  15.         private String positiveButtonText;  
  16.         private String negativeButtonText;  
  17.         private View contentView;  
  18.         private DialogInterface.OnClickListener positiveButtonClickListener;  
  19.         private DialogInterface.OnClickListener negativeButtonClickListener;  
  20.   
  21.         public Builder(Context context) {  
  22.             this.context = context;  
  23.         }  
  24.   
  25.         public Builder setMessage(String message) {  
  26.             this.message = message;  
  27.             return this;  
  28.         }  
  29.   
  30.         /** 
  31.          * Set the Dialog message from resource 
  32.          *  
  33.          * @param title 
  34.          * @return 
  35.          */  
  36.         public Builder setMessage(int message) {  
  37.             this.message = (String) context.getText(message);  
  38.             return this;  
  39.         }  
  40.   
  41.         /** 
  42.          * Set the Dialog title from resource 
  43.          *  
  44.          * @param title 
  45.          * @return 
  46.          */  
  47.         public Builder setTitle(int title) {  
  48.             this.title = (String) context.getText(title);  
  49.             return this;  
  50.         }  
  51.   
  52.         /** 
  53.          * Set the Dialog title from String 
  54.          *  
  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.          *  
  72.          * @param positiveButtonText 
  73.          * @return 
  74.          */  
  75.         public Builder setPositiveButton(int positiveButtonText,  
  76.                 DialogInterface.OnClickListener listener) {  
  77.             this.positiveButtonText = (String) context  
  78.                     .getText(positiveButtonText);  
  79.             this.positiveButtonClickListener = listener;  
  80.             return this;  
  81.         }  
  82.   
  83.         public Builder setPositiveButton(String positiveButtonText,  
  84.                 DialogInterface.OnClickListener listener) {  
  85.             this.positiveButtonText = positiveButtonText;  
  86.             this.positiveButtonClickListener = listener;  
  87.             return this;  
  88.         }  
  89.   
  90.         public Builder setNegativeButton(int negativeButtonText,  
  91.                 DialogInterface.OnClickListener listener) {  
  92.             this.negativeButtonText = (String) context  
  93.                     .getText(negativeButtonText);  
  94.             this.negativeButtonClickListener = listener;  
  95.             return this;  
  96.         }  
  97.   
  98.         public Builder setNegativeButton(String negativeButtonText,  
  99.                 DialogInterface.OnClickListener listener) {  
  100.             this.negativeButtonText = negativeButtonText;  
  101.             this.negativeButtonClickListener = listener;  
  102.             return this;  
  103.         }  
  104.   
  105.         public DialogBox create() {  
  106.             LayoutInflater inflater = (LayoutInflater) context  
  107.                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  108.             final DialogBox dialog = new DialogBox(context, R.style.Dialog);  
  109.             View layout = inflater.inflate(R.layout.dialog_normal_layout, null);  
  110.             dialog.addContentView(layout, new LayoutParams(  
  111.                     LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));  
  112.             ((TextView) layout.findViewById(R.id.title)).setText(title);  
  113.             if (positiveButtonText != null) {  
  114.                 ((Button) layout.findViewById(R.id.positiveButton))  
  115.                         .setText(positiveButtonText);  
  116.                 if (positiveButtonClickListener != null) {  
  117.                     ((Button) layout.findViewById(R.id.positiveButton))  
  118.                             .setOnClickListener(new View.OnClickListener() {  
  119.                                 public void onClick(View v) {  
  120.                                     positiveButtonClickListener.onClick(dialog,  
  121.                                             DialogInterface.BUTTON_POSITIVE);  
  122.                                 }  
  123.                             });  
  124.                 }  
  125.             } else {  
  126.                 layout.findViewById(R.id.positiveButton).setVisibility(  
  127.                         View.GONE);  
  128.             }  
  129.             if (negativeButtonText != null) {  
  130.                 ((Button) layout.findViewById(R.id.negativeButton))  
  131.                         .setText(negativeButtonText);  
  132.                 if (negativeButtonClickListener != null) {  
  133.                     ((Button) layout.findViewById(R.id.negativeButton))  
  134.                             .setOnClickListener(new View.OnClickListener() {  
  135.                                 public void onClick(View v) {  
  136.                                     negativeButtonClickListener.onClick(dialog,  
  137.                                             DialogInterface.BUTTON_NEGATIVE);  
  138.                                 }  
  139.                             });  
  140.                 }  
  141.             } else {  
  142.                 layout.findViewById(R.id.negativeButton).setVisibility(  
  143.                         View.GONE);  
  144.             }  
  145.             if (message != null) {  
  146.                 ((TextView) layout.findViewById(R.id.message)).setText(message);  
  147.             } else if (contentView != null) {  
  148.                 ((LinearLayout) layout.findViewById(R.id.content))  
  149.                         .removeAllViews();  
  150.                 ((LinearLayout) layout.findViewById(R.id.content)).addView(  
  151.                         contentView, new LayoutParams(LayoutParams.FILL_PARENT,  
  152.                                 LayoutParams.FILL_PARENT));  
  153.             }  
  154.             dialog.setContentView(layout);  
  155.             return dialog;  
  156.         }  
  157.   
  158.     }  
  159. }  



自定义View的布局文件:

 


<?xml version="1.0" encoding="utf-8"?>                                                                    




MainActivity.Java

 

 

public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void showAlertDialog(View view) {DialogBox.Builder builder = new DialogBox.Builder(this);builder.setMessage("删除该商品?");builder.setTitle("温馨提示");builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {dialog.dismiss();// 你的操作事项}});builder.setNegativeButton("取消",new android.content.DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int which) {dialog.dismiss();}});builder.create().show();}}

 

源码点击下载

更多相关文章

  1. android support Percent支持库开发
  2. Mars视频笔记——Animation(2)
  3. Hello Android(安卓)- File文件操作
  4. Ubuntu14.04 下面安装SVN服务器
  5. android部分目录分析
  6. 【Android】更改程序图标
  7. Android实习生 —— 数据存储与共享
  8. Android开发——Android(安卓)Studio下使用Cmake在NDK环境下移植
  9. Android(安卓)Support Percent百分比布局

随机推荐

  1. Android(安卓)4.0 隐藏虚拟按键(导航栏)
  2. Android(安卓)CoordinatorLayout自定义Be
  3. Android中文API(117)――WrapperListAdap
  4. AOSP android 源码批量下载 windows平台
  5. Android定时器和Handler用法实例分析
  6. Android踩坑日记:android7.0动态相机权限
  7. Greendao简单使用
  8. android http EOFException
  9. 使用Mac终端给安卓手机安卓apk
  10. Android:Xml(读取与存储)