分享一个自定义Dialog

开门见代码:

public final class CommonDialog extends Dialog {

    /*** 内部回调 */
    private interface InnerCallback {

        void onLeftButtonClicked();

        void onRightButtonClicked();
    }

    /*** 按钮监听器: 默认实现关闭弹窗 */
    public static class OnButtonClickedListener implements InnerCallback {

        private CommonDialog mDialog;

        public OnButtonClickedListener(CommonDialog dialog) {
            this.mDialog = dialog;
        }

        @Override
        public void onLeftButtonClicked() {
            UserInterfaceHelper.dismiss(mDialog);
            mDialog = null;
        }

        @Override
        public void onRightButtonClicked() {
            UserInterfaceHelper.dismiss(mDialog);
            mDialog = null;
        }
    }

    public Context mContext;

    public TextView mButtonLeft;

    public TextView mButtonRight;

    public TextView tv_title;

    public TextView tv_content;

    public LinearLayout btnLayout;

    public LinearLayout oneBtnLayout;

    public TextView mButtonOne;

    /*** 左按钮文字 */
    private
    @Nullable
    CharSequence mLeftBtnText;

    /*** 右按钮文字 */
    private
    @Nullable
    CharSequence mRightBtnText;

    /*** 按钮点击监听器 */
    private
    @Nullable
    InnerCallback mOnButtonClickedListener;

    public CommonDialog(Context context) {
        super(context);
        mContext = context;
        initView();
    }

    public void initView() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.layout_common_dialog);
        Window window = getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mButtonLeft = findViewById(R.id.tvLeftBtn);
        mButtonRight = findViewById(R.id.tvRightBtn);
        tv_title = findViewById(R.id.tvTitle);
        tv_content = findViewById(R.id.tvContent);
        btnLayout = findViewById(R.id.ll_two_buttons);
        oneBtnLayout = findViewById(R.id.ll_one_button);
        mButtonOne = findViewById(R.id.button_only_one);
        setCanceledOnTouchOutside(false);
    }

    /*** 设置 标题 */
    @Deprecated
    public CommonDialog setCustomTitle(@StringRes int resId) {
        return setCustomTitle(getString(resId));
    }

    /*** 设置 标题 */
    public CommonDialog setCustomTitle(CharSequence title) {
        if (tv_title != null) {
            tv_title.setText(title);
            tv_title.setVisibility(View.VISIBLE);
        }
        return this;
    }

    /*** 设置下标题字体大小 */
    public CommonDialog setTitleMargins(int left, int top, int right, int bottom) {
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) tv_title.getLayoutParams();
        if (params != null) {
            params.setMargins(left, top, right, bottom);
        }
        return this;
    }

    /*** 设置下标题字体大小 */
    public CommonDialog setCustomTitleSize(int unit, float size) {
        if (tv_title != null) {
            tv_title.setTextSize(unit, size);
        }
        return this;
    }

    /*** 设置下内容字体大小 */
    public CommonDialog setContentTextSize(int unit, float size) {
        if (tv_content != null) {
            tv_content.setTextSize(unit, size);
        }
        return this;
    }

    /**
     * 设置下内容区文字方向
     *
     * @param gravity
     *
     * @return
     */
    public CommonDialog setContentGravity(int gravity) {
        if (tv_content != null) {
            tv_content.setGravity(gravity);
        }
        return this;
    }


    /**
     * 设置内容区内部Spannable文字可响应点击
     *
     * @return
     */
    public CommonDialog setContentSpanableClick() {
        if (tv_content != null) {
            tv_content.setMovementMethod(LinkMovementMethod.getInstance());
            tv_content.setHighlightColor(Color.TRANSPARENT);
        }
        return this;
    }

    /*** 设置标题加粗 */
    public CommonDialog setTitleBoldStyle() {
        if (tv_title != null) {
            TextPaint paint = tv_title.getPaint();
            paint.setFakeBoldText(true);
        }
        return this;
    }

    /*** 设置右按钮加粗 */
    public CommonDialog setRightButtoneBoldStyle() {
        if (mButtonRight != null) {
            TextPaint paint = mButtonRight.getPaint();
            paint.setFakeBoldText(true);
        }
        return this;
    }

    /*** 设置 内容 */
    public CommonDialog setCustomContent(@StringRes int resId) {
        return setCustomContent(getString(resId));
    }

    /*** 设置 内容 传null不显示*/
    public CommonDialog setCustomContent(CharSequence content) {
        if (tv_content != null && !TextUtils.isEmpty(content)) {
            tv_content.setText(content);
            tv_content.setVisibility(View.VISIBLE);
        } else if (TextUtils.isEmpty(content)) {
            tv_content.setVisibility(View.GONE);
        }
        return this;
    }

    /*** 设置 左按钮文字 */
    public CommonDialog setLeftButton(@StringRes int resId) {
        return setLeftButton(getString(resId));
    }

    /*** 设置 左按钮文字 */
    public CommonDialog setLeftButton(String text) {
        mLeftBtnText = text;
        return this;
    }

    /*** 设置底部Button的大小 */
    public CommonDialog setButtonSize(int unit, float size) {
        if (mButtonLeft != null && mButtonRight != null) {
            mButtonLeft.setTextSize(unit, size);
            mButtonRight.setTextSize(unit, size);
        }
        if (mButtonOne != null) {
            mButtonOne.setTextSize(unit, size);
        }
        return this;
    }

    /*** 设置 左按钮文字颜色 */
    public CommonDialog setLeftButtonColor(@ColorRes int colorId) {
        mButtonLeft.setTextColor(getContext().getResources().getColor(colorId));
        return this;
    }

    /*** 设置 有按钮文字颜色 */
    public CommonDialog setRightButtonColor(@ColorRes int colorId) {
        mButtonRight.setTextColor(getContext().getResources().getColor(colorId));
        return this;
    }

    /*** 设置 右按钮文字 */
    public CommonDialog setRightButton(@StringRes int resId) {
        return setRightButton(getString(resId));
    }

    /*** 设置 右按钮文字 */
    public CommonDialog setRightButton(String text) {
        mRightBtnText = text;
        return this;
    }

    /*** 设置 按钮点击监听器 */
    public CommonDialog setOnButtonClickedListener(InnerCallback l) {
        mOnButtonClickedListener = l;
        return this;
    }

    /*** 设置 对话框 可取消 */
    public CommonDialog setDialogCancelable(boolean flag) {
        setCancelable(flag);
        return this;
    }

    /**
     * 设置左边button是否可用
     */
    public void setLeftButtonEnable(boolean enable) {
        mButtonLeft.setEnabled(enable);
    }

    /**
     * 设置右边Button是否可用
     */
    public void setRightButtonEnable(boolean enable) {
        mButtonRight.setEnabled(enable);
    }

    /**
     * 设置底部唯一Button是否可用
     */
    public void setOneButtonEnable(boolean enable) {
        mButtonOne.setEnabled(enable);
    }

    /*** 设置 对话框 可点击外部取消 */
    public CommonDialog setDialogCanceledOnTouchOutside(boolean flag) {
        setCanceledOnTouchOutside(flag);
        return this;
    }

    /*** 设置 对话框 对齐方式是否为横屏 */
    @Deprecated
    public CommonDialog setDialogOritention(boolean isLandspace) {
        return this;
    }

    /*** setXX() 之后,最后调用 */
    public boolean build() {
        if (getContext() instanceof Activity) {
            Activity act = (Activity) getContext();
            if (act.isFinishing()) {
                return false;
            }
        }
        return onBuild();
    }

    private boolean onBuild() {
        if (!TextUtils.isEmpty(mLeftBtnText) && !TextUtils.isEmpty(mRightBtnText)) {
            btnLayout.setVisibility(View.VISIBLE);
            oneBtnLayout.setVisibility(View.GONE);
            mButtonLeft.setText(mLeftBtnText);
            mButtonLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnButtonClickedListener != null) {
                        mOnButtonClickedListener.onLeftButtonClicked();
                    }
                }
            });
            mButtonRight.setText(mRightBtnText);
            mButtonRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnButtonClickedListener != null) {
                        mOnButtonClickedListener.onRightButtonClicked();
                    }
                }
            });
        } else {
            if (!TextUtils.isEmpty(mRightBtnText)) {
                btnLayout.setVisibility(View.GONE);
                oneBtnLayout.setVisibility(View.VISIBLE);
                mButtonOne.setText(mRightBtnText);
                mButtonOne.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (mOnButtonClickedListener != null) {
                            mOnButtonClickedListener.onRightButtonClicked();
                        }
                    }
                });
            }
        }
        show();
        return true;
    }

    private
    @NonNull
    String getString(@StringRes int resId) {
        return getContext().getString(resId);
    }
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/dialogLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/shape_rectangle_c_ffffff_r_6"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="@dimen/dp_15"
        android:layout_marginRight="@dimen/dp_15"
        android:layout_marginTop="@dimen/dp_21"
        android:gravity="center"
        android:textColor="@android:color/black"
        android:textSize="@dimen/font_30"
        android:textStyle="bold"/>

    <TextView
        android:id="@+id/tvContent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvTitle"
        android:paddingLeft="@dimen/dp_15"
        android:paddingRight="@dimen/dp_15"
        android:paddingTop="@dimen/dp_11"
        android:paddingBottom="@dimen/dp_5"
        android:gravity="center"
        android:maxHeight="@dimen/dp_420"
        android:textColor="@color/skin_color_text_primary"
        android:textSize="@dimen/font_20"/>

    <LinearLayout
        android:id="@+id/btnLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tvContent"
        android:orientation="vertical">

        <View
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_0.5"
            android:background="@color/base_color_DFE1E5"/>

        <LinearLayout
            android:id="@+id/ll_two_buttons"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_42">

            <FrameLayout
                android:id="@+id/leftBtnFrame"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1.0">

                <TextView
                    android:id="@+id/tvLeftBtn"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:drawablePadding="@dimen/dp_5"
                    android:gravity="center"
                    android:textColor="@color/color_767676"
                    android:textSize="@dimen/font_32"/>
            </FrameLayout>

            <View
                android:id="@+id/dividerVertical"
                android:layout_width="@dimen/dp_0.5"
                android:layout_height="match_parent"
                android:background="@color/base_color_DFE1E5"/>

            <FrameLayout
                android:id="@+id/rightBtnFrame"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1.0">

                <TextView
                    android:id="@+id/tvRightBtn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:textColor="@color/color_v60_mgtv"
                    android:textSize="@dimen/font_32"/>
            </FrameLayout>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll_one_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:visibility="gone">

            <TextView
                android:id="@+id/button_only_one"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_44"
                android:clickable="true"
                android:gravity="center"
                android:textColor="@color/color_v60_mgtv"
                android:textSize="@dimen/font_32"/>
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

使用:

mDialog = new CommonDialog(getActivity());
        String title = "xxxx";
        mConfirmDialog.setCustomTitle(title).setLeftButton(R.string
                .download_cancel).setCustomContent(sb).setRightButton(R.string.xxx)
                .setDialogCanceledOnTouchOutside(false)
                .setOnButtonClickedListener(new CommonDialog.OnButtonClickedListener(mConfirmDialog) {
                    @Override
                    public void onLeftButtonClicked() {
                        super.onLeftButtonClicked();
                    }

                    @Override
                    public void onRightButtonClicked() {
                        super.onRightButtonClicked();
                        }
                    }

                });
        mDialog.build();




 mUserDialog.setCustomTitle(xxxxx)).setCustomTitleSize(TypedValue.COMPLEX_UNIT_PX,
                    getResources()
                            .getDimension(R.dimen.font_34)).setTitleBoldStyle().setCustomContent(spanableBuilder)
                    .setContentTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.font_26))
                    .setContentGravity(Gravity.START).setLeftButton(R.string.xxx).setRightButton(R.string.xxx)
                    .setRightButtonColor(R.color.xxx).setRightButtoneBoldStyle().setDialogCanceledOnTouchOutside(false)
                    .setButtonSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.font_30))
                    .setContentSpanableClick().setOnButtonClickedListener(new CommonDialog.OnButtonClickedListener(mUserDialog) {
                @Override
                public void onLeftButtonClicked() {
                    // stay here,no dismiss.
                }

                @Override
                public void onRightButtonClicked() {
                    super.onRightButtonClicked();
                }
            });
            mUserDialog.setCancelable(false);
            mUserDialog.build();

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值