Android自定义dialog


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


下面就是要贴出的代码,代码中有详细的注释

public class CustomDialog extends Dialog {
//实现默认构造函数
public CustomDialog(Context context, int theme) {


    super(context, theme);
    // TODO Auto-generated constructor stub
}

protected CustomDialog(Context context, boolean cancelable,
        OnCancelListener cancelListener) {
    super(context, cancelable, cancelListener);
    // TODO Auto-generated constructor stub
}

public CustomDialog(Context context) {
    super(context);

    // TODO Auto-generated constructor stub
}
//所有的方法执行完都会返回一个Builder使得后面可以直接create和show
 public static class Builder {
        private Context context;
        private String title;
        private String message;
        private String positiveButtonText;//确定按钮
        private String negativeButtonText;//取消按钮
        private View contentView;
        private BaseAdapter adapter;//listview的adapter
        //确定按钮事件
        private DialogInterface.OnClickListener positiveButtonClickListener;
        //取消按钮事件
        private DialogInterface.OnClickListener negativeButtonClickListener;
        //listview的item点击事件
        private AdapterView.OnItemClickListener listViewOnclickListener;

       public Builder(Context context) {
            this.context = context;
        }
         //设置消息
        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        /**
         *设置内容
         * 
         * @param title
         * @return
         */
        public Builder setMessage(int message) {
            this.message = (String) context.getText(message);
            return this;
        }

        /**
         * 设置标题
         * 
         * @param title
         * @return
         */
        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        /**
         *设置标题
         * 
         * @param title
         * @return
         */

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }
        //设置适配器
        public Builder setAdapter(BaseAdapter adapter) {
            this.adapter = adapter;
            return this;
        }
        //设置点击事件
        public Builder setOnClickListener(AdapterView.OnItemClickListener listViewOnclickListener) {
            this.listViewOnclickListener = listViewOnclickListener;
            return this;
        }
        //设置整个背景
        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }
        /**
         * 设置确定按钮和其点击事件
         * 
         * @param positiveButtonText
         * @return
         */
        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;
        }
     //createview方法
        public CustomDialog create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // 设置其风格
            final CustomDialog dialog = new CustomDialog(context,R.style.CustomProgressDialog);
            View layout = inflater.inflate(R.layout.dialog_custom_layout, null);
            dialog.addContentView(layout, new LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            // 设置标题
            ((TextView) layout.findViewById(R.id.title)).setText(title);
          //设置listview的adapter如果没有就隐藏listview
            if(adapter != null && adapter.getCount()>0){
                ListView listView = (ListView)layout.findViewById(R.id.listView);
                listView.setAdapter(adapter);
                if(listViewOnclickListener!=null){

                    listView.setOnItemClickListener(listViewOnclickListener);
                }

            }else{
                layout.findViewById(R.id.listView).setVisibility(
                        View.GONE);
            }


            //设置确定按钮
            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);
                                }
                            });
                }
            } else {
                 // 如果没有确定按钮就将其隐藏
                layout.findViewById(R.id.positiveButton).setVisibility(
                        View.GONE);
            }
            // 设置取消按钮
            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);
                                }
                            });
                }
            } else {
                // 如果没有取消按钮就将其隐藏
                layout.findViewById(R.id.negativeButton).setVisibility(
                        View.GONE);
            }
            // 设置内容
            if (message != null) {
                ((TextView) layout.findViewById(R.id.message)).setText(message);
            } else if (contentView != null) {
                // if no message set
                // 添加view
                ((LinearLayout) layout.findViewById(R.id.message))
                        .removeAllViews();
                ((LinearLayout) layout.findViewById(R.id.message)).addView(
                        contentView, new LayoutParams(
                                LayoutParams.WRAP_CONTENT,
                                LayoutParams.WRAP_CONTENT));
            }
            dialog.setContentView(layout);
            return dialog;
        }

    }
}