android自定义三个按钮的dialog对话框

显示效果如下图

android自定义三个按钮的dialog对话框

直接上代码:

1.自定义dialog

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.musicdo.musicshop.R;


/**
 * 订单评论图片拍照或相册选择dialog
 * Created by Administrator on 2017/9/7.
 */


public class DialogOrderEvaluateChooseImg extends Dialog {


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


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


    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 OnClickListener positiveButtonClickListener;
        private OnClickListener negativeButtonClickListener;
        private OnClickListener neutralButtonClickListener;


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


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


        /**
         * Set the Dialog message from resource
         *
         * @param message
         * @return
         */
        public DialogOrderEvaluateChooseImg.Builder setMessage(int message) {
            this.message = (String) context.getText(message);
            return this;
        }


        /**
         * Set the Dialog title from resource
         *
         * @param title
         * @return
         */
        public DialogOrderEvaluateChooseImg.Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }


        /**
         * Set the Dialog title from String
         *
         * @param title
         * @return
         */


        public DialogOrderEvaluateChooseImg.Builder setTitle(String title) {
            this.title = title;
            return this;
        }


        public DialogOrderEvaluateChooseImg.Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }


        /**
         * Set the positive button resource and it's listener
         *
         * @param positiveButtonText
         * @return
         */
        public DialogOrderEvaluateChooseImg.Builder setPositiveButton(int positiveButtonText,
                                                      OnClickListener listener) {
            this.positiveButtonText = (String) context.getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }


        public DialogOrderEvaluateChooseImg.Builder setPositiveButton(String positiveButtonText,
                                                      OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }


        public DialogOrderEvaluateChooseImg.Builder setNegativeButton(int negativeButtonText,
                                                      OnClickListener listener) {
            this.negativeButtonText = (String) context.getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }


        public DialogOrderEvaluateChooseImg.Builder setNegativeButton(String negativeButtonText,
                                                      OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }
        public DialogOrderEvaluateChooseImg.Builder setNeutralButton(String neutralButtonText,
                                                      OnClickListener listener) {
            this.neutralButtonText = neutralButtonText;
            this.neutralButtonClickListener = listener;
            return this;
        }


        public DialogOrderEvaluateChooseImg create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            final DialogOrderEvaluateChooseImg dialog = new DialogOrderEvaluateChooseImg(context, R.style.Dialog);
            View layout = inflater.inflate(R.layout.dialog_evaluate_choose_img, null);
            dialog.addContentView(layout,
                    new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 

LinearLayout.LayoutParams.WRAP_CONTENT));
            // set the dialog title
            // 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);
                                }
                            });
                }
            } 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);
            }
            // set the cancel 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_NEUTRAL);
                                }
                            });
                }
            } 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) {
                ((TextView) layout.findViewById(R.id.message)).setText(message);
            } else 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 LinearLayout.LayoutParams(LinearLayout.LayoutParams.

FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
            }
            dialog.setContentView(layout);


            // 按返回键是否取消
            dialog.setCancelable(false);
            dialog.setCanceledOnTouchOutside(false);


            return dialog;
        }
    }
}

2.布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/photo_full"
        android:layout_alignParentBottom="true"
        android:visibility="visible"
        android:layout_margin="20dp"
        android:background="@drawable/corner_blue"
        android:orientation="vertical" >


        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_margin="8dp"
            android:background="@color/white"
            android:layout_height="wrap_content">
            <Button
                android:id="@+id/positiveButton"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.1"
                android:textSize="16dp"
                android:background="@color/white"
                android:text="本地选择" />


            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:background="@color/navpage"></View>
            <Button
                android:id="@+id/neutralButton"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.1"
                android:background="@color/white"
                android:textSize="16dp"
                android:text="拍照上传" />
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:background="@color/navpage"></View>
            <Button
                android:id="@+id/negativeButton"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.1"
                android:textSize="16dp"
                android:background="@color/white"
                android:text="取消" />
        </LinearLayout>


    </LinearLayout>
</RelativeLayout>

3.调用

DialogOrderEvaluateChooseImg.Builder builder = new DialogOrderEvaluateChooseImg.Builder(context);
//                                builder.setMessage("账户在其他地方已登录");
//                                builder.setTitle("注意");
        builder.setPositiveButton("本地图片", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                Intent local = new Intent();
                local.setType("image/*");
                local.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(local, 2);
            }
        });
        builder.setNeutralButton("拍照上传", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(it, 1);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();