自定义Android 应用对话框组件 - CustomDialog

自定义对话框组件 - CustomDialog

概述

CustomDialog 是一个可复用的对话框组件,可以轻松地集成到 Android 应用中。该组件允许开发者快速创建带有自定义标题、内容和按钮的对话框。此外,它还提供了回调接口,使得在对话框关闭时能够执行特定的操作。

特点
  • 自定义布局:通过 XML 布局文件自定义对话框的外观。
  • 灵活的回调:通过回调接口控制对话框关闭后的行为。
  • 简单易用:只需几行代码即可创建并显示对话框。
使用方法
  1. 添加依赖 如果您使用的是 Android Studio,可以在项目的 build.gradle 文件中添加必要的依赖(如果需要的话)。

  2. 创建对话框实例 在您的 Activity 或 Fragment 中,通过以下方式创建一个 CustomDialog 实例:

    java 复制代码
    1CustomDialog customDialog = CustomDialog.newInstance("示例消息");
  3. 设置监听器 设置一个监听器以处理对话框关闭时的行为:

    java 复制代码
    1customDialog.setOnDialogListener(new CustomDialog.OnDialogListener() {
    2    @Override
    3    public void onDialogDismissed() {
    4        // 对话框关闭时执行的操作
    5    }
    6});
  4. 显示对话框 显示对话框:

    java 复制代码
    1customDialog.show(getSupportFragmentManager(), CustomDialog.class.getSimpleName());
示例代码
java 复制代码
1import android.app.Dialog;
2import android.os.Bundle;
3import androidx.annotation.NonNull;
4import androidx.fragment.app.DialogFragment;
5import android.view.LayoutInflater;
6import android.view.View;
7import android.view.ViewGroup;
8import android.widget.Button;
9import android.widget.ImageView;
10import android.widget.TextView;
11
12public class CustomDialog extends DialogFragment implements View.OnClickListener {
13
14    private final static String TAG = "CustomDialog";
15    private String message;
16    private OnDialogListener listener;
17
18    public interface OnDialogListener {
19        void onDialogDismissed();
20    }
21
22    public void setOnDialogListener(OnDialogListener listener) {
23        this.listener = listener;
24    }
25
26    public CustomDialog() {
27        // Required empty public constructor
28    }
29
30    public static CustomDialog newInstance(String message) {
31        CustomDialog fragment = new CustomDialog();
32        Bundle args = new Bundle();
33        args.putString("message", message);
34        fragment.setArguments(args);
35        return fragment;
36    }
37
38    @Override
39    public void onCreate(Bundle savedInstanceState) {
40        super.onCreate(savedInstanceState);
41        if (getArguments() != null) {
42            message = getArguments().getString("message");
43        }
44    }
45
46    @NonNull
47    @Override
48    public Dialog onCreateDialog(Bundle savedInstanceState) {
49        LayoutInflater inflater = requireActivity().getLayoutInflater();
50        View view = inflater.inflate(R.layout.dialog_custom, null);
51
52        TextView textTitle = view.findViewById(R.id.tv_goods_text);
53        textTitle.setText(message);
54
55        Button buttonOk = view.findViewById(R.id.button_ok);
56        buttonOk.setOnClickListener(this);
57
58        // Assuming there is an ImageView with the id iv_close
59        ImageView imageViewClose = view.findViewById(R.id.iv_close);
60        imageViewClose.setOnClickListener(this);
61
62        return new Dialog(requireContext(), R.style.CustomDialogTheme);
63    }
64
65    @Override
66    public void onClick(View v) {
67        if (v.getId() == R.id.iv_close || v.getId() == R.id.button_ok) {
68            dismiss();
69            if (listener != null) {
70                listener.onDialogDismissed();
71            }
72        }
73    }
74
75    @Override
76    public void dismiss() {
77        super.dismiss();
78        Log.i(TAG, "Dialog dismissed");
79    }
80}
81
82// 在 Activity 或 Fragment 中使用
83CustomDialog customDialog = CustomDialog.newInstance("示例消息");
84customDialog.setOnDialogListener(new CustomDialog.OnDialogListener() {
85    @Override
86    public void onDialogDismissed() {
87        // 对话框关闭时执行的操作
88    }
89});
90customDialog.show(getSupportFragmentManager(), CustomDialog.class.getSimpleName());
XML 布局文件 dialog_custom.xml
XML 复制代码
1<?xml version="1.0" encoding="utf-8"?>
2<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3    android:id="@+id/ll_custom_dialog"
4    android:layout_width="match_parent"
5    android:layout_height="match_parent"
6    android:background="@drawable/dialog_custom_set"
7    android:orientation="vertical"
8    android:padding="16dp">
9
10    <ImageView
11        android:id="@+id/iv_goods_img"
12        android:layout_width="240dp"
13        android:layout_height="240dp"
14        android:layout_alignParentTop="true"
15        android:layout_centerHorizontal="true"
16        android:layout_marginTop="58dp"
17        android:background="@drawable/main_right_goods"
18        android:src="@drawable/goods_01" />
19
20    <TextView
21        android:id="@+id/tv_goods_text"
22        android:layout_width="wrap_content"
23        android:layout_height="42dp"
24        android:layout_below="@id/iv_goods_img"
25        android:layout_centerHorizontal="true"
26        android:layout_marginTop="16dp"
27        android:maxWidth="240dp"
28        android:text="商品名称"
29        android:textColor="#ff0a2640"
30        android:textSize="30sp" />
31
32    <Button
33        android:id="@+id/button_ok"
34        android:layout_width="458dp"
35        android:layout_height="72dp"
36        android:layout_alignParentBottom="true"
37        android:layout_centerHorizontal="true"
38        android:layout_marginBottom="75dp"
39        android:background="@drawable/dialog_close"
40        android:text="关闭"
41        android:textColor="#FFFFFFFF"
42        android:textSize="30sp" />
43
44</RelativeLayout>
定义样式

你需要在 styles.xml 文件中定义一个自定义的主题:

XML 复制代码
1<style name="CustomDialogTheme" parent="Theme.AppCompat.Light.Dialog">
2    <!-- 自定义对话框的样式 -->
3</style>
相关推荐
隐-梵1 小时前
Android studio学习之路(六)--真机的调试以及多媒体照相的使用
android·学习·android studio
stevenzqzq1 小时前
Android Studio Logcat V2 使用指南(适配 2024 年版本)
android·ide·android studio
bytebeats1 小时前
改进 Jetpack Compose 中的 ModalBottomSheet API
android
bytebeats1 小时前
使用Dagger SPI 查找非必要组件依赖项
android·gradle·dagger
bytebeats1 小时前
在Kotlin中编写依赖于时间的可测试协程代码
android·kotlin·测试
_一条咸鱼_2 小时前
AI 大模型之 Transformer 架构深入剖析
android
QING6182 小时前
Kotlin 中 reified 配合 inline 不再被类型擦除蒙蔽双眼
android·kotlin·app
Yang-Never2 小时前
OpenGL ES -> SurfaceView + EGL实现立方体纹理贴图+透视效果
android·kotlin·android studio·贴图
QING6182 小时前
Android应用启动与退出监听方案——新手入门指南
android·架构·app
叫我龙翔2 小时前
【项目日记】高并发服务器项目总结
android·运维·服务器