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

自定义对话框组件 - CustomDialog

概述

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

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

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

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

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

    1customDialog.show(getSupportFragmentManager(), CustomDialog.class.getSimpleName());
示例代码
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
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 文件中定义一个自定义的主题:

1<style name="CustomDialogTheme" parent="Theme.AppCompat.Light.Dialog">
2    <!-- 自定义对话框的样式 -->
3</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值