Android 封装AlertDialog工具类 自定义宽度、圆角、边距

AlertDialog 改变宽高、圆角核心方法

 Window dialogWindow = dialog.getWindow();
            //// 给 DecorView 设置背景颜色,很重要,不然导致 Dialog 内容
显示不全,有一部分内容会充当 padding,上面例子有举出
            dialogWindow.getDecorView().setBackgroundResource(R.drawable.shape_bg_confirm_button_radius_16);
            WindowManager.LayoutParams params = dialogWindow.getAttributes();
            params.width = (int) (display.getWidth() * 0.85f);
            dialogWindow.setAttributes(params);
            //把 DecorView 的默认 padding 取消,同时 DecorView 的默认大小也会取消
            dialogWindow.getDecorView().setPadding(0, 0, 0, 0);

封装工具类 all code

/**
 * 名称:退出断续弹框
 * Created by niudong on 2021/7/26 
 */
public class ExitDialogUtils {
private AppCompatActivity context;
private View[] textViews = null;
private AlertDialog dialog;
private OnDialogResultListener onDialogResultListener;

public static ExitDialogUtils getInstance() {
    return InnerDialogClass.holder;
}
private ExitDialogUtils() {
}
/**
 *
 */
public void showConfirmExitDialog(String exitTips, AppCompatActivity mActivity, OnDialogResultListener dialogResultListener) {
    context = mActivity;
    if (null == context || context.isDestroyed() || context.isFinishing()) {
        return;
    }
    
    onDialogResultListener = dialogResultListener;
    if (null == dialog) {
        textViews = new View[4];
        View mView = LayoutInflater.from(context).inflate(R.layout.dialog_pay_back_confirm_hint_user_view, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .setCancelable(false);
        dialog = builder.setView(mView).create();
        if (!TextUtils.isEmpty(exitTips)) {
            ((TextView) mView.findViewById(R.id.pay_title_hint_detail)).setText(exitTips);
        }
        //文本复制
        textViews[0] = mView.findViewById(R.id.pay_selected_1);
        textViews[1] = mView.findViewById(R.id.pay_selected_2);
        textViews[2] = mView.findViewById(R.id.pay_selected_3);
        textViews[3] = mView.findViewById(R.id.pay_selected_4);
        mView.findViewById(R.id.pay_selected_1).setOnClickListener(onClickListener);
        mView.findViewById(R.id.pay_selected_2).setOnClickListener(onClickListener);
        mView.findViewById(R.id.pay_selected_3).setOnClickListener(onClickListener);
        mView.findViewById(R.id.pay_selected_4).setOnClickListener(onClickListener);
        //时间View
        if (null != onDialogResultListener) {
            onDialogResultListener.initTimeView(((TextView) mView.findViewById(R.id.pay_title_hint_detail)));
        }
        mView.findViewById(R.id.pay_back_confirm_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        mView.findViewById(R.id.pay_back_confirm_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                String selectCurrIds = getSelectCurrIds();
                if (null != onDialogResultListener) {
                    onDialogResultListener.exitCall(selectCurrIds);
                }
            }
        });
        dialog.show();
        try {
            if (null != dialog.getWindow()) {
                Window dialogWindow = dialog.getWindow();
                //// 给 DecorView 设置背景颜色,很重要,不然导致 Dialog 内容显示不全,有一部分内容会充当 padding,上面例子有举出
                dialogWindow.getDecorView().setBackgroundResource(R.drawable.shape_bg_confirm_button_radius_16);
                WindowManager.LayoutParams params = dialogWindow.getAttributes();
                params.width = (int) (ScreenUtils.getDisplayWidth() * 0.85f);
                dialogWindow.setAttributes(params);
                //把 DecorView 的默认 padding 取消,同时 DecorView 的默认大小也会取消
                dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        if (!dialog.isShowing()) {
            dialog.show();
        }
    }
}

private View.OnClickListener onClickListener = view -> {
    resetStatus();
    view.setSelected(true);
};

private void resetStatus() {
    if (null != textViews)
        for (int i = 0; i < textViews.length; i++) {
            if (textViews[i].isSelected()) {
                textViews[i].setSelected(false);
            }
        }
}

private String getSelectCurrIds() {
    String result = "5";
    if (null != textViews)
        for (int i = 0; i < textViews.length; i++) {
            if (textViews[i].isSelected()) {
                result = String.valueOf(i + 1);
                break;
            }
        }
    return result;
}

public void cleanDilog() {
    try {
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
        if (null != context) {
            context = null;
        }
        if (null != onDialogResultListener) {
            onDialogResultListener = null;
        }
        if (null != textViews) {
            textViews = null;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public boolean isShowDialog() {
    return null != dialog && dialog.isShowing();
}

private static final class InnerDialogClass {
    private final static ExitDialogUtils holder = new ExitDialogUtils();
}

public interface OnDialogResultListener {
    void initTimeView(TextView textTimeView);

    void exitCall(String data);
}
}
在使用到的地方调用
    PayExitDialogUtils.getInstance().showConfirmExitDialog("文本信息", this, new PayExitDialogUtils.OnDialogResultListener() {
        @Override
        public void initTimeView(TextView textTime) {
            textTimeView = textTime;
            textTimeView.setText("可变文本");
        }

        @Override
        public void exitCall(String data) {
            finish();
        }
    });
Page onDestroy
    PayExitDialogUtils.getInstance().cleanDilog();

你可能感兴趣的:(Android 封装AlertDialog工具类 自定义宽度、圆角、边距)