自定义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>
相关推荐
还鮟4 小时前
CTF Web的数组巧用
android
小蜜蜂嗡嗡5 小时前
Android Studio flutter项目运行、打包时间太长
android·flutter·android studio
aqi006 小时前
FFmpeg开发笔记(七十一)使用国产的QPlayer2实现双播放器观看视频
android·ffmpeg·音视频·流媒体
zhangphil7 小时前
Android理解onTrimMemory中ComponentCallbacks2的内存警戒水位线值
android
你过来啊你7 小时前
Android View的绘制原理详解
android
移动开发者1号10 小时前
使用 Android App Bundle 极致压缩应用体积
android·kotlin
移动开发者1号10 小时前
构建高可用线上性能监控体系:从原理到实战
android·kotlin
ii_best15 小时前
按键精灵支持安卓14、15系统,兼容64位环境开发辅助工具
android
美狐美颜sdk15 小时前
跨平台直播美颜SDK集成实录:Android/iOS如何适配贴纸功能
android·人工智能·ios·架构·音视频·美颜sdk·第三方美颜sdk
恋猫de小郭20 小时前
Meta 宣布加入 Kotlin 基金会,将为 Kotlin 和 Android 生态提供全新支持
android·开发语言·ios·kotlin