Android自定义dialogdemo

很多时候,我们需要自己去定义dialog,目前我们就遇见了这样一个需求,我的想法是自己定义一个dialog,如果有list的话就使用listview,如果有msg的话就使用msg,并且取消和确定按钮也可自己定义。自定义一个dialog,好处是我们自己定义背景,自己定义事件,自己定义按钮,能很完美的达到自己想要的效果。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. public class CustomDialog extends Dialog {  
  2. //实现默认构造函数  
  3. public CustomDialog(Context context, int theme) {  
  4.   
  5.   
  6.     super(context, theme);  
  7.     // TODO Auto-generated constructor stub  
  8. }  
  9.   
  10. protected CustomDialog(Context context, boolean cancelable,  
  11.         OnCancelListener cancelListener) {  
  12.     super(context, cancelable, cancelListener);  
  13.     // TODO Auto-generated constructor stub  
  14. }  
  15.   
  16. public CustomDialog(Context context) {  
  17.     super(context);  
  18.   
  19.     // TODO Auto-generated constructor stub  
  20. }  
  21. //所有的方法执行完都会返回一个Builder使得后面可以直接create和show  
  22.  public static class Builder {  
  23.         private Context context;  
  24.         private String title;  
  25.         private String message;  
  26.         private String positiveButtonText;//确定按钮  
  27.         private String negativeButtonText;//取消按钮  
  28.         private View contentView;  
  29.         private BaseAdapter adapter;//listview的adapter  
  30.         //确定按钮事件  
  31.         private DialogInterface.OnClickListener positiveButtonClickListener;  
  32.         //取消按钮事件  
  33.         private DialogInterface.OnClickListener negativeButtonClickListener;  
  34.         //listview的item点击事件  
  35.         private AdapterView.OnItemClickListener listViewOnclickListener;  
  36.   
  37.        public Builder(Context context) {  
  38.             this.context = context;  
  39.         }  
  40.         //设置消息  
  41.         public Builder setMessage(String message) {  
  42.             this.message = message;  
  43.             return this;  
  44.         }  
  45.   
  46.         /** 
  47.          *设置内容 
  48.          *  
  49.          * @param title 
  50.          * @return 
  51.          */  
  52.         public Builder setMessage(int message) {  
  53.             this.message = (String) context.getText(message);  
  54.             return this;  
  55.         }  
  56.   
  57.         /** 
  58.          * 设置标题 
  59.          *  
  60.          * @param title 
  61.          * @return 
  62.          */  
  63.         public Builder setTitle(int title) {  
  64.             this.title = (String) context.getText(title);  
  65.             return this;  
  66.         }  
  67.   
  68.         /** 
  69.          *设置标题 
  70.          *  
  71.          * @param title 
  72.          * @return 
  73.          */  
  74.   
  75.         public Builder setTitle(String title) {  
  76.             this.title = title;  
  77.             return this;  
  78.         }  
  79.         //设置适配器  
  80.         public Builder setAdapter(BaseAdapter adapter) {  
  81.             this.adapter = adapter;  
  82.             return this;  
  83.         }  
  84.         //设置点击事件  
  85.         public Builder setOnClickListener(AdapterView.OnItemClickListener listViewOnclickListener) {  
  86.             this.listViewOnclickListener = listViewOnclickListener;  
  87.             return this;  
  88.         }  
  89.         //设置整个背景  
  90.         public Builder setContentView(View v) {  
  91.             this.contentView = v;  
  92.             return this;  
  93.         }  
  94.         /** 
  95.          * 设置确定按钮和其点击事件 
  96.          *  
  97.          * @param positiveButtonText 
  98.          * @return 
  99.          */  
  100.         public Builder setPositiveButton(int positiveButtonText,  
  101.                 DialogInterface.OnClickListener listener) {  
  102.             this.positiveButtonText = (String) context  
  103.                     .getText(positiveButtonText);  
  104.             this.positiveButtonClickListener = listener;  
  105.             return this;  
  106.         }  
  107.   
  108.         public Builder setPositiveButton(String positiveButtonText,  
  109.                 DialogInterface.OnClickListener listener) {  
  110.             this.positiveButtonText = positiveButtonText;  
  111.             this.positiveButtonClickListener = listener;  
  112.             return this;  
  113.         }  
  114.         //设置取消按钮和其事件  
  115.         public Builder setNegativeButton(int negativeButtonText,  
  116.                 DialogInterface.OnClickListener listener) {  
  117.             this.negativeButtonText = (String) context  
  118.                     .getText(negativeButtonText);  
  119.             this.negativeButtonClickListener = listener;  
  120.             return this;  
  121.         }  
  122.   
  123.         public Builder setNegativeButton(String negativeButtonText,  
  124.                 DialogInterface.OnClickListener listener) {  
  125.             this.negativeButtonText = negativeButtonText;  
  126.             this.negativeButtonClickListener = listener;  
  127.             return this;  
  128.         }  
  129.     //createview方法  
  130.         public CustomDialog create() {  
  131.             LayoutInflater inflater = (LayoutInflater) context  
  132.                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  133.             // 设置其风格  
  134.             final CustomDialog dialog = new CustomDialog(context,R.style.CustomProgressDialog);  
  135.             View layout = inflater.inflate(R.layout.dialog_custom_layout, null);  
  136.             dialog.addContentView(layout, new LayoutParams(  
  137.                     LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));  
  138.             // 设置标题  
  139.             ((TextView) layout.findViewById(R.id.title)).setText(title);  
  140.           //设置listview的adapter如果没有就隐藏listview  
  141.             if(adapter != null && adapter.getCount()>0){  
  142.                 ListView listView = (ListView)layout.findViewById(R.id.listView);  
  143.                 listView.setAdapter(adapter);  
  144.                 if(listViewOnclickListener!=null){  
  145.   
  146.                     listView.setOnItemClickListener(listViewOnclickListener);  
  147.                 }  
  148.   
  149.             }else{  
  150.                 layout.findViewById(R.id.listView).setVisibility(  
  151.                         View.GONE);  
  152.             }  
  153.   
  154.   
  155.             //设置确定按钮  
  156.             if (positiveButtonText != null) {  
  157.                 ((Button) layout.findViewById(R.id.positiveButton))  
  158.                         .setText(positiveButtonText);  
  159.                 if (positiveButtonClickListener != null) {  
  160.                     ((Button) layout.findViewById(R.id.positiveButton))  
  161.                             .setOnClickListener(new View.OnClickListener() {  
  162.                                 public void onClick(View v) {  
  163.                                     positiveButtonClickListener.onClick(dialog,  
  164.                                             DialogInterface.BUTTON_POSITIVE);  
  165.                                 }  
  166.                             });  
  167.                 }  
  168.             } else {  
  169.                  // 如果没有确定按钮就将其隐藏  
  170.                 layout.findViewById(R.id.positiveButton).setVisibility(  
  171.                         View.GONE);  
  172.             }  
  173.             // 设置取消按钮  
  174.             if (negativeButtonText != null) {  
  175.                 ((Button) layout.findViewById(R.id.negativeButton))  
  176.                         .setText(negativeButtonText);  
  177.                 if (negativeButtonClickListener != null) {  
  178.                     ((Button) layout.findViewById(R.id.negativeButton))  
  179.                             .setOnClickListener(new View.OnClickListener() {  
  180.                                 public void onClick(View v) {  
  181.                                     negativeButtonClickListener.onClick(dialog,  
  182.                                             DialogInterface.BUTTON_NEGATIVE);  
  183.                                 }  
  184.                             });  
  185.                 }  
  186.             } else {  
  187.                 // 如果没有取消按钮就将其隐藏  
  188.                 layout.findViewById(R.id.negativeButton).setVisibility(  
  189.                         View.GONE);  
  190.             }  
  191.             // 设置内容  
  192.             if (message != null) {  
  193.                 ((TextView) layout.findViewById(R.id.message)).setText(message);  
  194.             } else if (contentView != null) {  
  195.                 // if no message set  
  196.                 // 添加view  
  197.                 ((LinearLayout) layout.findViewById(R.id.message))  
  198.                         .removeAllViews();  
  199.                 ((LinearLayout) layout.findViewById(R.id.message)).addView(  
  200.                         contentView, new LayoutParams(  
  201.                                 LayoutParams.WRAP_CONTENT,  
  202.                                 LayoutParams.WRAP_CONTENT));  
  203.             }  
  204.             dialog.setContentView(layout);  
  205.             return dialog;  
  206.         }  
  207.   
  208.     }  
  209. }  
posted @ 2015-06-04 09:30  网络虫  阅读(367)  评论(0编辑  收藏  举报