自定义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>
相关推荐
YueTann31 分钟前
大模型入门3:理解LLAMA
android·llama
&岁月不待人&1 小时前
Android 通过相机和系统相册获取图片,压缩,结果回调
android·数码相机
前端小菜鸟也有人起2 小时前
ElementUI大坑Notification修改样式
android·前端·elementui
嵌入式成长家5 小时前
[rk3399 android11]收到广播执行相应的adb命令
android·adb·广播
电手5 小时前
Chrome远程桌面安卓版怎么使用?
android·远程连接·远程控制
我命由我123455 小时前
Kotlin 极简小抄 P2(插值表达式、运算符、选择结构赋值)
android·java·开发语言·后端·kotlin·安卓
宝杰X75 小时前
Compose Multiplatform+kotlin Multiplatfrom第三弹
android·开发语言·kotlin
大猫熊猫15 小时前
Android Studio 加载多个FLutter项目
android·ide·android studio
!!!+++15 小时前
Android Studio打开Modem模块出现:The project ‘***‘ is not a Gradle-based project
android·ide·android studio
Tom哈哈16 小时前
Android命令行查看CPU频率和温度
android