万能dialog弹窗。



//自定义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.ImageView;
import android.widget.RelativeLayout;

import com.ecmoban.android.huodj.R;
import com.nostra13.universalimageloader.core.ImageLoader;

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

public class CustomDialogHuodong extends Dialog {

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

    public static class Builder {

        private String backPictureUrl;
        private String deleteImageUrl;
        private View contentView;
        private String nextshopImageurl;
        private String immidateImageurl;
        private String singleButtonText;
        private View.OnClickListener nextShopButtonClickListener;
        private View.OnClickListener immediateButtonClickListener;
        private View.OnClickListener singleButtonClickListener;
        private View.OnClickListener deleteButtonClickListener;
        private View.OnClickListener jxsgoodsButtonClickListener;

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


        /**
         * 背景大图
         * @param backPictureUrl
         * @return
         */
        public Builder setBackPicture(String backPictureUrl,View.OnClickListener listener) {
            this.backPictureUrl = backPictureUrl;
            this.jxsgoodsButtonClickListener = listener;
            return this;
        }

        /**
         * 关闭按钮图标
         * @param deleteImageUrl
         * @return
         */
        public Builder setDeletePicBtn(String deleteImageUrl,View.OnClickListener listener) {
            this.deleteImageUrl = deleteImageUrl;
            this.deleteButtonClickListener = listener;
            return this;
        }

        /**
         * 加载视图
         * @param v
         * @return
         */
        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        /**
         * 下次再说按钮
         * @param nextshopImageurl
         * @param listener
         * @return
         */
        public Builder setNextShopButton(String nextshopImageurl, View.OnClickListener listener) {
            this.nextshopImageurl = nextshopImageurl;
            this.nextShopButtonClickListener = listener;
            return this;
        }

        /**
         * 继续购买按钮
         * @param immidateImageurl
         * @param listener
         * @return
         */
        public Builder setImmediateButton(String immidateImageurl, View.OnClickListener listener) {
            this.immidateImageurl = immidateImageurl;
            this.immediateButtonClickListener = listener;
            return this;
        }

        /**
         * 单按钮对话框
         * @param singleButtonText
         * @param listener
         * @return
         */
        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 CustomDialogHuodong createTwoButtonDialog() {
            showTwoButton();
            layout.findViewById(R.id.next_shop).setOnClickListener(nextShopButtonClickListener);
            layout.findViewById(R.id.goumai_shop).setOnClickListener(immediateButtonClickListener);
            //如果传入的按钮文字为空,则使用默认的            if (nextshopImageurl == null) {
                ((ImageView) layout.findViewById(R.id.next_shop)).setVisibility(View.GONE);
            }
            if (immidateImageurl == null) {
                ((ImageView) layout.findViewById(R.id.goumai_shop)).setVisibility(View.GONE);
            }
            if (nextshopImageurl != null) {
                ImageLoader.getInstance().displayImage(nextshopImageurl, (ImageView) layout.findViewById(R.id.next_shop));
            } else {

                ((ImageView) layout.findViewById(R.id.next_shop)).setImageResource(R.drawable.default_image);
            }

            if (immidateImageurl != null) {
                ImageLoader.getInstance().displayImage(immidateImageurl, (ImageView) layout.findViewById(R.id.goumai_shop));
            } else {
                ((ImageView) layout.findViewById(R.id.goumai_shop)).setImageResource(R.drawable.default_image);
            }
            create();
            return dialog;
        }

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

            layout.findViewById(R.id.back_jxs_tanchuang).setOnClickListener(jxsgoodsButtonClickListener);
            layout.findViewById(R.id.image_delete).setOnClickListener(deleteButtonClickListener);

            if(backPictureUrl != null){
                ImageLoader.getInstance().displayImage(backPictureUrl, (ImageView) layout.findViewById(R.id.back_jxs_tanchuang));
            }else {
                ((ImageView) layout.findViewById(R.id.back_jxs_tanchuang)).setImageResource(R.drawable.default_image);
            }
            if (deleteImageUrl != null) {      //设置提示内容
                ((ImageView) layout.findViewById(R.id.image_delete)).setImageResource(R.drawable.deletex);
            } else if (contentView != null) {       //如果使用BuildersetContentview()方法传入了布局,则使用传入的布局
                ((RelativeLayout) layout.findViewById(R.id.countdown_img)).removeAllViews();
                ((RelativeLayout) layout.findViewById(R.id.countdown_img))
                        .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.btn_lin).setVisibility(View.VISIBLE);
        }

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

    }
}


//创建方法

private CustomDialogHuodong.Builder builder;
private CustomDialogHuodong mDialog;

private void showHuoDongDialog(String backpicurl, String nextshopurl, String immdateurl, String deleteurl, View.OnClickListener backListener, View.OnClickListener nextshopListener,View.OnClickListener immdateListener,View.OnClickListener deleteListener) {

    builder = new CustomDialogHuodong.Builder(ECJiaMainActivity.this);
    mDialog = builder.setBackPicture(backpicurl,backListener)
            .setDeletePicBtn(deleteurl,deleteListener)
            .setNextShopButton(nextshopurl, nextshopListener)
            .setImmediateButton(immdateurl, immdateListener)
            .createTwoButtonDialog();
    mDialog.show();
}
//使用

showHuoDongDialog(pic_url, null, null, url_to_vip,
        new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (url_to_vip!=null){
                    ECJiaOpenType.getDefault().startAction(ECJiaMainActivity.this, url_to_vip);
                    mDialog.dismiss();
                }
            }
        },null,null,
        new OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });



从底部弹出的dialog。位置你可以在base里自己改。使用方法都有。public class BaseDialog extends Dialog { private View mContentView; public Context mContext; public LayoutInflater mInflater; public BaseDialog(Context context) { this(context, R.style.BaseDialogStyle); mContext = context; } public BaseDialog(Context context, int themeResId) { super(context, themeResId); mContext = context; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.dialog_base); Window dialogWindow = getWindow(); WindowManager.LayoutParams lp1 = dialogWindow.getAttributes(); WindowManager wm = (WindowManager) getContext() .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); int width = outMetrics.widthPixels; lp1.width = width; dialogWindow.setGravity(Gravity.BOTTOM); dialogWindow.setAttributes(lp1); mInflater = LayoutInflater.from(mContext); } public void setContentView(int layoutResID) { mContentView = View.inflate(mContext, layoutResID, null); LinearLayout view = (LinearLayout) findViewById(R.id.base_container); view.addView(mContentView); } public View getContextView() { return mContentView; } } public class CustomDialog extends BaseDialog implements View.OnClickListener { /** * 标题 */ private TextView mTitleTv; /** * 确定按钮 */ private Button mPositiveBt; /** * 取消按钮 */ private Button mNegativeBt; /** * 确定取消中间分割线 */ private View mDiverView; /** * 内容容器 */ private LinearLayout mContentLin; private View.OnClickListener mPositiveClickListener; private View.OnClickListener mNegativeClickListener; private List<String> mContents; private String mContent; private String mTilte; private String mPositiveText; private String mNegativeText; private CustomDialog(Context context) { super(context); } private CustomDialog(Context context, int themeResId) { super(context, themeResId); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dialog_one_button); mTitleTv = (TextView) findViewById(R.id.title); mContentLin = (LinearLayout) findViewById(R.id.container); mPositiveBt = (Button) findViewById(R.id.positiveButton); mNegativeBt = (Button) findViewById(R.id.negativeButton); mDiverView = findViewById(R.id.diver); mPositiveBt.setOnClickListener(this); mNegativeBt.setOnClickListener(this); setContent(mContent); setContents(); setTitle(); setButtonClickListener(); setNegativeBtText(); setPositiveBtText(); } private void setContents() { if (mContents != null && mContents.size() != 0) { if (mContents.size() == 1) { setContent(mContents.get(0)); } else { for (int i = 0; i < mContents.size(); i++) { View view = mInflater.inflate(R.layout.diaolog_content_item_left, null); TextView tv = (TextView) view.findViewById(R.id.tv); tv.setText(mContents.get(i)); mContentLin.addView(view); } } } } private void setContent(String content) { if (!TextUtils.isEmpty(content)) { View view = mInflater.inflate(R.layout.diaolog_content_item_center, null); TextView tv = (TextView) view.findViewById(R.id.tv); tv.setText(content); mContentLin.addView(view); } } /** * 设置标题 */ private void setTitle() { if (!TextUtils.isEmpty(mTilte)) { mTitleTv.setVisibility(View.VISIBLE); mTitleTv.setText(mTilte); } } /** * 设置右侧按钮的显示文字,默认为确定 */ private void setPositiveBtText() { if (!TextUtils.isEmpty(mPositiveText)) { mPositiveBt.setText(mPositiveText); } } /** * 设置左侧按钮的显示文字,默认为取消 */ private void setNegativeBtText() { if (!TextUtils.isEmpty(mNegativeText)) { mNegativeBt.setText(mNegativeText); } } /** * 设置监听 可不设置 */ private void setButtonClickListener() { if (mPositiveClickListener != null) { mPositiveBt.setVisibility(View.VISIBLE); } if (mNegativeClickListener != null) { mNegativeBt.setVisibility(View.VISIBLE); } if (mNegativeClickListener != null && mPositiveClickListener != null) { mDiverView.setVisibility(View.VISIBLE); } } public static class Builder { private CustomDialog dialog; public Builder(Context context) { dialog = new CustomDialog(context); } public Builder(Context context, int themeResId) { dialog = new CustomDialog(context, themeResId); } /** * 通过Builder类创建dialog */ public CustomDialog create() { return dialog; } /** * 多条提示 */ public Builder setContent(List<String> contents) { dialog.mContents = contents; return this; } /** * 单条提示 */ public Builder setContent(String content) { dialog.mContent = content; return this; } /** * 设置标题 */ public Builder setTitle(String tilte) { dialog.mTilte = tilte; return this; } /** * 设置右侧按钮的显示文字,默认为确定 * * @param positiveText 按钮上的显示内容 */ public Builder setPositiveBtText(String positiveText) { dialog.mPositiveText = positiveText; return this; } /** * 设置左侧按钮的显示文字,默认为取消 * * @param negativeText 按钮上的显示内容 */ public Builder setNegativeBtText(String negativeText) { dialog.mNegativeText = negativeText; return this; } ; /** * 设置监听 可不设置 * * @param positiveClickListener 确定按钮 * @param negativeClickListener 确定取消按钮 */ public Builder setButtonClickListener(View.OnClickListener positiveClickListener, View.OnClickListener negativeClickListener) { dialog.mPositiveClickListener = positiveClickListener; dialog.mNegativeClickListener = negativeClickListener; return this; } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.positiveButton: if (mPositiveClickListener != null) { mPositiveClickListener.onClick(v); dismiss(); } break; case R.id.negativeButton: if (mNegativeClickListener != null) { mNegativeClickListener.onClick(v); dismiss(); } break; } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值