Android 自定义Dialog

话不多说,直接上代码哈~

1、Dialog工具类
/**
 * @Author Catherine
 * @Description Custom common dialog.
 * @Date 2021/5/27
 **/
public class CustomCommonDialog extends Dialog {

    /**
     * 显示的标题
     */
    private TextView dialog_tv_title;

    /**
     * 显示的图片
     */
    private ImageView dialog_iv_image;

    /**
     * 显示的内容
     */
    private TextView dialog_tv_content;

    /**
     * 确认和取消按钮
     */
    private Button dialog_btn_negative, dialog_btn_positive;

    /**
     * 按钮之间的分割线
     */
    private View dialog_view_column_line;

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

    /**
     * 都是内容数据
     */
    private String title;
    private String content;
    private String btnPositiveStr, btnNegativeStr;
    private int imageResId = -1;

    /**
     * 底部是否只有一个按钮
     */
    private boolean isSingle = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.layout_custom_common_dialog);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);
        //初始化界面控件
        initView();
        //初始化界面数据
        refreshView();
        //初始化界面控件的事件
        initEvent();
    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {

        //设置确定按钮被点击后,向外界提供监听
        dialog_btn_positive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickBottomListener != null) {
                    onClickBottomListener.onPositiveClick();
                }
            }
        });

        //设置取消按钮被点击后,向外界提供监听
        dialog_btn_negative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (onClickBottomListener != null) {
                    onClickBottomListener.onNegativeClick();
                }
            }
        });
    }

    /**
     * 初始化界面控件的显示数据
     */
    private void refreshView() {
        //如果用户自定了title和message
        if (!TextUtils.isEmpty(title)) {
            dialog_tv_title.setText(title);
            dialog_tv_title.setVisibility(View.VISIBLE);
        } else {
            dialog_tv_title.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(content)) {
            dialog_tv_content.setText(content);
        }

        //如果设置按钮的文字
        if (!TextUtils.isEmpty(btnPositiveStr)) {
            dialog_btn_positive.setText(btnPositiveStr);
        } else {
            dialog_btn_positive.setText("确定");
        }
        if (!TextUtils.isEmpty(btnNegativeStr)) {
            dialog_btn_negative.setText(btnNegativeStr);
        } else {
            dialog_btn_negative.setText("取消");
        }

        if (imageResId != -1) {
            dialog_iv_image.setImageResource(imageResId);
            dialog_iv_image.setVisibility(View.VISIBLE);
        } else {
            dialog_iv_image.setVisibility(View.GONE);
        }

        /**
         * 只显示一个按钮的时候隐藏取消按钮,回掉只执行确定的事件
         */
        if (isSingle) {
            dialog_view_column_line.setVisibility(View.GONE);
            dialog_btn_negative.setVisibility(View.GONE);
        } else {
            dialog_btn_negative.setVisibility(View.VISIBLE);
            dialog_view_column_line.setVisibility(View.VISIBLE);
        }
    }

    @Override

    public void show() {
        super.show();
        refreshView();
    }

    /**
     * 初始化界面控件
     */
    private void initView() {
        dialog_btn_negative = (Button) findViewById(R.id.dialog_btn_negative);
        dialog_btn_positive = (Button) findViewById(R.id.dialog_btn_positive);
        dialog_tv_title = (TextView) findViewById(R.id.dialog_tv_title);
        dialog_tv_content = (TextView) findViewById(R.id.dialog_tv_content);
        dialog_iv_image = (ImageView) findViewById(R.id.dialog_iv_image);
        dialog_view_column_line = findViewById(R.id.dialog_view_column_line);
    }

    /**
     * 设置确定取消按钮的回调
     */
    public OnClickBottomListener onClickBottomListener;

    public CustomCommonDialog setOnClickBottomListener(OnClickBottomListener onClickBottomListener) {
        this.onClickBottomListener = onClickBottomListener;
        return this;
    }

    public interface OnClickBottomListener {

        /**
         * 点击确定按钮事件
         */
        void onPositiveClick();

        /**
         * 点击取消按钮事件
         */
        void onNegativeClick();
    }

    public String getContent() {
        return content;
    }

    public CustomCommonDialog setContent(String content) {
        this.content = content;
        return this;
    }

    public String getTitle() {
        return title;
    }

    public CustomCommonDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    public String getBtnPositiveStr() {
        return btnPositiveStr;
    }

    public CustomCommonDialog setBtnPositiveStr(String btnPositiveStr) {
        this.btnPositiveStr = btnPositiveStr;
        return this;
    }

    public String getBtnNegativeStr() {
        return btnNegativeStr;
    }

    public CustomCommonDialog setBtnNegativeStr(String btnNegativeStr) {
        this.btnNegativeStr = btnNegativeStr;
        return this;
    }

    public int getImageResId() {
        return imageResId;
    }

    public boolean isSingle() {
        return isSingle;
    }

    public CustomCommonDialog setSingle(boolean single) {
        isSingle = single;
        return this;
    }

    public CustomCommonDialog setImageResId(int imageResId) {
        this.imageResId = imageResId;
        return this;
    }
}

2、layout_custom_common_dialog.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:background="@drawable/shape_rectangle_white"
    android:orientation="vertical"
    android:paddingTop="16dp">

    <TextView
        android:id="@+id/dialog_tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="10dp"
        android:gravity="center"
        android:textColor="#333333"
        android:textSize="18sp"
        android:visibility="visible"
        tools:text="提示" />

    <ImageView
        android:id="@+id/dialog_iv_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="10dp"
        android:maxWidth="150dp"
        android:maxHeight="150dp"
        android:visibility="visible"
        tools:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/dialog_tv_content"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:gravity="center|left"
        android:lineSpacingExtra="3dp"
        android:textColor="#666666"
        android:textSize="15sp"
        tools:text="提示内容" />

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:layout_marginTop="16dp"
        android:background="#999999" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/dialog_btn_negative"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@null"
            android:gravity="center"
            android:paddingTop="16dp"
            android:paddingBottom="16dp"
            android:singleLine="true"
            android:textColor="#999999"
            android:textSize="16sp"
            tools:text="No" />

        <View
            android:id="@+id/dialog_view_column_line"
            android:layout_width="0.5dp"
            android:layout_height="match_parent"
            android:background="#999999" />

        <Button
            android:id="@+id/dialog_btn_positive"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@null"
            android:gravity="center"
            android:paddingTop="16dp"
            android:paddingBottom="16dp"
            android:singleLine="true"
            android:textColor="#38ADFF"
            android:textSize="16sp"
            tools:text="Yes" />

    </LinearLayout>
</LinearLayout>
3、CustomDialog —— style.xml
    <style name="CustomDialog" parent="DialogTheme">
        <!--背景颜色和透明度-->
        <item name="android:windowBackground">@color/transparent</item>
        <!--是否去除标题-->
        <item name="android:windowNoTitle">true</item>
        <!--是否去除边框-->
        <item name="android:windowFrame">@null</item>
        <!--是否悬浮在activity之上-->
        <item name="android:windowIsFloating">true</item>
        <!--是否模糊-->
        <item name="android:backgroundDimEnabled">true</item>
    </style>
4、@drawable/shape_rectangle_white
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:width="32dp" android:height="32dp">
        <shape android:shape="rectangle">
            <solid android:color="#fff" />
            <corners android:radius="12dp" />
        </shape>
    </item>
</selector>
5、在Activity里使用
    //初始化并弹出对话框方法
    private void showDialog() {
        CustomCommonDialog dialog = new CustomCommonDialog(this).setTitle("提示")
                .setContent("悬浮窗权限将用于保持教师和学生间的通话。若不同意,将会影响部分功能的正常使用。\n请问是否同意我们向您获取该权限?")
        .setBtnPositiveStr("同意并前往开启").setBtnNegativeStr("下次再说");
        dialog.create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
        dialog.setOnClickBottomListener(new CustomCommonDialog.OnClickBottomListener() {
            @Override
            public void onPositiveClick() {
                if (checkIsHuaweiRom()) {
                    HuaweiFloatingPermissionUtils.applyPermission(MainActivity.this);
                } else if (checkIsMiuiRom()) {
                    MiuiFloatingPermissionUtils.applyMiuiPermission(MainActivity.this);
                } else if (checkIs360Rom()) {
                    QikuFloatingPermissionUtils.applyPermission(MainActivity.this);
                } else if (checkIsMeizuRom()) {
                    MeizuFloatingPermissionUtils.applyPermission(MainActivity.this);
                } else if (checkIsOppoRom()) {
                    OppoFloatingPermissionUtils.applyPermission(MainActivity.this);
                } else {
                    requestAlertWindowPermission();
                }
                dialog.dismiss();
            }

            @Override
            public void onNegativeClick() {

            }
        });
     dialog.show();
     //此处设置位置窗体大小,设置为手机屏幕宽度的5/6,注意一定要在show方法调用后再写设置窗口大小的代码,否则不起效果
     dialog.getWindow().setLayout((ScreenUtils.getScreenWidth(this) / 6 * 5), LinearLayout.LayoutParams.WRAP_CONTENT);
    }

Android 自定义Dialog

上一篇:快速排序


下一篇:Java-面向对象特征-多态