package com.demo.app.ui.dialog; import android.app.Activity; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.graphics.Color; import android.text.method.LinkMovementMethod; import android.view.LayoutInflater; import android.view.View; import android.widget.FrameLayout; import android.widget.TextView; import com.demo.app.R; import com.demo.app.ui.common.SimpleBrowserActivity; import com.demo.lib.common.util.SystemCommon; import com.demo.lib.common.util.common.StringUtils; import com.demo.lib.common.util.ui.TextViewUtil; /** * 用户协议弹框 */ public class UserProtocolDialog extends Dialog { private static String TAG = "UserProtocolDialog"; public UserProtocolDialog(Context context) { super(context); this.setCancelable(false); } public UserProtocolDialog(Context context, int theme) { super(context, theme); this.setCancelable(false); } public static class Builder { public final static int TEXT_ALIGIN_LEFT = 1; public final static int TEXT_ALIGIN_MIDDLE = 2; public final static int TEXT_ALIGIN_RIGHT = 3; private Activity context; String webviewData; String title; private String positiveButtonText; private String negativeButtonText; private OnClickListener positiveButtonClickListener; private OnClickListener negativeButtonClickListener; public Builder(Activity context) { this.context = context; } public Builder setData(String data) { this.webviewData = data; return this; } public Builder setTitle(String title) { this.title = title; 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 UserProtocolDialog create() { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); final UserProtocolDialog dialog = new UserProtocolDialog(context, R.style.Dialog); dialog.setCanceledOnTouchOutside(false); final View layout = inflater.inflate(R.layout.dialog_user_protocol, null); dialog.addContentView(layout, new FrameLayout.LayoutParams( FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT)); TextView tv_title = layout.findViewById(R.id.tv_title); TextView tv_content = layout.findViewById(R.id.tv_content); tv_content.setText(TextViewUtil.getClickableHtml(webviewData, context, Color.rgb(23,106,230), SimpleBrowserActivity.class)); tv_content.setMovementMethod(LinkMovementMethod.getInstance()); tv_content.setClickable(false); TextView tv_negative = layout.findViewById(R.id.tv_negative); TextView tv_positive = layout.findViewById(R.id.tv_positive); if (!StringUtils.isEmpty(title)) tv_title.setText(title); // set the confirm button if (positiveButtonClickListener != null) { if (!StringUtils.isEmpty(positiveButtonText)) tv_positive.setText(positiveButtonText); tv_positive.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE); } }); } if (negativeButtonClickListener != null) { if (!StringUtils.isEmpty(negativeButtonText)) tv_negative.setText(negativeButtonText); tv_negative.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE); } }); } dialog.setContentView(layout); android.view.WindowManager.LayoutParams params = dialog.getWindow() .getAttributes(); params.width = (int) ((SystemCommon.getScreenWidth(context) * 3) / 4); params.height = android.view.WindowManager.LayoutParams.WRAP_CONTENT; dialog.getWindow().setAttributes(params); return dialog; } } public interface MeasureCallBack { void onMeasure(int height); } }