作为一个拖延症严重类型的病人,终于开始这篇文章了,如果看到这篇文章的朋友,谁有对付拖延症的好的方法,谢谢推荐一下。代码部分昨天完成,其实是在上一周,在Activity(4.0开发环境下,Fragment学习资料:android之Fragment(官网资料翻译))使用AlertDialog,被告知说showDialog(int id)这个方法Deprecated。被弃用了,建议使用DialogFragment。

就开始dialogFragment的学习吧,先看下说明:

A fragment that displays a dialog window, floating on top of its activity's window. This fragment contains a Dialog object, which it displays as appropriate based on the fragment's state. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog.

一个显示一个对话框窗口的Fragment,漂浮在其Activity显示窗口的顶部。这个Fragment包含一个对话框对象,它将基于Fragment的状态适当的选择显示。应该通过API来控制Dialog(决定何时显示、隐藏、销毁),而不是直接调用Dialog。

Implementations should override this class and implement onCreateView(LayoutInflater, ViewGroup, Bundle) to supply the content of the dialog. Alternatively, they can override onCreateDialog(Bundle) to create an entirely custom dialog, such as an AlertDialog, with its own content.

实现DialogFragment是通过继承DialogFragment并且实现 onCreateView(LayoutInflater, ViewGroup, Bundle) 这个方法来提供对话框内容。或者也可以重写 onCreateDialog(Bundle)这个方法来创建一个自定义的对话框,像AlertDialog,有它自己的内容。

下面开始一个简单的例子:

主Activity:

public class DialogFragmentDemoActivity extends Activity implements OnClickListener{/** Called when the activity is first created. */private Button mDialogButton;private Button mAlertDialogButton;private int mStackLevel;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);mDialogButton = (Button)findViewById(R.id.dialog_button);mAlertDialogButton = (Button)findViewById(R.id.alert_dialog_button);mDialogButton.setOnClickListener(this);mAlertDialogButton.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch(v.getId()) {case R.id.dialog_button:showDialog();break;case R.id.alert_dialog_button:showAlertDialog();break;}}void showDialog() {mStackLevel++;// DialogFragment.show() will take care of adding the fragment// in a transaction.  We also want to remove any currently showing// dialog, so make our own transaction and take care of that here.FragmentTransaction ft = getFragmentManager().beginTransaction();Fragment prev = getFragmentManager().findFragmentByTag("dialog");if (prev != null) {ft.remove(prev);}ft.addToBackStack(null);// Create and show the dialog.DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel);newFragment.show(ft, "dialog");}void showAlertDialog() {DialogFragment newFragment = MyAlertDialogFragment.newInstance(R.string.alert_dialog_button);newFragment.show(getFragmentManager(), "dialog");}public void doPositiveClick() {// Do stuff here.Log.i("FragmentAlertDialog", "Positive click!");}public void doNegativeClick() {// Do stuff here.Log.i("FragmentAlertDialog", "Negative click!");}}

主要看下showDialog的注释说明: DialogFragment.show() will take care of adding the fragment in a transaction.  We also want to remove any currently showing dialog, so make our own transaction and take care of that here.

DialogFragment.show()会管理添加到事务里面的Fragment。在这里,我们想要删除当前显示的对话框,就必须用自己的事务进行Fragment管理。

看下MyDialogFragment和MyAlertDialogFragment的定义:

public class MyDialogFragment extends DialogFragment {int mNum;/*** Create a new instance of MyDialogFragment, providing "num"* as an argument.*/static MyDialogFragment newInstance(int num) {MyDialogFragment f = new MyDialogFragment();// Supply num input as an argument.Bundle args = new Bundle();args.putInt("num", num);f.setArguments(args);return f;}@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);mNum = getArguments().getInt("num");// Pick a style based on the num.int style = DialogFragment.STYLE_NORMAL, theme = 0;switch ((mNum-1)%6) {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-1)%6) {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;}setStyle(style, theme);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View v = inflater.inflate(R.layout.fragment_dialog, container, false);View tv = v.findViewById(R.id.text);((TextView)tv).setText("Dialog #" + mNum + ": using style "+ "getNameForNum(mNum)");// Watch for button clicks.Button button = (Button)v.findViewById(R.id.dialog_show);button.setOnClickListener(new OnClickListener() {public void onClick(View v) {// When button is clicked, call up to owning activity.((DialogFragmentDemoActivity)getActivity()).showDialog();}});return v;}
}

在onCreate()方法里面通过num参数来设定DialogFragment的格式,看一下setArgument()这个方法:

Supply the construction arguments for this fragment.

为Fragment提供构造参数。

setStyle()方法:

Call to customize the basic appearance and behavior of the fragment's dialog. This can be used for some common dialog behaviors, taking care of selecting flags, theme, and other options for you. The same effect can be achieve by manually setting Dialog and Window attributes yourself. Calling this after the fragment's Dialog is created will have no effect.

Parameters
style Selects a standard style: may be STYLE_NORMALSTYLE_NO_TITLESTYLE_NO_FRAME, or STYLE_NO_INPUT.
theme Optional custom theme. If 0, an appropriate theme (based on the style) will be selected for you.

在自定义Fragment对话框的外观和行为时调用,通常被用于设定一些常见对话框的行为,像选择Flag、主题以及其他。同样你也可以通过手动选择来设置对话框的属性,但是在Fragment的Dialog创建以后,调用该方法无效。

傲慢的上校原创作品,转载请说明出处:http://blog.csdn.net/aomandeshangxiao/article/details/7790520

再看MyAlertDialogFragment:

public class MyAlertDialogFragment extends DialogFragment {public static MyAlertDialogFragment newInstance(int title) {MyAlertDialogFragment frag = new MyAlertDialogFragment();Bundle args = new Bundle();args.putInt("title", title);frag.setArguments(args);return frag;}@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {int title = getArguments().getInt("title");return new AlertDialog.Builder(getActivity())
//                .setIcon(R.drawable.alert_dialog_icon).setTitle(title).setPositiveButton(R.string.alert_dialog_ok,new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int whichButton) {((DialogFragmentDemoActivity)getActivity()).doPositiveClick();}}).setNegativeButton(R.string.alert_dialog_cancel,new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int whichButton) {((DialogFragmentDemoActivity) getActivity()).doNegativeClick();}}).create();}
}

比较简单,在onCreateDialog里面返回一个自定义的dialog。

找了近一个周的工作,也非常感谢CSDN上面好友们的帮助,现在工作不太好找,请换工作朋友三思。

最后,文件下载地址:http://download.csdn.net/detail/aomandeshangxiao/4456176

大体写道如此,做饭吃饭去了。。。

DialogFragment以及AlertDialogFragment相关推荐

  1. DialogFragment 的使用

    为什么要使用DialogFragment ? 不知道大家有没有这个需求就是当手机屏幕旋转的时候弹框不消失 如果弹框使用AlertDialog 这个时候手机屏幕旋转 弹框会消失的,也就是这个原因自己才把 ...

  2. LDialog基于DialogFragment封装的库,也许是一个Nice的库

    先放上地址GitHub,欢迎star,也欢迎给我发issues LDialog 一个基于Google推荐的DialogFragment封装的的库,根据自身业务提取封装,本库全部使用kotlin编写,j ...

  3. android fragment 弹出对话框,Android DialogFragment弹框实现

    前言: 网上说了一堆好处,我只说自己深有体会的两点吧 1.屏幕横竖屏切换的时候,弹框可以保存状态 2.可以在popwindow中使用 使用步骤 1.创建继承DialogFragment的dialogF ...

  4. Android开发 - 解决DialogFragment在全屏时View被状态栏遮住的问题

    Android开发 - 解决DialogFragment在全屏时View被状态栏遮住的问题 参考文章: (1)Android开发 - 解决DialogFragment在全屏时View被状态栏遮住的问题 ...

  5. Android DialogFragment 遇到 java.lang.IllegalStateException: Fragment already added: 的解决方法

    Android DialogFragment 遇到 java.lang.IllegalStateException: Fragment already added: 的解决方法 参考文章: (1)An ...

  6. DialogFragment源码分析

    2019独角兽企业重金招聘Python工程师标准>>> 目录介绍 1.最简单的使用方法 1.1 官方建议 1.2 最简单的使用方法 1.3 DialogFragment做屏幕适配 2 ...

  7. Android弹窗组件工作机制之Dialog、DialogFragment(二)

    二.Dialog的消失 1.dismiss private final Runnable mDismissAction = this::dismissDialog;public void dismis ...

  8. Android中Dialog与DialogFragment的对比

    2019独角兽企业重金招聘Python工程师标准>>> 最近学习对话框时发现有两种类型的可供使用,一种是Dialog,另一种则是Android 3.0 引入的基于Fragment的D ...

  9. Android开发 - 设置DialogFragment全屏显示

    默认的DialogFragment并不是全屏,但有些需求需要我们将对话框设置为全屏(内容全屏),Android并没有提供直接的API,通过其它不同的方法设置全屏在不同的机型上总有一些诡异的问题,经过测 ...

最新文章

  1. 要活多久才能赚回你交的养老金
  2. 使用EventViewer记录VSTO add-in启动错误
  3. poj1673 EXOCENTER OF A TRIANGLE
  4. B2C和B2B之间有多大差距
  5. ssh(Spring+Spring mvc+hibernate)——EmpDaoImpl.java
  6. C语言写数据库(三)
  7. Microsoft Jet SQL 参考在线手册
  8. Ubuntu 16.04 Hadoop-2.7.3全分布模式 + eclipse hadoop
  9. google android ui,UI Automator
  10. DVWA中国菜刀连接不上问题(低安全级别就连不上,看看这!!)
  11. $$\int_0^{nh}x(x-h)\cdots (x-nh)dx=h^{n+2}\int_0^nx(x-1)\cdots (x-n)dx$$
  12. VI/VIM常用命令
  13. ubuntu 黑体_Ubuntu 10.04下安装字体最简单的方法
  14. ansible安装sipp测压工具
  15. 仿酷狗音乐列表点击item子控件展开功能
  16. UISlider滑条
  17. 电脑配置之CPU性能参数[一]
  18. idea 设置编辑器 table 全部显示
  19. 抖音是怎么动摇美国国本的?
  20. 你觉得一个测试工程师应该具备哪些素质和技能?

热门文章

  1. 【JavaScript实现十进制转换成二进制】
  2. PIL Image创建空白图片的bug
  3. 电脑使用技巧提升篇7:两种方法给U盘加密
  4. python机械学习(一)
  5. c语言 如何对数组取地址,C语言 对数组名取地址
  6. [ FI基本业务流程 ] - FI与MM间的业务集成
  7. xp系统升级Win7系统需要什么条件
  8. 敏捷观点和态度-《敏捷项目管理》读后感
  9. 翁恺《零基础学习Java语言》作业答案 第1周到第7周
  10. 动态站点:部署论坛系统Discuz!