自定义弹窗,dialog

package com.ecjia.mydialog;

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

import com.ecmoban.android.huodj.R;

/**
 * Created by Jongey on 2018/5/22 0022.
 */

public class CustomDialog extends Dialog {

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

    public static class Builder {
        private CharSequence message;
        private String title;
        private View contentView;
        private String positiveButtonText;
        private String negativeButtonText;
        private String singleButtonText;
        private View.OnClickListener positiveButtonClickListener;
        private View.OnClickListener negativeButtonClickListener;
        private View.OnClickListener singleButtonClickListener;

        private View layout;
        private CustomDialog dialog;
        public Builder(Context context) {
            //这里传入自定义的style,直接影响此Dialog的显示效果。style具体实现见style.xml
            dialog = new CustomDialog(context, R.style.dialog_update);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            layout = inflater.inflate(R.layout.dialog_layout, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        }

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

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

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

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

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

        public Builder setSingleButton(String singleButtonText, View.OnClickListener listener) {
            this.singleButtonText = singleButtonText;
            this.singleButtonClickListener = listener;
            return this;
        }

        /**
         * 创建单按钮对话框
         * @return
         */
//        public CustomDialog createSingleButtonDialog() {
//            showSingleButton();
//            layout.findViewById(R.id.singleButton).setOnClickListener(singleButtonClickListener);
//            //如果传入的按钮文字为空,则使用默认的返回//            if (singleButtonText != null) {
//                ((Button) layout.findViewById(R.id.singleButton)).setText(singleButtonText);
//            } else {
//                ((Button) layout.findViewById(R.id.singleButton)).setText("返回");
//            }
//            create();
//            return dialog;
//        }

        /**
         * 创建双按钮对话框
         * @return
         */
        public CustomDialog createTwoButtonDialog() {
            showTwoButton();
            layout.findViewById(R.id.yes).setOnClickListener(positiveButtonClickListener);
            layout.findViewById(R.id.no).setOnClickListener(negativeButtonClickListener);
            //如果传入的按钮文字为空,则使用默认的            if (positiveButtonText != null) {
                ((TextView) layout.findViewById(R.id.yes)).setText(positiveButtonText);
            } else {
                ((TextView) layout.findViewById(R.id.yes)).setText("确定");
            }
            if (negativeButtonText != null) {
                ((TextView) layout.findViewById(R.id.no)).setText(negativeButtonText);
            } else {
                ((TextView) layout.findViewById(R.id.no)).setText("取消");
            }
            create();
            return dialog;
        }

        /**
         * 单按钮对话框和双按钮对话框的公共部分在这里设置
         */
        private void create() {

            if(title != null){
                ((TextView) layout.findViewById(R.id.dialog_title)).setText(title);
            }
            if (message != null) {      //设置提示内容
                ((TextView) layout.findViewById(R.id.dialog_message)).setText(message);
            } else if (contentView != null) {       //如果使用BuildersetContentview()方法传入了布局,则使用传入的布局
                ((LinearLayout) layout.findViewById(R.id.content)).removeAllViews();
                ((LinearLayout) layout.findViewById(R.id.content))
                        .addView(contentView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            }
            dialog.setContentView(layout);
            dialog.setCancelable(true);     //用户可以点击手机Back键取消对话框显示
            dialog.setCanceledOnTouchOutside(false);        //用户不能通过点击对话框之外的地方取消对话框显示
        }

        /**
         * 显示双按钮布局,隐藏单按钮
         */
        private void showTwoButton() {
           // layout.findViewById(R.id.singleButtonLayout).setVisibility(View.GONE);
            layout.findViewById(R.id.update_cancel).setVisibility(View.VISIBLE);
        }

        /**
         * 显示单按钮布局,隐藏双按钮
         */
//        private void showSingleButton() {
//            layout.findViewById(R.id.singleButtonLayout).setVisibility(View.VISIBLE);
//            layout.findViewById(R.id.twoButtonLayout).setVisibility(View.GONE);
//        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值