DialogFragment在android 3.0时被引入。是一种特殊的Fragment,用于在Activity的内容之上展示一个模态的对话框。典型的用于:展示警告框,输入框,确认框等等。

在DialogFragment产生之前,我们创建对话框:一般采用AlertDialog和Dialog。注:官方不推荐直接使用Dialog创建对话框。

2、 好处与用法

使用DialogFragment来管理对话框,当旋转屏幕和按下后退键时可以更好的管理其声明周期,它和Fragment有着基本一致的声明周期。且DialogFragment也允许开发者把Dialog作为内嵌的组件进行重用,类似Fragment(可以在大屏幕和小屏幕显示出不同的效果)。上面会通过例子展示这些好处~

使用DialogFragment至少需要实现onCreateView或者onCreateDIalog方法。onCreateView即使用定义的xml布局文件展示Dialog。onCreateDialog即利用AlertDialog或者Dialog创建出Dialog。

3、 重写onCreateView创建Dialog

a)布局文件,我们创建一个设置名称的布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content" >
  5. <TextView
  6. android:id="@+id/id_label_your_name"
  7. android:layout_width="wrap_content"
  8. android:layout_height="32dp"
  9. android:gravity="center_vertical"
  10. android:text="Your name:" />
  11. <EditText
  12. android:id="@+id/id_txt_your_name"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:layout_toRightOf="@id/id_label_your_name"
  16. android:imeOptions="actionDone"
  17. android:inputType="text" />
  18. <Button
  19. android:id="@+id/id_sure_edit_name"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:layout_alignParentRight="true"
  23. android:layout_below="@id/id_txt_your_name"
  24. android:text="ok" />
  25. </RelativeLayout>

b)继承DialogFragment,重写onCreateView方法

  1. package com.example.zhy_dialogfragment;
  2. import android.app.DialogFragment;
  3. import android.os.Bundle;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. public class EditNameDialogFragment extends DialogFragment
  8. {
  9. @Override
  10. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  11. Bundle savedInstanceState)
  12. {
  13. View view = inflater.inflate(R.layout.fragment_edit_name, container);
  14. return view;
  15. }
  16. }

c)测试运行:

Main方法中调用:

  1. public void showEditDialog(View view)
  2. {
  3. EditNameDialogFragment editNameDialog = new EditNameDialogFragment();
  4. editNameDialog.show(getFragmentManager(), "EditNameDialog");
  5. }

效果图:

可以看到,对话框成功创建并显示出来,不过默认对话框有个讨厌的标题,我们怎么去掉呢:可以在onCreateView中调用getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);即可去掉。即:
  1. public class EditNameDialogFragment extends DialogFragment
  2. {
  3. @Override
  4. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  5. Bundle savedInstanceState)
  6. {
  7. getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
  8. View view = inflater.inflate(R.layout.fragment_edit_name, container);
  9. return view;
  10. }
  11. }

效果图:


很完美的去掉了讨厌的标题。

4、 重写onCreateDialog创建Dialog

在onCreateDialog中一般可以使用AlertDialog或者Dialog创建对话框,不过既然google不推荐直接使用Dialog,我们就使用AlertDialog来创建一个登录的对话框。

a)布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:orientation="vertical" >
  6. <ImageView
  7. android:layout_width="match_parent"
  8. android:layout_height="64dp"
  9. android:background="#FFFFBB33"
  10. android:contentDescription="@string/app_name"
  11. android:scaleType="center"
  12. android:src="@drawable/title" />
  13. <EditText
  14. android:id="@+id/id_txt_username"
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. android:layout_marginBottom="4dp"
  18. android:layout_marginLeft="4dp"
  19. android:layout_marginRight="4dp"
  20. android:layout_marginTop="16dp"
  21. android:hint="input username"
  22. android:inputType="textEmailAddress" />
  23. <EditText
  24. android:id="@+id/id_txt_password"
  25. android:layout_width="match_parent"
  26. android:layout_height="wrap_content"
  27. android:layout_marginBottom="16dp"
  28. android:layout_marginLeft="4dp"
  29. android:layout_marginRight="4dp"
  30. android:layout_marginTop="4dp"
  31. android:fontFamily="sans-serif"
  32. android:hint="input password"
  33. android:inputType="textPassword" />
  34. </LinearLayout>

b)继承DialogFragment重写onCreateDialog方法

  1. package com.example.zhy_dialogfragment;
  2. import android.app.AlertDialog;
  3. import android.app.Dialog;
  4. import android.app.DialogFragment;
  5. import android.content.DialogInterface;
  6. import android.os.Bundle;
  7. import android.view.LayoutInflater;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10. import android.widget.EditText;
  11. public class LoginDialogFragment extends DialogFragment
  12. {
  13. @Override
  14. public Dialog onCreateDialog(Bundle savedInstanceState)
  15. {
  16. AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  17. // Get the layout inflater
  18. LayoutInflater inflater = getActivity().getLayoutInflater();
  19. View view = inflater.inflate(R.layout.fragment_login_dialog, null);
  20. // Inflate and set the layout for the dialog
  21. // Pass null as the parent view because its going in the dialog layout
  22. builder.setView(view)
  23. // Add action buttons
  24. .setPositiveButton("Sign in",
  25. new DialogInterface.OnClickListener()
  26. {
  27. @Override
  28. public void onClick(DialogInterface dialog, int id)
  29. {
  30. }
  31. }).setNegativeButton("Cancel", null);
  32. return builder.create();
  33. }
  34. }

c)调用

  1. public void showLoginDialog(View view)
  2. {
  3. LoginDialogFragment dialog = new LoginDialogFragment();
  4. dialog.show(getFragmentManager(), "loginDialog");
  5. }

效果图:

可以看到通过重写onCreateDialog同样可以实现创建对话框,效果还是很nice的。

5、传递数据给Activity

从dialog传递数据给Activity,可以使用“fragment interface pattern”的方式,下面通过一个改造上面的登录框来展示这种模式。

改动比较小,直接贴代码了:

  1. package com.example.zhy_dialogfragment;
  2. import android.app.AlertDialog;
  3. import android.app.Dialog;
  4. import android.app.DialogFragment;
  5. import android.content.DialogInterface;
  6. import android.os.Bundle;
  7. import android.view.LayoutInflater;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10. import android.widget.EditText;
  11. public class LoginDialogFragment extends DialogFragment
  12. {
  13. private EditText mUsername;
  14. private EditText mPassword;
  15. public interface LoginInputListener
  16. {
  17. void onLoginInputComplete(String username, String password);
  18. }
  19. @Override
  20. public Dialog onCreateDialog(Bundle savedInstanceState)
  21. {
  22. AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  23. // Get the layout inflater
  24. LayoutInflater inflater = getActivity().getLayoutInflater();
  25. View view = inflater.inflate(R.layout.fragment_login_dialog, null);
  26. mUsername = (EditText) view.findViewById(R.id.id_txt_username);
  27. mPassword = (EditText) view.findViewById(R.id.id_txt_password);
  28. // Inflate and set the layout for the dialog
  29. // Pass null as the parent view because its going in the dialog layout
  30. builder.setView(view)
  31. // Add action buttons
  32. .setPositiveButton("Sign in",
  33. new DialogInterface.OnClickListener()
  34. {
  35. @Override
  36. public void onClick(DialogInterface dialog, int id)
  37. {
  38. LoginInputListener listener = (LoginInputListener) getActivity();
  39. listener.onLoginInputComplete(mUsername
  40. .getText().toString(), mPassword
  41. .getText().toString());
  42. }
  43. }).setNegativeButton("Cancel", null);
  44. return builder.create();
  45. }
  46. }

拿到username和password的引用,在点击登录的时候,把activity强转为我们自定义的接口:LoginInputListener,然后将用户输入的数据返回。

MainActivity中需要实现我们的接口LoginInputListener,实现我们的方法,就可以实现当用户点击登陆时,获得我们的帐号密码了:

  1. c)  MainActivity
  2. package com.example.zhy_dialogfragment;
  3. import com.example.zhy_dialogfragment.LoginDialogFragment.LoginInputListener;
  4. import android.app.Activity;
  5. import android.app.AlertDialog;
  6. import android.content.DialogInterface;
  7. import android.os.Bundle;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.widget.Toast;
  11. public class MainActivity extends Activity implements LoginInputListener
  12. {
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState)
  15. {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18. }
  19. public void showLoginDialog(View view)
  20. {
  21. LoginDialogFragment dialog = new LoginDialogFragment();
  22. dialog.show(getFragmentManager(), "loginDialog");
  23. }
  24. @Override
  25. public void onLoginInputComplete(String username, String password)
  26. {
  27. Toast.makeText(this, "帐号:" + username + ",  密码 :" + password,
  28. Toast.LENGTH_SHORT).show();
  29. }
  30. }

效果:

6、DialogFragment做屏幕适配

我们希望,一个对话框在大屏幕上以对话框的形式展示,而小屏幕上则直接嵌入当前的Actvity中。这种效果的对话框,只能通过重写onCreateView实现。下面我们利用上面的EditNameDialogFragment来显示。

EditNameDialogFragment我们已经编写好了,直接在MainActivity中写调用

  1. public void showDialogInDifferentScreen(View view)
  2. {
  3. FragmentManager fragmentManager = getFragmentManager();
  4. EditNameDialogFragment newFragment = new EditNameDialogFragment();
  5. boolean mIsLargeLayout = getResources().getBoolean(R.bool.large_layout) ;
  6. Log.e("TAG", mIsLargeLayout+"");
  7. if (mIsLargeLayout )
  8. {
  9. // The device is using a large layout, so show the fragment as a
  10. // dialog
  11. newFragment.show(fragmentManager, "dialog");
  12. } else
  13. {
  14. // The device is smaller, so show the fragment fullscreen
  15. FragmentTransaction transaction = fragmentManager
  16. .beginTransaction();
  17. // For a little polish, specify a transition animation
  18. transaction
  19. .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
  20. // To make it fullscreen, use the 'content' root view as the
  21. // container
  22. // for the fragment, which is always the root view for the activity
  23. transaction.replace(R.id.id_ly, newFragment)
  24. .commit();
  25. }
  26. }

可以看到,我们通过读取R.bool.large_layout,然后根据得到的布尔值,如果是大屏幕则直接以对话框显示,如果是小屏幕则嵌入我们的Activity布局中

这个R.bool.large_layout是我们定义的资源文件:

在默认的values下新建一个bools.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <bool name="large_layout">false</bool>
  4. </resources>

然后在res下新建一个values-large,在values-large下再新建一个bools.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <bool name="large_layout">true</bool>
  4. </resources>

最后测试:

          

左边为模拟器,右边为我的手机~~~~~

7、屏幕旋转

当用户输入帐号密码时,忽然旋转了一下屏幕,帐号密码不见了~~~是不是会抓狂

传统的new AlertDialog在屏幕旋转时,第一不会保存用户输入的值,第二还会报异常,因为Activity销毁前不允许对话框未关闭。而通过DialogFragment实现的对话框则可以完全不必考虑旋转的问题。

我们直接把上面登录使用AlertDialog创建的登录框,拷贝到MainActivity中直接调用:

  1. public void showLoginDialogWithoutFragment(View view)
  2. {
  3. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  4. // Get the layout inflater
  5. LayoutInflater inflater = this.getLayoutInflater();
  6. // Inflate and set the layout for the dialog
  7. // Pass null as the parent view because its going in the dialog layout
  8. builder.setView(inflater.inflate(R.layout.fragment_login_dialog, null))
  9. // Add action buttons
  10. .setPositiveButton("Sign in",
  11. new DialogInterface.OnClickListener()
  12. {
  13. @Override
  14. public void onClick(DialogInterface dialog, int id)
  15. {
  16. // sign in the user ...
  17. }
  18. }).setNegativeButton("Cancel", null).show();
  19. }

下面我分别点击两种方式创建的登录框,看效果图:

可以看到,传统的Dialog旋转屏幕时就消失了,且后台log会报异常~~~使用DialogFragment则不受影响。

好了,关于DialogFragment的介绍结束~~~~

有任何疑问请留言

源码点击下载

android推荐使用dialogFrament而不是alertDialog的更多相关文章

  1. Android开发手记(13) 几种Alertdialog的使用

    本文主要讨论七种形式的AlertDialog,及其编写方法. 1.退出 在用户退出的时候提示用户是否退出,含有“确定”和“退出”两个按键. btnExit.setOnClickListener(new ...

  2. Xamarin.Android 记事本(二)自定义AlertDialog

    导读 1.自定义一个AlertDialog 2.添加一条数据 正文 记事本应当有一个添加功能,这里我打算在右上角放一个item,然后点击这个item弹出一个对话框,输入名称,点击确定跳转到另一个act ...

  3. 学习android推荐网站

    1. Android Developers 作为一个Android开发者,官网的资料当然不可错过,从设计,培训,指南,文档,都不应该错过,在以后的学习过程中慢慢理解体会. 2. Android Gui ...

  4. Android学习总结——系统提示对话框(AlertDialog)

    new AlertDialog.Builder(MainActivity.this).setTitle("退出")//设置对话框标题 .setMessage("官人可是要 ...

  5. 为什么在Python里推荐使用多进程而不是多线程

    转载  http://bbs.51cto.com/thread-1349105-1.html 最近在看Python的多线程,经常我们会听到老手说:"Python下多线程是鸡肋,推荐使用多进程 ...

  6. 为什么在Python里推荐使用多进程而不是多线程?

    最近在看Python的多线程,经常我们会听到老手说:“Python下多线程是鸡肋,推荐使用多进程!”,但是为什么这么说呢?   要知其然,更要知其所以然.所以有了下面的深入研究: 首先强调背景: 1. ...

  7. 为什么在Python里推荐使用多进程而不是多线程?(为什么python多线程无法增加CPU使用率?)

    最近在看Python的多线程,经常我们会听到老手说:“Python下多线程是鸡肋,推荐使用多进程!”,但是为什么这么说呢? 要知其然,更要知其所以然.所以有了下面的深入研究: 首先强调背景:     ...

  8. 为什么在python中推荐使用多进程而不是多线程(转载)

    最近在看Python的多线程,经常我们会听到老手说:"Python下多线程是鸡肋,推荐使用多进程!",但是为什么这么说呢? 要知其然,更要知其所以然.所以有了下面的深入研究: GI ...

  9. python(34):为什么在Python里推荐使用多进程而不是多线程?

    最近在看Python的多线程,经常我们会听到老手说:“Python下多线程是鸡肋,推荐使用多进程!”,但是为什么这么说呢?                  要知其然,更要知其所以然.所以有了下面的深 ...

随机推荐

  1. 【BZOJ3233】【tyvj1729】文艺平衡树

    原题传送门 解题思路:裸平衡树操作,支持区间翻转即可,这里写了无旋treap. 其实平衡树的区间操作就和线段树差不多,你用个标记搞一下就好了,,,,, #include <stdio.h> ...

  2. Codeforces Round #452 F. Letters Removing

    Description Petya has a string of length n consisting of small and large English letters and digits. ...

  3. 【USACO11NOV】牛的阵容Cow Lineup 尺取法+哈希

    题目描述 Farmer John has hired a professional photographer to take a picture of some of his cows. Since ...

  4. 2015 多校联赛 ——HDU5348(搜索)

    Problem Description As we all kown, MZL hates the endless loop deeply, and he commands you to solve ...

  5. bzoj3295[Cqoi2011]动态逆序对 树套树

    3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 5987  Solved: 2080[Submit][Sta ...

  6. 使用json-Server与postman快速模拟服务环境搭建

    在前后端分离的这种工作模式下,分工明确,各司其职.前端负责展示数据,后端提供数据.然而,在这种过程中对于接口的规范 需要提前制定好.例如根据规范提前模拟数据,这个时候就比较麻烦的.JsonServer ...

  7. ChatGirl 一个基于 TensorFlow Seq2Seq 模型的聊天机器人[中文文档]

    ChatGirl 一个基于 TensorFlow Seq2Seq 模型的聊天机器人[中文文档] 简介 简单地说就是该有的都有了,但是总体跑起来效果还不好. 还在开发中,它工作的效果还不好.但是你可以直 ...

  8. python2.7练习小例子(二)

        2):题目:企业发放的奖金根据利润提成.利润(I)低于或等于10万元时,奖金可提10%:利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%:2 ...

  9. JS区分中英文字符的两种方法: 正则和charCodeAt()方法

    1.正则regExpForm.onblur=function(){ entryVal=this.value; entryLen=entryVal.length; cnChar=entryVal.mat ...

  10. linux route 路由设置小记

    情景一: 有一台ip为172.16.160.53服务器,此服务器为固定ip,由于某些特殊情况,此服务器的ip不能修改. 现在这台服务器需要与另外一个网段ip为172.16.176.150服务器进行局域 ...