加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

CustomDialog 自定义Dialog (圆角 背景自定义)

(2012-08-20 15:16:23)
标签:

杂谈

public class CustomDialog extends Dialog {
 
   

public CustomDialog(Context context, int theme) {
        super(context, theme);
    }
 
    public CustomDialog(Context context) {
        super(context);
    }
 
   
    public static class Builder {
 
        private Context context;
       
        private String title;
        private String message;
        private String positiveButtonText;
        private String negativeButtonText;
        private String neutralButtonText;
        private View contentView;
        private View view;
        private Drawable icon;
        private Bitmap iconBitmap;
        private int  iconResId = -1;
        private DialogInterface.OnClickListener 
                        positiveButtonClickListener,
                        negativeButtonClickListener,
                        neutralButtonClickListener;
 
        public Builder(Context context) {
            this.context = context;
            
            title = null;
            message = null;
            positiveButtonText = null;
            negativeButtonText = null;
            neutralButtonText = null;
            contentView = null;
            view = null;
            icon = null;
            iconBitmap = null;
            iconResId = -1;
            
            positiveButtonClickListener = null;
            negativeButtonClickListener = null;
            neutralButtonClickListener = null;
            
        }
 
       
        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }
 
       
        public Builder setMessage(int message) {
            this.message = (String) context.getText(message);
            return this;
        }
 
       
        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }
 
        
        public Builder setView(View view) {
            this.view = view;
            return this;
        }
        public Builder setIcon(Drawable icon) {
            this.icon = icon;
            return this;
        }
        public Builder setIcon(Bitmap iconBitmap) {
            this.iconBitmap = iconBitmap;
            return this;
        }
        public Builder setIcon(int iconResId) {
            this.iconResId = iconResId;
            return this;
        }
        
        
       
        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }
 
       
        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }
 
       
        public Builder setPositiveButton(int positiveButtonText,
                DialogInterface.OnClickListener listener) {
            this.positiveButtonText = (String) context
                    .getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }
 
       
        public Builder setPositiveButton(String positiveButtonText,
                DialogInterface.OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }
 
       
        public Builder setNegativeButton(int negativeButtonText,
                DialogInterface.OnClickListener listener) {
            this.negativeButtonText = (String) context
                    .getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }
 
       
        public Builder setNegativeButton(String negativeButtonText,
                DialogInterface.OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }
        
        public Builder setNeutralButton(String negativeButtonText,
                DialogInterface.OnClickListener listener) {
            this.neutralButtonText = negativeButtonText;
            this.neutralButtonClickListener = listener;
            return this;
        }
 
       
        public CustomDialog create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            final CustomDialog dialog = new CustomDialog(context, 
            R.style.Dialog);
            View layout = inflater.inflate(R.layout.dialog, null);
            dialog.addContentView(layout, new LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            // set the dialog title
            ((TextView) layout.findViewById(R.id.title)).setText(title);
           
            
            //set icon 
            if(icon!=null){
            ((ImageView)(layout.findViewById(R.id.d_icon))).setBackgroundDrawable(icon);
            }
            if(iconBitmap!=null){
            ((ImageView)(layout.findViewById(R.id.d_icon))).setImageBitmap(iconBitmap);
            }
            if(iconResId!=-1){
            ((ImageView)(layout.findViewById(R.id.d_icon))).setBackgroundResource(iconResId);
            }
            // set the confirm button
            if (positiveButtonText != null) {
                ((Button) layout.findViewById(R.id.positiveButton))
                        .setText(positiveButtonText);
                if (positiveButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.positiveButton))
                            .setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    positiveButtonClickListener.onClick(
                                    dialog, 
                                            DialogInterface.BUTTON_POSITIVE);
                                    dialog.cancel();
                                }
                            });
                }else{
                ((Button) layout.findViewById(R.id.positiveButton))
                 .setOnClickListener(new View.OnClickListener() {
                     public void onClick(View v) {
                     
                         dialog.cancel();
                         
                     }
                 });
           
            }
                
                
                
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.positiveButton).setVisibility(
                        View.GONE);
            }
            // set the cancel button
            if (negativeButtonText != null) {
                ((Button) layout.findViewById(R.id.negativeButton))
                        .setText(negativeButtonText);
                if (negativeButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.negativeButton))
                            .setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                negativeButtonClickListener.onClick(
                                    dialog, 
                                            DialogInterface.BUTTON_NEGATIVE);
                                    dialog.cancel();
                                    
                                }
                            });
                }else{
                ((Button) layout.findViewById(R.id.negativeButton))
                     .setOnClickListener(new View.OnClickListener() {
                         public void onClick(View v) {
                         
                             dialog.cancel();
                             
                         }
                     });
               
                }
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.negativeButton).setVisibility(
                        View.GONE);
            }
            
            // set the neutralButton button
            if ( neutralButtonText != null) {
                ((Button) layout.findViewById(R.id.neutralButton))
                        .setText(neutralButtonText);
                if (neutralButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.neutralButton))
                            .setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                neutralButtonClickListener.onClick(
                                    dialog, 
                                            DialogInterface.BUTTON_NEGATIVE);
                                    dialog.cancel();
                                    
                                }
                            });
                }else{
                ((Button) layout.findViewById(R.id.neutralButton))
                     .setOnClickListener(new View.OnClickListener() {
                         public void onClick(View v) {
                         
                             dialog.cancel();
                             
                         }
                     });
               
                }
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.neutralButton).setVisibility(
                        View.GONE);
            }
            
            
            
            
            
            
            
            
            // set the content message
            if (message != null) {
            ((LinearLayout)layout.findViewById(R.id.content)).setVisibility(View.VISIBLE);
               ( (TextView)( layout.findViewById(R.id.dialog_message))).setText(message);
               ((TextView)layout.findViewById(R.id.dialog_message)).setVisibility(View.VISIBLE);
            } else{
            ((TextView)layout.findViewById(R.id.dialog_message)).setVisibility(View.GONE);
            }
            //set view 
             if(view!=null){
            ((LinearLayout)layout.findViewById(R.id.content)).setVisibility(View.VISIBLE);
            ((LinearLayout)layout.findViewById(R.id.content)).addView(view);
            }else if(message ==null){
            ((LinearLayout)layout.findViewById(R.id.content)).setVisibility(View.GONE);
            }
           
            if (contentView != null) {
                // if no message set
                // add the contentView to the dialog body
                ((LinearLayout) layout.findViewById(R.id.content))
                        .removeAllViews();
                ((LinearLayout) layout.findViewById(R.id.content))
                        .addView(contentView, 
                                new LayoutParams(
                                        LayoutParams.WRAP_CONTENT, 
                                        LayoutParams.WRAP_CONTENT));
            }
            dialog.setContentView(layout);
            return dialog;
        }
 
       
        
    }
 
}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有