Android之DialogFragment

Android Dialog:Dialog和DialogFragment的区别?DialogFragment如何使用?源码解析 Dialog在创建时会关联一个Context(上下文),并通过这个Context获取WindowManager服务,进而与WindowManager绑定,实现窗口的添加和管理。如果onCreateDialog不传递一个新的dialog,那么就会使用默认的,而布局,就是从onCreateView里面取,也就是requireView方法。Dialog是Android中用于显示一个浮动窗口的类,这个窗口会覆盖在当前的活动(Activity)或应用程序的顶部。我们可以看一下Dialog的源码。 阅读详情

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:14px;">        DialogFragment是基于Fragment展示Dialog的窗口。对话框的控制(何时隐藏、显示、关闭)通过对应的Dialogment 的API来完成,而不是Dialog的API.</span></span>

         DialogFragment需要保证Fragment和Dialog的状态保持一致。它监听dialog的dissmiss event,并且去处理自己的状态。这意味着应该调用 show(FragmentManager, String)或者show(FragmentTransaction, String)来添加一个DialogFragment的实例给UI层,并且它自主将remove当dialog消失的时候。

       实现这个类创建Dialog,需要通过重写法onCreateView(LayoutInflater, ViewGroup, Bundle)来填充dialog的内容,或者重写方法onCreateDialog(Bundle)来创建一个完全自定义的dialog。

1.   重写onCreateView创建Dialog

(a)布局文件,一个登陆窗口的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="10dp"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓名:"
            android:textSize="18sp"/>

        <EditText
            android:id="@+id/edit_logiName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:hint="请输入名字"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="10dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码:"
            android:textSize="18sp" />

        <EditText
            android:id="@+id/edit_logiPwd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:hint="请输入密码"/>
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorPrimary"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#00000000"
            android:text="取消"/>

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@color/colorPrimary"
            />

        <Button
            android:id="@+id/btn_login"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="#00000000"
            android:layout_weight="1"
            android:text="登录"/>
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorAccent"
        />
</LinearLayout>
(b) 继承DialogFragmeng重写onCreateView方法

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.dia_login, container, false);

    return view;
}

(c) 测试展示

LoginDialog loginDialog = LoginDialog.newInstance();
loginDialog.show(getFragmentManager(), "dilog"); 


(d) 设置Dialog样式

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    mNum = getArguments().getInt("num");

    int style = DialogFragment.STYLE_NORMAL, theme = 0;

    switch (mNum) {
        case 1: style = DialogFragment.STYLE_NO_TITLE; break;
        case 2: style = DialogFragment.STYLE_NO_FRAME; break;
        case 3: style = DialogFragment.STYLE_NO_INPUT; break;
        case 4: style = DialogFragment.STYLE_NORMAL; break;
        case 5: style = DialogFragment.STYLE_NORMAL; break;
        case 6: style = DialogFragment.STYLE_NO_TITLE; break;
        case 7: style = DialogFragment.STYLE_NO_FRAME; break;
        case 8: style = DialogFragment.STYLE_NORMAL; break;
    }
    switch (mNum) {
        case 4: theme = android.R.style.Theme_Holo; break;
        case 5: theme = android.R.style.Theme_Holo_Light_Dialog; break;
        case 6: theme = android.R.style.Theme_Holo_Light; break;
        case 7: theme = android.R.style.Theme_Holo_Light_Panel; break;
        case 8: theme = android.R.style.Theme_Holo_Light;
            break;
    }

   
2.重写onCreateDialog方法

(a) DIalog布局同上

(b) 继承DialogFragment,重写OnCreateDialog,创建Dialog

public Dialog onCreateDialog(Bundle savedInstanceState) {
    LayoutInflater inflater = LayoutInflater.from(getActivity());
    View view = inflater.inflate(R.layout.dia_login, null, false);

    initUI(view);

    setListener();

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setView(view);
    return builder.create();
(c) 在合适的位置展示Dialog
(d)设置Dialog样式

在XML中定义

<style name="DialogStyle" parent="@android:style/Theme.Dialog">  
	<item name="android:windowBackground">@drawable/settings_window</item>  
	<item name="android:windowFrame">@null</item>  
	<item name="android:windowNoTitle">true</item>  
	<item name="android:windowIsFloating">true</item>  
	<item name="android:windowIsTranslucent">true</item>  
	<item name="android:background">@android:color/background_light</item>  
	<item name="android:windowFullscreen">true</item>  
	<item name="android:backgroundDimEnabled">true</item>  
</style> 
创建Dialog时调用相应的style
public Dialog onCreateDialog(Bundle savedInstanceState) {  

*******
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),R.style.DialogStyle);

******
}


集思广益、坚持不懈的总结,总会进步!

参考资料:

        1.http://www.android-doc.com/reference/android/app/DialogFragment.html

2.https://github.com/codepath/android_guides/wiki/Using-DialogFragment

  3.http://blog.youkuaiyun.com/pipisky2006/article/details/8547539

Android通用DialogFragment 前言 在开发过程中,我们会经常使用对话框,为了提高开发效率,于是我使用DialogFragment封装了一个通过的对话框。 为什么使用DialogFragment 首先在使用 DialogFragment之前,我们创建对话框一般都会采用Dialog的形式,并且从编写代码的角度来看,Dialog使用起来也较为简单,但Android 官方推荐使用 DialogFragment 来代替 Dialog。 DialogFragment 有着Dialog 所没有的特性在手机配置变化,导致Activity需要重新创建时, 阅读详情

相关推荐

Yolov5s/Yolov8s网络结构图

yolov8、yolov5网络结构图

Goodness2020的博客 9925

android_基础_dialogFragment

转载自:http://blog.youkuaiyun.com/lmj623565791/article/details/37815413 1、 概述 DialogFragment在android 3.0时被引入。是一种特殊的Fragment,用于在Activity的内容之上展示一个模态的对话框。典型的用于:展示警告框,输入框,确认框等等。 在DialogFragment产生之前,我们创建对话框:一般采用AlertDialog和Dialog。注:官方不推荐直接使用Dialog创建对话框。 2、 好处与用法 使用Dia.

写bug的蚊子的博客 2426

Visual Studio调试时遇到ntdll.dll的PDB文件缺失?别慌,这3个方法帮你搞定(附详细步骤)

本文详细解析了Visual Studio调试时ntdll.dll的PDB文件缺失问题,提供了3种实用解决方案:调整调试设置、配置符号服务器与缓存目录、手动下载管理符号文件。通过分步指导和高级技巧,帮助开发者快速恢复完整的调试功能,特别适用于Windows平台下的C++项目开发。

weixin_42537919的博客 513

Android Dialog之DialogFragment详解与使用

看到DialogFragment,从单词可以看到是Dialog与Fragment组合而成,但是在DialogFragment中,DialogFragment其实是一个fragment,内部持有一个Dialog,这样方便了开发者的管理。fragment在碎片管理中其实就是一个View,最终view的显示是通过dialog来完成。

5952

USRP N210与MATLAB连接实战:从驱动安装到信号接收

本文详细介绍了USRP N210与MATLAB连接的完整流程,涵盖驱动安装、网络配置、支持包安装及信号接收实战。重点解决了安装过程中的常见错误和硬件连接问题,并通过MATLAB代码示例展示了如何配置接收器、处理信号数据,帮助用户快速实现从设备对接到信号分析的全过程。

blue的专栏 819

【人工智能】向量数据库全生命周期数据安全防护体系:破解向量化与检索双环节泄露风险

大模型时代,向量数据库已成为RAG(检索增强生成)架构、语义检索、多模态应用的核心基础设施,解决了大模型幻觉、长上下文窗口限制、私有知识落地等核心痛点。但行业普遍存在认知误区:将Embedding向量等同于“无害的语义压缩乱码”,忽视了向量数据原生的泄露风险。

奶爸的编程之路,也就一周冷个三天 159

Android DialogFragment总结

#.简介 DialogFragment是继承Fragment,兼具Fragment和Dialog的特点: 一方面,它具有Fragment的生命周期,可以由Activity的FragmentManager来管理器生命周期; 另一方面,其内部包含一个Dialog成员变量,可以像窗口一样展示。 ##.DialogFragment相对于Dialog的优点: Dialog的生命周期不会随着Activity生命周期变化而变化,而DialogFragment继承自Fragme...

u013914309的博客 2697

华为OD机试真题 新系统 2026-08-16 Python&JS【智能家居模式调度优化】

智能家居系统中有多个自动化联动模式,每个模式 modes[i]=[start_time,end_time,power],表示它在左闭右开区间 [start_time,end_time) 内持续消耗固定功率 power。

MISAYAONE的博客 85

android fragment dialogfragment,安卓开发-Fragment与DialogFragment及Activity之间的通信

内容纲要Fragment与DialogFragment及Activity之间的通信要完成Fragment中弹出DialogFragment对话框,并且在Fragment中响应对话框按钮的点击操作,需要Fragment和DialogFragmeng通信要完成Fragment给Activity传递切换Fragment信号,需要Fragment和Activity通信DialogFragment传递数据给...

weixin_36015463的博客 697

YOLO11改进|YOLO11中引入注意力和卷积融合模块CAFMAttention

YOLO11改进|YOLO11中引入注意力和卷积融合模块CAFMAttention

A1983Z的博客 1220

高通Camx功能feature分析之八:高通NCS服务之架构和初始化

本文介绍了高通NCS(NONCameraSensor)服务的架构与初始化流程。NCS服务通过QMI接口访问非相机传感器,为CameraEIS、AEC等算法节点提供陀螺仪、加速度计等传感器数据支持。文章详细解析了NCS服务的分层架构,包括客户端、服务端、QSEE接口实现等组件,并重点分析了初始化过程中获取传感器列表、查询能力等关键步骤。通过camxsetting.xml中的enableNCSService开关控制服务启动,初始化过程涉及QSEE单例创建、传感器能力查询等核心操作。后续文章将继续深入讲解相关技术

929

Android 弹窗 DialogFragment

前言 一直以来 , 做弹窗功能的时候都是使用的Dialog , 布局复杂的弹窗则使用Activity , 把背景设置成透明, 最近了解到一个DialogFragment , 发现使用起来更加方便, 也是谷歌建议的做法. 简介 DialogFragment 继承自Fragment , 所以它的用法基本跟Fragment一致 , 也包含了一些生命周期和上下文context 和 监听之类的东西. 而且...

卓原的进化之旅 7190

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

安卓常用的Dialog:DialogFragment(Android 官方推荐 : DialogFragment 创建对话框)

1、 概述DialogFragment在android 3.0时被引入。是一种特殊的Fragment,用于在Activity的内容之上展示一个模态的对话框。典型的用于:展示警告框,输入框,确认框等等。在DialogFragment产生之前,我们创建对话框:一般采用AlertDialog和Dialog。注:官方不推荐直接使用Dialog创建对话框。2、 好处与用法使用DialogFragment来管...

Jason_HD的博客 2547

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Android DialogFragment

In this tutorial, we’ll be discussing what are DialogFragments. We’ll see how they are different from the Dialogs too with the help of a simple android application. 在本教程中,我们将讨论什么是DialogFragments。 我们将在...

从零开始的教程世界 619

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

Android 官方推荐 : DialogFragment 创建对话框

1、 概述 DialogFragment在android 3.0时被引入。是一种特殊的Fragment,用于在Activity的内容之上展示一个模态的对话框。典型的用于:展示警告框,输入框,确认框等等。 在DialogFragment产生之前,我们创建对话框:一般采用AlertDialog和Dialog。注:官方不推荐直接使用Dialog创建对话框。 2、 好处与用法 使用DialogFr

Hongyang 26万+

android dialogfragment style,android DialogFragment使用 附例子

之前一直用Activity中的onCreateDialog方法来创建dialog,现在在弄Android 4.0,API一直提示onCreateDialog“This method is deprecated“,推荐使用DialogFragment,去Android 官网网站查了下DialogFragment的使用方法,第一个例子为了介绍style和theme,显得非常复杂,还不知道如何设置tit...

weixin_33295562的博客 235

关于安卓dialogFragment中,EditText无法删除文字的问题

摘要:安卓开发中DialogFragment的EditText无法删除文字的问题分析。常见说法认为是焦点丢失导致,但作者发现键盘能正常拉起,因此推断是事件响应问题。最终定位到DialogFragment基类中设置的setOnKeyListener拦截了删除事件。解决方案是修改该监听器逻辑,确保不屏蔽删除键事件。文章提供了完整的BaseDialogFragment代码示例,展示了如何通过调整事件监听解决EditText删除功能失效的问题。

motosheep的博客 983

Android的DialogFragment的基本使用方法

Android的DialogFragment的使用 在使用时,对Android系统有了最低API为11的限制 1.新建继承于DialogFragment的类 在类中由一个按钮,点击按钮关闭对话框。 layout中只有一个按钮,代码在此就不贴出来了。 public class Fragment_Search extends DialogFragment { Button btn_c

琪花亿草 1万+

在Android中创建DialogFragment

Override// 膨胀自定义布局// 初始化视图组件// 处理点击事件dismiss();// 关闭对话框});1. 布局文件 (res/layout/fragment_dialog.xml)

qq_39460057的博客 1390

Android使用DialogFragment创建对话框

目录目录 概述 DialogFragment 重写onCreateView来创建Dialog 重写onCreateDialog创建Dialog 与Activity进行通信 参考链接概述DialogFragment是在Android 3.0时引入的,是一种特殊的Fragment,用于在Activity的内容之上展示一个模拟的对话框。例如,用于展示一个警示框、密码框等。在DialogFragment之前

小一的专栏 6018

android fragment dialogfragment,Android中使用DialogFragment编写对话框的实例教程

Android提供alert、prompt、pick-list,单选、多选,progress、time-picker和date-picker对话框,并提供自定义的dialog。在Android 3.0后,dialog基于fragment,并对之前版本提供兼容支持库,也就是说对于开发者而言,dialog是基于DialogFragment的,但此时需要在应用中加入相关的兼容库。和Windows或者网页...

weixin_29531177的博客 908

Android为啥推荐用DialogFragment创建Dialog?

前言:这段时候有点忙,因为再敢需求,说忙也都是在敷衍,时间挤挤还是有的,从开始写博客的那一刻起就应该一直坚持下来,不要三天打鱼两天晒网,上次写过一个Android进阶之(dialog详解一),今天继续上次的内容探究一下,从Android3.0后Android引入的DailogFragment,我们一起从源码的角度去看看为啥谷歌推荐这样创建Dialog. DialogFragment对话框出现的意义

vv_bug 6742
上一篇: Android之 Activity间通信总结
下一篇: Android之DialogFragment宽度充满屏幕
行云间
博客等级 码龄12年 438粉丝 173原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值