Android 简单通用Dialog

744 阅读3分钟

自定义dialog几乎是每个程序比不可少的一项工作,github上也有很多这样的开源库,功能很是强大,但是有时候需要修改就比较麻烦,这里提供一种简洁、比较常用的Dialog供大家使用。

CommonDialog 源代码如下:

public class CommonDialog extends Dialog {

    private LinearLayout llRoot;
    private LinearLayout llDefault;
    private AppCompatTextView tvMessage;
    private AppCompatTextView tvNegative, tvPositive;
    private View horizontalLine,verticalLine;
    private FrameLayout flCustomer;
    private View customerView;

    private String message = "";
    private int messageColor = -1;
    private float messageSize = -1;
    private String positive = "";
    private int positiveColor = -1;
    private float positiveSize = -1;
    private String negative = "";
    private int negativeColor = -1;
    private float negativeSize = -1;
    private int backgroundResId = -1;
    private int verticalLineColor = -1;
    private int horizontalLineColor = -1;
    private boolean isSingleButton = false;

    private View.OnClickListener onDefaultClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            cancel();
        }
    };
    private View.OnClickListener onPositiveListener = onDefaultClickListener;
    private View.OnClickListener onNegativeListener = onDefaultClickListener;

    public CommonDialog(Context context) {
        super(context, R.style.CommonDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_common);
        initView();
    }

    private void initView(){
        llRoot = findViewById(R.id.llRoot);
        llDefault = findViewById(R.id.llDefault);
        flCustomer = findViewById(R.id.flCustomer);
        tvMessage = findViewById(R.id.tvMessage);
        tvNegative = findViewById(R.id.tvNegative);
        tvPositive = findViewById(R.id.tvPositive);
        horizontalLine = findViewById(R.id.horizontalLine);
        verticalLine = findViewById(R.id.verticalLine);
    }

    @Override
    public void show() {
        super.show();
        show(this);
    }

    private void show(CommonDialog dialog){

        if(dialog.backgroundResId != -1){
            dialog.llRoot.setBackgroundResource(dialog.backgroundResId);
        }

        if(dialog.customerView != null){
            dialog.flCustomer.addView(dialog.customerView);
            dialog.flCustomer.setVisibility(View.VISIBLE);
            dialog.llDefault.setVisibility(View.GONE);
        }else {
            dialog.flCustomer.setVisibility(View.GONE);
            dialog.llDefault.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(dialog.message)) {
                dialog.tvMessage.setText(dialog.message);
            }
            if (dialog.messageColor != -1) {
                dialog.tvMessage.setTextColor(dialog.messageColor);
            }
            if (dialog.messageSize != -1) {
                dialog.tvMessage.setTextSize(dialog.messageSize);
            }
            if (!TextUtils.isEmpty(dialog.negative)) {
                dialog.tvNegative.setText(dialog.negative);
            } else {
                dialog.tvNegative.setText(getContext().getString(R.string.dialog_cancel));
            }
            if (dialog.negativeColor != -1) {
                dialog.tvNegative.setTextColor(dialog.negativeColor);
            }
            if (dialog.negativeSize != -1) {
                dialog.tvNegative.setTextSize(dialog.negativeSize);
            }
            if (!TextUtils.isEmpty(dialog.positive)) {
                dialog.tvPositive.setText(dialog.positive);
            } else {
                dialog.tvPositive.setText(getContext().getString(R.string.dialog_sure));
            }
            if (dialog.positiveColor != -1) {
                dialog.tvPositive.setTextColor(dialog.positiveColor);
            }
            if (dialog.positiveSize != -1) {
                dialog.tvPositive.setTextSize(dialog.positiveSize);
            }
            if(horizontalLineColor != -1){
                dialog.horizontalLine.setBackgroundColor(horizontalLineColor);
            }
            if(verticalLineColor != -1){
                dialog.verticalLine.setBackgroundColor(verticalLineColor);
            }
            if (dialog.isSingleButton) {
                tvPositive.setOnClickListener(dialog.onPositiveListener);
                verticalLine.setVisibility(View.GONE);
                tvNegative.setVisibility(View.GONE);
            } else {
                tvNegative.setOnClickListener(dialog.onNegativeListener);
                tvPositive.setOnClickListener(dialog.onPositiveListener);
                verticalLine.setVisibility(View.VISIBLE);
                tvNegative.setVisibility(View.VISIBLE);
            }

        }
    }

    public static class Builder {

        private CommonDialog commonDialog;

        public Builder(Context context) {
            commonDialog = new CommonDialog(context);
        }

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

        public Builder setMessageColor(@ColorRes int color){
            commonDialog.messageColor = color;
            return this;
        }

        public Builder setMessageSize(float size){
            commonDialog.messageSize = size;
            return this;
        }

        public Builder setNegative(View.OnClickListener onClickListener){
            commonDialog.onNegativeListener = onClickListener;
            return this;
        }

        public Builder setNegative(String negative,View.OnClickListener onClickListener){
            commonDialog.negative = negative;
            commonDialog.onNegativeListener = onClickListener;
            return this;
        }

        public Builder setNegativeColor(@ColorRes int color){
            commonDialog.negativeColor = color;
            return this;
        }

        public Builder setNegativeSize(float size){
            commonDialog.negativeSize = size;
            return this;
        }

        public Builder setPositive(View.OnClickListener onClickListener){
            commonDialog.onPositiveListener = onClickListener;
            return this;
        }

        public Builder setPositive(String positive,View.OnClickListener onClickListener){
            commonDialog.positive = positive;
            commonDialog.onPositiveListener = onClickListener;
            return this;
        }

        public Builder setPostiveColor(@ColorRes int color){
            commonDialog.positiveColor = color;
            return this;
        }

        public Builder setPostiveSize(float size){
            commonDialog.positiveSize = size;
            return this;
        }

        public Builder setCustomerView(View view){
            commonDialog.customerView = view;
            return this;
        }

        public Builder setSingleButton(boolean isSingleButton, View.OnClickListener onClickListener){
            commonDialog.isSingleButton = isSingleButton;
            commonDialog.onPositiveListener = onClickListener;
            return this;
        }

        public Builder setSingleButton(boolean isSingleButton,String positive, View.OnClickListener onClickListener){
            commonDialog.isSingleButton = isSingleButton;
            commonDialog.positive = positive;
            commonDialog.onPositiveListener = onClickListener;
            return this;
        }

        public Builder setVerticalLineColor(@ColorRes int resId){
            commonDialog.verticalLineColor = resId;
            return this;
        }

        public Builder setHorizontalLineColor(@ColorRes int resId){
            commonDialog.horizontalLineColor = resId;
            return this;
        }

        public Builder setBackgroundDrawable(@DrawableRes int backgroundResId){
            commonDialog.backgroundResId = backgroundResId;
            return this;
        }

        public Builder setCancelable(boolean cancelable) {
            commonDialog.setCancelable(cancelable);
            return this;
        }

        public Builder setCanceledOnTouchOutside(boolean canceledOnTouchOutside) {
            commonDialog.setCanceledOnTouchOutside(canceledOnTouchOutside);
            return this;
        }

        public CommonDialog create() {
            return commonDialog;
        }

    }
}

默认布局dialog_common.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/llRoot"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_centerInParent="true"
    android:orientation="vertical"
    android:background="@drawable/background_common_dialog_default">
    <FrameLayout
        android:id="@+id/flCustomer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"/>
    <LinearLayout
        android:id="@+id/llDefault"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/tvMessage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="@dimen/size_100dip"
            android:padding="@dimen/size_16dip"
            android:gravity="center"
            android:textSize="@dimen/sp_16" />
        <View
            android:id="@+id/horizontalLine"
            android:layout_width="match_parent"
            android:layout_height="@dimen/size_1dip"
            android:background="@color/color_E1E4EA"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/size_50dip">
            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/tvNegative"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:textSize="@dimen/sp_16"
                tools:text="@string/dialog_cancel"/>
            <View
                android:id="@+id/verticalLine"
                android:layout_width="@dimen/size_1dip"
                android:layout_height="match_parent"
                android:background="@color/color_E1E4EA"/>
            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/tvPositive"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:textSize="@dimen/sp_16"
                tools:text="@string/dialog_sure"/>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

dialog默认样式CommonDialog如下:

<style name="CommonDialog" parent="android:style/Theme.Dialog">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:backgroundDimEnabled">true</item>
</style>

dialog默认背景background_common_dialog_default.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:width="@dimen/size_1dip"
        android:color="@color/white"/>
    <solid
        android:color="@color/white"/>
    <corners
        android:radius="@dimen/size_10dip"/>
</shape>

CommonDialog 支持Message文本、字体、字号设置;支持底部按钮单个、两个显示设置;支持底部按钮文本、字体字号设置;支持背景样式替换;支持自定义布局等,使用方法如下:

new CommonDialog.Builder(MainActivity.this)
        .setMessage("Dialog message")
        .setMessageSize(ScreenUtils.dp2px(20))
        .setMessageColor(R.color.colorPrimary)
        .setSingleButton(true,  new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        })
        .setHorizontalLineColor(R.color.colorPrimary)
        .setBackgroundDrawable(R.drawable.background_common_dialog_default)
        .setCancelable(true)
        .setCanceledOnTouchOutside(true)
        .create()
        .show();;