//自定义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) { //如果使用Builder的setContentview()方法传入了布局,则使用传入的布局 ((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(); } });