简介

上一篇简单的介绍了下Fragment的使用方法,这一篇主要看下DialogFragment。

在android 3.0时被引入。是一种特殊的Fragment,用于在Activity的内容之上展示一个模态的对话框。典型的用于:展示警告框,输入框,确认框等等。之前的博客中有针对AlertDialog作出过详细的介绍,这个的功能和它比较类似,但是官方推荐的必然会有其牛逼之处~

使用方法 ~ 参考Fragment

使用方法和Fragment基本相同,但是DialogFragment多出来一个onCreateDialog方法



1.创建Fragment对应的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:orientation="vertical"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:text="MOBLIE APP"
android:textColor="@color/colorAccent"
android:textStyle="bold|italic" /> <android.support.design.widget.TextInputLayout
android:layout_width="250dp"
android:layout_height="wrap_content"> <EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Username" />
</android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout
android:layout_width="250dp"
android:layout_height="wrap_content"> <EditText
android:id="@+id/et_passwrod"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Password" /> </android.support.design.widget.TextInputLayout> <Button
android:id="@+id/bt_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录" />
</LinearLayout>

2.创建继承DialogFragment的Fragment并重写onCreateView方法

public class ContentFragment extends DialogFragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.content_fragment, container);
return view;
}
}

3.Activity中使用

    private void showRunDialog() {
ContentFragment dialog = new ContentFragment();
dialog.show(getFragmentManager(), "login");//后面的字串是Tag
}

4.效果

使用方法 ~ DialogFragment独有

1.创建Fragment对应的布局文件

这个布局可以和上面相同,但是为了区分效果,单独编写一个

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:text="Mobile APP"
android:textColor="@color/colorAccent"
android:textSize="20sp"
android:textStyle="bold|italic" /> <android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"> <EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Username" /> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"> <EditText
android:id="@+id/et_passwrod"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Password" /> </android.support.design.widget.TextInputLayout>
</LinearLayout>

2.创建继承DialogFragment的Fragment并重写onCreateDialog方法

public class DialogContent extends DialogFragment {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); final View view = getActivity().getLayoutInflater().inflate(R.layout.content_dialog, null, false); builder.setView(view)
.setPositiveButton("登录", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final String username = ((EditText) (view.findViewById(R.id.et_username))).getText().toString();
final String password = ((EditText) (view.findViewById(R.id.et_passwrod))).getText().toString();
((MainActivity) getActivity()).setUsernameAndPassword(username, password);//与Activity交互,也可以通过实现接口的方式实现
}
})
.setNegativeButton("注册", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final String username = ((EditText) (view.findViewById(R.id.et_username))).getText().toString();
final String password = ((EditText) (view.findViewById(R.id.et_passwrod))).getText().toString();
((MainActivity) getActivity()).setUsernameAndPassword("注册", "注册");//与Activity交互,也可以通过实现接口的方式实现
}
}); return builder.create();
}
}

在onCreateDialog方法中,通过创建AlertDialog的方式来生成Dialog



3.Activity中使用方式和上面相同



4.效果

优势

屏幕旋转过程中对话框不消失



<Android 基础(二十九)> Fragment (2) ~ DialogFragment的更多相关文章

  1. Bootstrap <基础二十九>面板(Panels)

    Bootstrap 面板(Panels).面板组件用于把 DOM 组件插入到一个盒子中.创建一个基本的面板,只需要向 <div> 元素添加 class .panel 和 class .pa ...

  2. <Android 基础(十九)> CoordinatorLayout

    介绍 CoordinatorLayout,中文翻译,协调布局,顾名思义,此布局中的子View之间,子View与父布局之间应该是可以协调工作的,如何协调,Behavior. 今天看下Android St ...

  3. Bootstrap <基础二十八>列表组

    列表组.列表组件用于以列表形式呈现复杂的和自定义的内容.创建一个基本的列表组的步骤如下: 向元素 <ul> 添加 class .list-group. 向 <li> 添加 cl ...

  4. Bootstrap <基础二十六>进度条

    Bootstrap 进度条.在本教程中,你将看到如何使用 Bootstrap 创建加载.重定向或动作状态的进度条. Bootstrap 进度条使用 CSS3 过渡和动画来获得该效果.Internet ...

  5. Bootstrap <基础二十五>警告(Alerts)

    警告(Alerts)以及 Bootstrap 所提供的用于警告的 class.警告(Alerts)向用户提供了一种定义消息样式的方式.它们为典型的用户操作提供了上下文信息反馈. 您可以为警告框添加一个 ...

  6. Bootstrap<基础二十四> 缩略图

    Bootstrap 缩略图.大多数站点都需要在网格中布局图像.视频.文本等.Bootstrap 通过缩略图为此提供了一种简便的方式.使用 Bootstrap 创建缩略图的步骤如下: 在图像周围添加带有 ...

  7. Bootstrap <基础二十二>超大屏幕(Jumbotron)

    Bootstrap 支持的另一个特性,超大屏幕(Jumbotron).顾名思义该组件可以增加标题的大小,并为登陆页面内容添加更多的外边距(margin).使用超大屏幕(Jumbotron)的步骤如下: ...

  8. Bootstrap<基础二十> 标签

    Bootstrap 标签.标签可用于计数.提示或页面上其他的标记显示.使用 class .label 来显示标签,如下面的实例所示: <!DOCTYPE html> <html> ...

  9. 第三百二十九节,web爬虫讲解2—urllib库爬虫—ip代理—用户代理和ip代理结合应用

    第三百二十九节,web爬虫讲解2—urllib库爬虫—ip代理 使用IP代理 ProxyHandler()格式化IP,第一个参数,请求目标可能是http或者https,对应设置build_opener ...

  10. 使用Typescript重构axios(二十九)——添加baseURL

    0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ...

随机推荐

  1. 自己搞了20万张图片100个分类,tensorflow训练23万次后。。。。。。

    自己搞了20万张图片100个分类,tensorflow训练23万次后...... 我自己把训练用的一张图片,弄乱之后做了一个预测 100个汉字,20多万张图片,tensorflow CNN训练23万次 ...

  2. Java之IO(十二)CharArrayReader和CharArrayWriter

    转载请注明源出处:http://www.cnblogs.com/lighten/p/7082668.html 1.前言 本章介绍字符数组流,作用和ByteArrayInputStream字节数组流相同 ...

  3. numpy 基本使用1

    Numpy是一个非常强大的库,具有大量线性代数以及大规模科学计算的方法. #-*- coding:utf-8 -*- import numpy as np #Numpy生成一维数组 a=np.arra ...

  4. spring websocket 和socketjs实现单聊群聊,广播的消息推送详解

    spring websocket 和socketjs实现单聊群聊,广播的消息推送详解 WebSocket简单介绍 随着互联网的发展,传统的HTTP协议已经很难满足Web应用日益复杂的需求了.近年来,随 ...

  5. SpringAOP-切面优先级

    项目中有两个切面,这两个切面都作用于同一个方法,哪个先执行哪个后执行呢,所以要定义一个切面的优先级 import java.util.Arrays; import org.aspectj.lang.J ...

  6. 删除Open with Atom右键菜单

    特别特别讨厌Atom的右键菜单,叕没有设置项可以去掉,烦!安装完RegScanner v2.15汉化版之后,开始搜索删除Atom的右键菜单 1.打开E:\RegScanner2.加载配置文件 E:\R ...

  7. WINDOWS 下 修改APACHE 并发数

    某次,配置大型站点.日IP过2W. 刚解析完,就特别卡,每个页面都是慢吞吞的打开的. 至少30秒.但是,3389进入服务器很快,CPU 内存都是几乎为0. 想到WINDOWS下使用的是APACHE,并 ...

  8. java的if语句,少于一行可以省略大括号

    我们认识的 if 语句,大概是这样的: if(条件){ 语句1; }else{ 语句2; } 如果要执行的语句少于1行,大括号是可以省略的 可以让程序更简洁和美观 if(条件){ 语句1; 语句2; ...

  9. JDK的spi实现

    SPI的全名为Service Provider Interface.大多数开发人员可能不熟悉,因为这个是针对厂商或者插件的.在java.util.ServiceLoader的文档里有比较详细的介绍.简 ...

  10. 169.254地址无网关信息 ----- 解决方案 启动DHCP服务

    169.254.X.X是Windows操作系统在DHCP信息租用失败时自动给客户机分配的IP地址,.看到地址的时候没有网关. 解决方案   启动DHCP服务 1. 首先确认 路由器  DHCP 服务已 ...