Android 自定义弹窗 Dialog

自定义Dialog,弹出提示。总的来说就是 extends Dialog类。

下面列出一些Dialog的基本属性:

1 AlertDialog.Builder属性
* setTitle:    为对话框设置标题 ;
* setIcon :    为对话框设置图标;
* setMessage:   为对话框设置内容;
* setView :    给对话框设置自定义样式 ;
* setItems:    设置对话框要显示的一个list,一般用于显示几个命令时;
* setMultiChoiceItems:用来设置对话框显示一系列的复选框;
* setNeutralButton : 响应中立行为的点击;
* setPositiveButton : 响应Yes/Ok的点击 ;
* setNegativeButton :响应No/Cancel的点击 ;
* create :   创建对话框 ;
* show :    显示对话框;
2 ProgressDialog属性
*setProgressStyle:    设置进度条风格,风格为圆形,旋转的;
*setTitlt:        设置ProgressDialog 标题;
*setMessage:         设置ProgressDialog提示信息;
*setIcon:        设置ProgressDialog标题图标;
*setIndeterminate:     设置ProgressDialog 的进度条是否不明确;
*setCancelable:         设置ProgressDialog 是否可以按返回键取消;
*setButton:             设置ProgressDialog 的一个Button(需要监听Button事件);
*show:                  显示ProgressDialog。
如果了解了Dialog的基本属性。我们在自定义一个Dialog。呈现出我们自己的弹窗
public class HintToast extends Dialog {

    public HintToast(Context context) {
        super(context);
    }

    public HintToast(Context context, int theme) {
        super(context, theme);
    }

    protected HintToast(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder {
        private Context mContext;
        private View mView;
        private String mMessage;
        private String positiveButtonText;   // 确定按钮
        private String negativeButtonText;    // 取消
        private DialogInterface.OnClickListener positiveButtonClickListener;   // 确定监听
        private DialogInterface.OnClickListener negativeButtonClickListener;


        public Builder(Context context) {
            this.mContext = context;
        }

        public Builder setMessage(String message) {
            this.mMessage = message;
            return this;
        }

        public Builder setContentView(View view) {
            this.mView = view;
            return this;
        }

        // 设置监听(确定)
        public Builder setPositiveButton(String string, DialogInterface.OnClickListener listener) {
            this.positiveButtonText = string;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String string, DialogInterface.OnClickListener listener) {
            this.negativeButtonText = string;
            this.negativeButtonClickListener = listener;
            return this;
        }

        public HintToast create() {
            LayoutInflater infalter = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = infalter.inflate(R.layout.mimicam_toast, null);
            final HintToast dialog = new HintToast(mContext, R.style.Dialog);
            // 是ViewGroup 下的LayoutParans  包
            LayoutParams layoutparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            dialog.addContentView(layout, layoutparams);
            // z设置确定 按钮
            Button ok = (Button) layout.findViewById(R.id.id_toast_ok);
            if (null != positiveButtonText) {
                ok.setText(positiveButtonText);
                if (null != positiveButtonClickListener) {
                    ok.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                        }
                    });
                }
            } else {
                ok.setVisibility(View.GONE);
            }

            //设置取消 按钮
            Button no = (Button) layout.findViewById(R.id.id_toast_no);
            if (null != negativeButtonText) {
                no.setText(negativeButtonText);
                if (null != negativeButtonClickListener) {
                    no.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                        }
                    });
                }
            } else {
                no.setVisibility(View.GONE);
            }

            // 设置消息
            if (null != mMessage) {
                TextView message = (TextView) layout.findViewById(R.id.id_toast_message);
                message.setText(mMessage);
            } else if (null != mView) {
                LinearLayout linear = (LinearLayout) layout.findViewById(R.id.layout_content);
                linear.removeAllViews();
                linear.addView(mView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

            }
            dialog.setContentView(layout);
            return dialog;
        }

    }
}
下面是我们自定义的XML 布局。



    

        


        
    

    

        
引用的背景Drawable xml  文件是:


    
    
        
    
    
    
 
简单的两张图片而已。


你可能感兴趣的:(Android)