【Android】源码中的建造者模式

本文是基于 Android 14 的源码解析

在 Android 源码中,最常用到的建造者模式就是 AlertDialog.Builder,使用该建造者来构建复杂的 AlertDialog 对象。在开发过程中,我们经常用到 AlertDialog,具体示例如下:

private fun showDialog(context: Context) {
    val builder = AlertDialog.Builder(context)
    with(builder) {
        setIcon(R.mipmap.ic_launcher)
        setTitle("Title")
        setMessage("Message")
        setPositiveButton("Positive") { dialog, which ->
            ...
        }
        setNeutralButton("Neutral") { dialog, which ->
            ...
        }
        setNegativeButton("Negative") { dialog, which ->
            ...
        }
        create()
    }.show()
}

显示结果如图1所示:
请添加图片描述

图1

从类名就可以看出这就是一个建造者模式,通过建造者对象来组装 Dialog 的各个部分,如 title、button、message 等,将 Dialog 的构造和表示进行分离。下面看看 AlertDialog 的相关源码:

public class AlertDialog extends AppCompatDialog implements DialogInterface {

    // AlertController 接收建造者成员变量 P 中的各个参数
    final AlertController mAlert;

    ...

    // 构造函数
    protected AlertDialog(@NonNull Context context) {
        this(context, 0);
    }

    // 构造 AlertDialog
    protected AlertDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, resolveDialogTheme(context, themeResId));
        // 构造AlertController
        mAlert = new AlertController(getContext(), this, getWindow());
    }

    ...

    // 实际上调用的是 mAlert 的 setTitle 方法
    @Override
    public void setTitle(CharSequence title) {
        super.setTitle(title);
        mAlert.setTitle(title);
    }

    // 实际上调用的是 mAlert 的 setCustomTitle 方法
    public void setCustomTitle(View customTitleView) {
        mAlert.setCustomTitle(customTitleView);
    }

    ...

    public static class Builder {
        // 存储 AlertDialog 的各个参数,如 title、message、icon 等
        private final AlertController.AlertParams P;
        
        ...

        public Builder(@NonNull Context context) {
            this(context, resolveDialogTheme(context, 0));
        }

        public Builder(@NonNull Context context, @StyleRes int themeResId) {
            P = new AlertController.AlertParams(new ContextThemeWrapper(
                    context, resolveDialogTheme(context, themeResId)));
            mTheme = themeResId;
        }

        ...

        // 设置各种参数
        public Builder setTitle(@Nullable CharSequence title) {
            P.mTitle = title;
            return this;
        }

        public Builder setMessage(@Nullable CharSequence message) {
            P.mMessage = message;
            return this;
        }

        public Builder setView(View view) {
            P.mView = view;
            P.mViewLayoutResId = 0;
            P.mViewSpacingSpecified = false;
            return this;
        }

        ...

        // 构建 AlertDialog, 传递参数
        @NonNull
        public AlertDialog create() {
            // 调用 new AlertDialog 构造对象,并且将参数传递给个体 AlertDialog
            final AlertDialog dialog = new AlertDialog(P.mContext, mTheme);
            // 将 P 中的参数应用到 dialog 中的 mAlert 对象中
            P.apply(dialog.mAlert);
            dialog.setCancelable(P.mCancelable);
            if (P.mCancelable) {
                dialog.setCanceledOnTouchOutside(true);
            }
            dialog.setOnCancelListener(P.mOnCancelListener);
            dialog.setOnDismissListener(P.mOnDismissListener);
            if (P.mOnKeyListener != null) {
                dialog.setOnKeyListener(P.mOnKeyListener);
            }
            return dialog;
        }

        public AlertDialog show() {
            final AlertDialog dialog = create();
            dialog.show();
            return dialog;
        }
    }
}

上述代码中,Builder 类可以设置 AlertDialog 中的 title、message、button 等参数,这些参数都存储在类型为 AlertController.AlertParams 的成员变量 P 中,AlertController.AlertParams 中包含了与 AlertDialog 视图中对应的成员变量。在调用 Builder 类的 create 函数时会创建 AlertDialog,并且将 Builder 成员变量 P 中保存的参数应用到 AlertDialog 的 mAlert 对象中,即 P.apply(dialog.mAlert) 代码段。我们再看看 apply 函数的实现:

public void apply(AlertController dialog) {
    if (mCustomTitleView != null) {
        dialog.setCustomTitle(mCustomTitleView);
    } else {
        if (mTitle != null) {
            dialog.setTitle(mTitle);
        }
        if (mIcon != null) {
            dialog.setIcon(mIcon);
        }
        if (mIconId != 0) {
            dialog.setIcon(mIconId);
        }
        if (mIconAttrId != 0) {
            dialog.setIcon(dialog.getIconAttributeResId(mIconAttrId));
        }
    }
    if (mMessage != null) {
        dialog.setMessage(mMessage);
    }
    if (mPositiveButtonText != null || mPositiveButtonIcon != null) {
        dialog.setButton(DialogInterface.BUTTON_POSITIVE, mPositiveButtonText,
                mPositiveButtonListener, null, mPositiveButtonIcon);
    }
    if (mNegativeButtonText != null || mNegativeButtonIcon != null) {
        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, mNegativeButtonText,
                mNegativeButtonListener, null, mNegativeButtonIcon);
    }
    if (mNeutralButtonText != null || mNeutralButtonIcon != null) {
        dialog.setButton(DialogInterface.BUTTON_NEUTRAL, mNeutralButtonText,
                mNeutralButtonListener, null, mNeutralButtonIcon);
    }

    // 如果设置了 mItems,则表示是单选或者多选列表,此时创建—个 ListView
    if ((mItems != null) || (mCursor != null) || (mAdapter != null)) {
        createListView(dialog);
    }

    // 将 mView 设置给 Dialog
    if (mView != null) {
        if (mViewSpacingSpecified) {
            dialog.setView(mView, mViewSpacingLeft, mViewSpacingTop, mViewSpacingRight,
                    mViewSpacingBottom);
        } else {
            dialog.setView(mView);
        }
    } else if (mViewLayoutResId != 0) {
        dialog.setView(mViewLayoutResId);
    }
}

在 apply 函数中,只是将 AlertParams 参数设置到 AlertController 中,例如,将标题设置到 Dialog 对应的标题视图中,将 Message 设置到内容视图中等。当我们获取到 AlertDialog 对象后,通过 show 函数就可以显示这个对话框。我们看看 Dialog 的 show 函数:

public void show() {
    ...

    if (!mCreated) {
        dispatchOnCreate(null);
    } else {
        final Configuration config = mContext.getResources().getConfiguration();
        mWindow.getDecorView().dispatchConfigurationChanged(config);
    }

    onStart();
    mDecor = mWindow.getDecorView();

    ...

    WindowManager.LayoutParams l = mWindow.getAttributes();

    ...

    mWindowManager.addView(mDecor, l);
    
    ...
}

在 show 函数中主要做了如下几个事情:

  1. 通过 dispatchOnCreate 函数来调用 AlertDialog 的 onCreate 函数。
  2. 然后调用 AlertDialog 的 onStart 函数。
  3. 最后将 Dialog 的 DecorView 添加到 WindowManager 中。

很明显,这就是一系列典型的生命周期函数。那么按照惯例,AlertDialog 的内容视图构建按理应该在 onCreate 函数中,我们来看看是不是:

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

在 onCreate 函数中主要调用 了AlertController 的 installContent 方法,Dialog 中的 onCreate 函数只是一个空实现而己,可以忽略它。那么 AlertDialog 的内容视图必然就在 installContent 函数中:

public void installContent() {
    final int contentView = selectContentView();
    mDialog.setContentView(contentView);
    setupView();
}

installContent 函数的代码很少,但极为重要,它调用了 Window 对象的 setContentView,这个 setContentView 就与 Activity 中的一模一样,实际上 Activity 最终也是调用 Window 对象的 setContentView 函数。因此,这里就是设置 AlertDialog 的内容布局,这个布局就是 mAlertDialogLayout 字段的值,这个值在 AlertController 的构造函数中进行了初始化,具体代码如下:

public AlertController(Context context, AppCompatDialog di, Window window) {
    ...

    final TypedArray a = context.obtainStyledAttributes(null, R.styleable.AlertDialog,
            R.attr.alertDialogStyle, 0);

    mAlertDialogLayout = a.getResourceId(R.styleable.AlertDialog_android_layout, 0);
    
    ...

    a.recycle();

    ...
}

用图2来大致描述一下 AlertDialog 的布局结构:
请添加图片描述

图2

当通过 Builder 对象的 setTitle、setMessage 等方法设置具体内容时,就是将这些内容填充到对应的视图中。而 AlertDialog 也允许你通过 setView 传入内容视图,这个内容视图就是替换掉图2的内容区域,AlertDialog 预留了一个 customPanel 区域用来显示用户自定义的内容视图。我们来看看 setupView 方法:

private void setupView() {
    final View parentPanel = mWindow.findViewById(R.id.parentPanel);
    final View defaultTopPanel = parentPanel.findViewById(R.id.topPanel);
    final View defaultContentPanel = parentPanel.findViewById(R.id.contentPanel);
    final View defaultButtonPanel = parentPanel.findViewById(R.id.buttonPanel);

    final ViewGroup customPanel = (ViewGroup) parentPanel.findViewById(R.id.customPanel);
    setupCustomContent(customPanel);

    final View customTopPanel = customPanel.findViewById(R.id.topPanel);
    final View customContentPanel = customPanel.findViewById(R.id.contentPanel);
    final View customButtonPanel = customPanel.findViewById(R.id.buttonPanel);

    final ViewGroup topPanel = resolvePanel(customTopPanel, defaultTopPanel);
    final ViewGroup contentPanel = resolvePanel(customContentPanel, defaultContentPanel);
    final ViewGroup buttonPanel = resolvePanel(customButtonPanel, defaultButtonPanel);

    setupContent(contentPanel);
    setupButtons(buttonPanel);
    setupTitle(topPanel);

    final boolean hasCustomPanel = customPanel != null
            && customPanel.getVisibility() != View.GONE;
    final boolean hasTopPanel = topPanel != null
            && topPanel.getVisibility() != View.GONE;
    final boolean hasButtonPanel = buttonPanel != null
            && buttonPanel.getVisibility() != View.GONE;

    if (!hasButtonPanel) {
        if (contentPanel != null) {
            final View spacer = contentPanel.findViewById(R.id.textSpacerNoButtons);
            if (spacer != null) {
                spacer.setVisibility(View.VISIBLE);
            }
        }
    }

    if (hasTopPanel) {
        if (mScrollView != null) {
            mScrollView.setClipToPadding(true);
        }

        View divider = null;
        if (mMessage != null || mListView != null) {
            divider = topPanel.findViewById(R.id.titleDividerNoCustom);
        }

        if (divider != null) {
            divider.setVisibility(View.VISIBLE);
        }
    } else {
        if (contentPanel != null) {
            final View spacer = contentPanel.findViewById(R.id.textSpacerNoTitle);
            if (spacer != null) {
                spacer.setVisibility(View.VISIBLE);
            }
        }
    }

    if (mListView instanceof RecycleListView) {
        ((RecycleListView) mListView).setHasDecor(hasTopPanel, hasButtonPanel);
    }

    if (!hasCustomPanel) {
        final View content = mListView != null ? mListView : mScrollView;
        if (content != null) {
            final int indicators = (hasTopPanel ? ViewCompat.SCROLL_INDICATOR_TOP : 0)
                    | (hasButtonPanel ? ViewCompat.SCROLL_INDICATOR_BOTTOM : 0);
            setScrollIndicators(contentPanel, content, indicators,
                    ViewCompat.SCROLL_INDICATOR_TOP | ViewCompat.SCROLL_INDICATOR_BOTTOM);
        }
    }

    final ListView listView = mListView;
    if (listView != null && mAdapter != null) {
        listView.setAdapter(mAdapter);
        final int checkedItem = mCheckedItem;
        if (checkedItem > -1) {
            listView.setItemChecked(checkedItem, true);
            listView.setSelection(checkedItem);
        }
    }
}

这个 setupView 方法的名字已经很直观了:它用于初始化 AlertDialog 布局中的各个部分,例如标题区域、按钮区域和内容区域。在调用此函数之后,整个对话框的视图内容都会被设置完毕。这些不同区域的视图都是 mAlertDialogLayout 布局的子元素。Window 对象与整个 mAlertDialogLayout 的布局树相关联。当 setupView 调用完成后,整个视图树的数据都被填充完毕。当用户调用 show 函数时,WindowManager 会将 Window 对象的 DecorView(也就是对应于 mAlertDialogLayout 的视图)添加到用户的窗口上,并显示出来。

总之,AlertDialog 的建造者模式使得创建和配置对话框变得更加灵活和易于维护。通过链式调用,我们可以按需设置对话框的各个属性,而不必关心参数的顺序或数量。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/452531.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

SA3D:基于 NeRF 的三维场景分割方法

Paper: Cen J, Zhou Z, Fang J, et al. Segment anything in 3d with nerfs[J]. Advances in Neural Information Processing Systems, 2024, 36. Introduction: https://jumpat.github.io/SA3D/ Code: https://github.com/Jumpat/SegmentAnythingin3D SA3D 是一种用于 NeRF 表…

RabbitMQ 面试题及答案整理,最新面试题

RabbitMQ的核心组件有哪些? RabbitMQ的核心组件包括: 1、生产者(Producer): 生产者是发送消息到RabbitMQ的应用程序。 2、消费者(Consumer): 消费者是接收RabbitMQ消息的应用程序…

阿里云领盲盒活动

阿里云每次的活动都很给力,实打实地发东西。 这次是体验 通义灵码 的活动,这个是体验的推广链接 「通义灵码 体验 AI 编码,开 AI 盲盒」 我是在vscode安装的,体验还行,抽奖抽到了马克杯 这个是抽奖的具体步骤 https:…

mysql对索引的选择简述

概述 在业务中经常会优化一些mysql的慢查询,通常都是使用explain去查看分析,检查扫描行数和索引的命中情况; 但是在具体索引的选择上,explain结果中并没有直接展示出来; 此时可以开启mysql的追踪优化器Trace功能&…

【Greenhills】MULTI IDE网络版license统计授权使用情况

【更多软件使用问题请点击亿道电子官方网站查询】 1、 文档目标 用于购买了GHS网络版的客户,对于license的调用情况进行统计和管理 2、 问题场景 对于购买了GHS网络版license的客户,想要对于授权的使用情况进行分析和管理,但是,…

c++ 常用函数 集锦 整理中

c 常用函数集锦 目录 1、string和wstring之间转换 1、string和wstring之间转换 std::string convertWStringToString(std::wstring wstr) {std::string str;if (!wstr.empty()){std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;str converter.to_b…

【论文阅读】ACM MM 2023 PatchBackdoor:不修改模型的深度神经网络后门攻击

文章目录 一.论文信息二.论文内容1.摘要2.引言3.作者贡献4.主要图表5.结论 一.论文信息 论文题目&#xff1a; PatchBackdoor: Backdoor Attack against Deep Neural Networks without Model Modification&#xff08;PatchBackdoor:不修改模型的深度神经网络后门攻击&#xf…

西井科技参与IATA全球货运大会 以AI绿动能引领智慧空港新未来

3月12日至14日&#xff0c;由国际航空运输协会IATA主办的全球货运大会&#xff08;World Cargo Symposium&#xff09;在中国香港成功举办&#xff0c;这是全球航空货运领域最大规模与影响力的年度盛会。作为大物流领域全球领先的“智能化与新能源化”综合解决方案提供商&#…

参加大广赛是否有价值?让我们来看答案!

大广赛全称是全国大学生广告艺术大赛&#xff0c;是中国最大的高校广告创意竞赛活动。它由教育部高等教育司指导&#xff0c;中国传媒大学、大广赛文化传播&#xff08;北京&#xff09;有限公司共同举办。 命题素材在线预览https://js.design/f/Jspbti?sourcesh&planbtt…

互联网操作系统Puter

什么是 Puter &#xff1f; Puter 是一个先进的开源桌面环境&#xff0c;运行在浏览器中&#xff0c;旨在具备丰富的功能、异常快速和高度可扩展性。它可以用于构建远程桌面环境&#xff0c;也可以作为云存储服务、远程服务器、Web 托管平台等的界面。Puter 是一个隐私至上的个…

垃圾清理软件大全免费 磁盘空间不足?注册表不敢乱动怎么办?ccleaner官方下载

在日常的工作中&#xff0c;面对重要文件时往往都会备份一份&#xff1b;在下载文件时&#xff0c;有时也会不小心把一份文件下载好多次。这些情况会导致电脑中出现重复的文件&#xff0c;删除这些重复文件&#xff0c;可以节省电脑空间&#xff0c;帮助提高电脑运行速度。那么…

websocket 使用示例

websocket 使用示例 前言html中使用vue3中使用1、安装websocket依赖2、代码 vue2中使用1、安装websocket依赖2、代码 前言 即时通讯webSocket 的使用 html中使用 以下是一个简单的 HTML 页面示例&#xff0c;它连接到 WebSocket 服务器并包含一个文本框、一个发送按钮以及 …

大语言模型RAG-技术概览 (一)

大语言模型RAG-技术概览 (一) 一 RAG概览 检索增强生成&#xff08;Retrieval-AugmentedGeneration, RAG&#xff09;。即大模型在回答问题或生成问题时会先从大量的文档中检索相关的信息&#xff0c;然后基于这些信息进行回答。RAG很好的弥补了传统搜索方法和大模型两类技术…

自然语言处理(NLP)—— 语义关系提取

语义关系是指名词或名词短语之间的联系。这些关系可以是表面形式&#xff08;名词性实体&#xff09;之间的联系&#xff0c;也可以是知识工程中概念之间的联系。在自然语言处理&#xff08;NLP&#xff09;和文本挖掘领域&#xff0c;识别和理解这些语义关系对于信息提取、知识…

[媒体宣传]上海有哪些可以邀约的新闻媒体资源汇总

传媒如春雨&#xff0c;润物细无声&#xff0c;大家好&#xff0c;我是51媒体网胡老师。 上海作为中国最大的城市之一&#xff0c;拥有丰富的新闻媒体资源。以下是一些可以邀约的新闻媒体资源汇总&#xff1a; 报纸媒体&#xff1a; 《新民晚报》&#xff1a;上海最具影响力…

C#,红黑树(Red-Black Tree)的构造,插入、删除及修复、查找的算法与源代码

1 红黑树(Red-Black Tree) 如果二叉搜索树满足以下红黑属性,则它是红黑树: 每个节点不是红色就是黑色。根是黑色的。每片叶子(无)都是黑色的。如果一个节点是红色的,那么它的两个子节点都是黑色的。对于每个节点,从节点到后代叶的所有路径都包含相同数量的黑色节点。红…

Linux进程概念(2)

一、进程状态 Linux的进程状态实际上就是 struct task_struct 结构体中的一个变量 1.1状态汇总 其中&#xff0c;Linux 状态是用数组储存的&#xff0c;如下&#xff1a; static const char * const task_state_array[] { "R (running)", // 0 …

OceanBase4.2版本 Docker 体验

&#x1f4e2;&#x1f4e2;&#x1f4e2;&#x1f4e3;&#x1f4e3;&#x1f4e3; 哈喽&#xff01;大家好&#xff0c;我是【IT邦德】&#xff0c;江湖人称jeames007&#xff0c;10余年DBA及大数据工作经验 一位上进心十足的【大数据领域博主】&#xff01;&#x1f61c;&am…

Unity开发中Partial 详细使用案例

文章目录 **1. 分割大型类****2. 与 Unity 自动生成代码协同工作****3. 团队协作****4. 共享通用逻辑****5. 自定义编辑器相关代码****6. 配合 Unity 的 ScriptableObjects 使用****7. 多人协作与版本控制系统友好** 在 Unity 开发中&#xff0c; partial 关键字是 C# 语言提供…

ChatGPT提问技巧——问题解答提示

ChatGPT提问技巧——问题解答提示 问题解答提示是一种允许模型生成回答特定问题或任务的文本的技术。要做到这一点&#xff0c;需要向模型提供一个问题或任务作为输入&#xff0c;以及与该问题或任务相关的任何附加信息。 一些提示示例及其公式如下&#xff1a; 示例 1&…
最新文章