Android自定义对话框

由于Android系统的碎片化,导致对话框的样式在每款手机上都不尽相同,基于这个原因和UI的需求,在项目中自己定义了一个对话框,提供基本的样式设定,效果如图

这里写图片描述

我们定义一个CustomerDialog继承自Dialog,并对外暴露设置属性的相关方法,类代码如下.

package com.qfang.androidclient.widgets.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qfang.qfangmobilecore.R;

/**
 * 自定义Dialog,提供设置标题,内容,确定,取消点击事件
 * 采用Builder构造模式创建,下边是代码示例
 * <p/>a
 * CustomerDialog.Builder builder = new CustomerDialog.Builder(this);
 * CustomerDialog dialog = builder.setTitle("标题").setMessage("内容").setPositiveButton("确定", new DialogInterface.OnClickListener() {
 *
 * @Override public void onClick(DialogInterface dialog, int which) {
 * dialog.dismiss();
 * }
 * }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
 * @Override public void onClick(DialogInterface dialog, int which) {
 * dialog.dismiss();
 * }
 * }).setPositiveButtonTextColor(getResources().getColor(R.color.orange_ff9933))
 * .create();
 * dialog.setCanceledOnTouchOutside(true);
 * dialog.show();
 * <p>
 * created by shidong
 * modified by shidong 4.6.0
 */
public class CustomerDialog extends Dialog {

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

    public CustomerDialog(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 View contentView;
        private OnClickListener positiveButtonClickListener;
        private OnClickListener negativeButtonClickListener;
        private int positiveButtonTextColor;
        private int negativeButtonTextColor;
        private boolean isCenter;

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

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

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

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

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

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

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

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

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

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

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

        public Builder setPositiveButtonTextColor(int color) {
            this.positiveButtonTextColor = color;
            return this;
        }

        public Builder setNegativeButtonTextColor(int color) {
            this.negativeButtonTextColor = color;
            return this;
        }

        /**
         * 设置Message居中
         *
         * @param isCenter
         * @return
         */
        public Builder setMessageCenter(boolean isCenter) {
            this.isCenter = isCenter;
            return this;
        }

        public CustomerDialog create() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            final CustomerDialog dialog = new CustomerDialog(context, R.style.ExitDialog);
            View layout = inflater.inflate(R.layout.layout_normal_dialog, null);
            dialog.addContentView(layout, new LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

            // 设置标题
            ((TextView) layout.findViewById(R.id.tv_title)).setText(title);
            // 设置确定按钮
            if (positiveButtonText != null) {
                ((TextView) layout.findViewById(R.id.tv_positive))
                        .setText(positiveButtonText);
                if (positiveButtonClickListener != null) {
                    layout.findViewById(R.id.tv_positive)
                            .setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    positiveButtonClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_POSITIVE);
                                }
                            });
                }
                if (positiveButtonTextColor != 0) {
                    ((TextView) layout.findViewById(R.id.tv_positive))
                            .setTextColor(positiveButtonTextColor);
                }
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.tv_positive).setVisibility(View.GONE);
                layout.findViewById(R.id.view_vertical_line).setVisibility(View.GONE);
            }
            // 设置取消按钮
            if (negativeButtonText != null) {
                ((TextView) layout.findViewById(R.id.tv_negative))
                        .setText(negativeButtonText);
                if (negativeButtonClickListener != null) {
                    layout.findViewById(R.id.tv_negative)
                            .setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    negativeButtonClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_NEGATIVE);
                                }
                            });
                }
                if (negativeButtonTextColor != 0) {
                    ((TextView) layout.findViewById(R.id.tv_negative))
                            .setTextColor(negativeButtonTextColor);
                }
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.tv_negative).setVisibility(View.GONE);
                layout.findViewById(R.id.view_vertical_line).setVisibility(View.GONE);
            }
            // set the content message
            if (message != null) {
                ((TextView) layout.findViewById(R.id.tv_message)).setText(message);
                if (isCenter) {
                    ((TextView) layout.findViewById(R.id.tv_message)).setGravity(Gravity.CENTER);
                }
            } 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 LayoutParams(
                            LayoutParams.MATCH_PARENT,
                            LayoutParams.MATCH_PARENT));
                } else {
                    layout.findViewById(R.id.content).setVisibility(View.GONE);
                }
            }
            dialog.setContentView(layout);
            return dialog;
        }
    }
}

下载Demo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值