android_自定义弹出框

代码部分
public class CustomDialog extends Dialog {
    private View dialogContent;
    private TextView titleView;

    public View getDialogContent() {
        return dialogContent;
    }

    private CustomDialog(Context context, int theme) {
        super(context, theme);
    }

    public void setTitle(String title) {
        if (null != titleView)
            titleView.setText(title);
    }

    public static void showMessage(String message) {
        showMessage(message, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
    }

    public static void showMessage(String message, OnClickListener listener) {
        new CustomDialog.Builder(BaseActivity.currentActivity())
                .setTitle(Constant.STR_PROMPT)
                .setCancelable(true, null)
                .setMessage(message)
                .setPositiveButton(R.string.dialog_ok, listener).create().show();
    }

    public static class Builder {
        private Context context;
        private String title;
        private String message;
        private String positiveBtnText;
        private String negativeBtnText;
        private View contentView;
        boolean cancelable = false;
        OnCancelListener onCancelListener;
        OnDismissListener onDismissListener;

        private OnClickListener positiveBtnOnClickedListener;
        private OnClickListener negativeBtnOnClickedListener;

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

        public Builder setCancelable(boolean cancelable, OnCancelListener listener) {
            this.cancelable = cancelable;
            this.onCancelListener = listener;
            return this;
        }

        public Builder setOnDismissListener(OnDismissListener onDismissListener) {
            this.onDismissListener = onDismissListener;
            return this;
        }

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

        public Builder setTitle(int titleId) {
            this.title = (String) context.getText(titleId);
            return this;
        }

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

        public Builder setMessage(int messageId) {
            this.message = (String) context.getText(messageId);
            return this;
        }

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

        public Builder setContentView(int layoutId) {
            this.contentView = ((LayoutInflater) context.getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE)).inflate(layoutId, null);
            return this;
        }

        public Builder setPositiveButton(String text, OnClickListener listener) {
            this.positiveBtnText = text;
            this.positiveBtnOnClickedListener = listener;
            return this;
        }

        public Builder setPositiveButton(int textId, OnClickListener listener) {
            this.positiveBtnText = (String) context.getText(textId);
            this.positiveBtnOnClickedListener = listener;
            return this;
        }

        public Builder setNegativeButton(String text, OnClickListener listener) {
            this.negativeBtnText = text;
            this.negativeBtnOnClickedListener = listener;
            return this;
        }

        public Builder setNegativeButton(int textId, OnClickListener listener) {
            this.negativeBtnText = (String) context.getText(textId);
            this.negativeBtnOnClickedListener = listener;
            return this;
        }

        public CustomDialog create() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final CustomDialog dialog = new CustomDialog(context, R.style.CustomDialog);
            View dialogView = inflater.inflate(R.layout.custom_dialog, null);
            dialog.addContentView(dialogView, new LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.WRAP_CONTENT));
            TextView titleTv = (TextView) dialogView.findViewById(R.id.custom_dialog_title);
            dialog.titleView = titleTv;
            TextView messageTv = (TextView) dialogView.findViewById(R.id.custom_dialog_content_text);
            Button positiveBtn = (Button) dialogView.findViewById(R.id.custom_dialog_positive_button);
            Button negativeBtn = (Button) dialogView.findViewById(R.id.custom_dialog_negative_button);
            LinearLayout contentLayout = (LinearLayout) dialogView.findViewById(R.id.custom_dialog_content);

            dialog.setCancelable(cancelable);
            if (cancelable && null != onCancelListener)
                dialog.setOnCancelListener(onCancelListener);
            if ( null != onDismissListener)
                dialog.setOnDismissListener(onDismissListener);
            // 标题
            if (!TextUtils.isEmpty(title)) {
                titleTv.setVisibility(View.VISIBLE);
                dialogView.findViewById(R.id.custom_dialog_top_line).setVisibility(View.VISIBLE);
                titleTv.setText(title);
            } else {
                dialogView.findViewById(R.id.custom_dialog_top_line).setVisibility(View.INVISIBLE);
                titleTv.setVisibility(View.GONE);
            }

            // Message Or ContentView
            if (!TextUtils.isEmpty(message)) {
                messageTv.setText(message);
                dialog.dialogContent = messageTv;
            } else if (contentView != null) {
                contentLayout.removeAllViews();
                contentLayout.addView(contentView, new LayoutParams(LayoutParams.MATCH_PARENT,
                        LayoutParams.WRAP_CONTENT));
                dialog.dialogContent = contentView;
            }

            // PositiveButton or NegativeButton
            if (!TextUtils.isEmpty(positiveBtnText) || !TextUtils.isEmpty(negativeBtnText)) { // 有按钮
                dialogView.findViewById(R.id.custom_dialog_bottom_line).setVisibility(View.VISIBLE);
                if (!TextUtils.isEmpty(positiveBtnText)) {
                    positiveBtn.setVisibility(View.VISIBLE);
                    positiveBtn.setText(positiveBtnText);
                    if (positiveBtnOnClickedListener != null) {
                        positiveBtn.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                positiveBtnOnClickedListener.onClick(dialog, Dialog.BUTTON_POSITIVE);
                            }
                        });
                    }
                } else {
                    positiveBtn.setVisibility(View.GONE);
                }
                if (!TextUtils.isEmpty(negativeBtnText)) {
                    negativeBtn.setVisibility(View.VISIBLE);
                    negativeBtn.setText(negativeBtnText);
                    if (negativeBtnOnClickedListener != null) {
                        negativeBtn.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                negativeBtnOnClickedListener.onClick(dialog, Dialog.BUTTON_NEGATIVE);
                            }
                        });
                    }
                } else {
                    negativeBtn.setVisibility(View.GONE);
                }
                if (!(!TextUtils.isEmpty(positiveBtnText) && !(TextUtils.isEmpty(negativeBtnText)))) {
                    dialogView.findViewById(R.id.custom_dialog_button_line).setVisibility(View.GONE);
                } else {
                    dialogView.findViewById(R.id.custom_dialog_button_line).setVisibility(View.VISIBLE);
                }
            } else { // 无按钮
                dialogView.findViewById(R.id.custom_dialog_button_line).setVisibility(View.GONE);
                dialogView.findViewById(R.id.custom_dialog_bottom_line).setVisibility(View.INVISIBLE);
                dialogView.findViewById(R.id.custom_dialog_button_layout).setVisibility(View.GONE);
            }
            return dialog;
        }

    }
}
布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   style="@style/CustomDialogBackground"
    android:minWidth="@dimen/min_width_dialog" >

    <TextView
        android:id="@+id/custom_dialog_title"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:textColor="@android:color/black"
        android:textSize="@dimen/title_small"
        android:gravity="center" />

    <View
        android:id="@+id/custom_dialog_top_line"
        android:layout_width="match_parent"
        android:layout_height="@dimen/sigle_pexls"
        android:background="@color/gray" />

    <LinearLayout
        android:id="@+id/custom_dialog_content"
        android:layout_width="wrap_content"
        android:layout_height="0px"
        android:gravity="center"
        android:padding="@dimen/medium"
        android:minHeight="@dimen/min_height_dialog_content"
        android:layout_weight="1">

        <TextView
            android:id="@+id/custom_dialog_content_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxWidth="240dp"
            android:layout_gravity="center_vertical"
            android:textColor="@color/font_black"
            android:textSize="@dimen/text_noraml"/>

    </LinearLayout>

    <View
        android:id="@+id/custom_dialog_bottom_line"
        android:layout_width="match_parent"
        android:layout_height="@dimen/sigle_pexls"
        android:background="@color/gray" />

    <LinearLayout
        android:id="@+id/custom_dialog_button_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/custom_dialog_positive_button"
            style="@style/CustomDialogButton"/>

        <View
            android:id="@+id/custom_dialog_button_line"
            android:layout_width="@dimen/sigle_pexls"
            android:layout_height="match_parent"
            android:background="@color/gray"/>

        <Button
            android:id="@+id/custom_dialog_negative_button"
            style="@style/CustomDialogButton"/>
    </LinearLayout>

</LinearLayout>
样式

<style name="CustomDialogBackground">

        <item name="android:layout_width">wrap_content</item>

        <item name="android:layout_height">wrap_content</item>

        <item name="android:background">@drawable/rectangle_write</item>

        <item name="android:gravity">center</item>

        <item name="android:orientation">vertical</item>

    </style>


你可能感兴趣的:(android_自定义弹出框)