Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3307067
  • 博文数量: 366
  • 博客积分: 9916
  • 博客等级: 中将
  • 技术积分: 7194
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-29 23:27
个人简介

简单!

文章分类

全部博文(366)

文章存档

2013年(51)

2012年(269)

2011年(46)

分类: 嵌入式

2012-10-23 12:39:22

       顾名思义,DialogFragment就是一个浮动在Activity上面的Fragment。当需要用户的反馈时,它就会派上用场。下面将会展示如何使用DialogFragment,与使用ListFragment类似,需要继承DialogFragment基类。

1、创建一个工程:DialogFragmentExample
2、在包路径下面新建一个类Fragment1
  1. public class Fragment1 extends DialogFragment {
  2.         static Fragment1 newInstance(String title) {
  3.             Fragment1 fragment = new Fragment1();
  4.             Bundle args = new Bundle();
  5.             args.putString("title", title);
  6.             fragment.setArguments(args);
  7.             return fragment;
  8.         }
  9.        
  10.         @Override
  11.         public Dialog onCreateDialog(Bundle savedInstanceState) {
  12.             String title = getArguments().getString("title");
  13.             return new AlertDialog.Builder(getActivity())
  14.             .setIcon(R.drawable.ic_launcher)
  15.             .setTitle(title)
  16.             .setPositiveButton("OK",
  17.                     new DialogInterface.OnClickListener() {
  18.                 public void onClick(DialogInterface dialog,
  19.                         int whichButton) {
  20.                     ((DialogFragmentExampleActivity)
  21.                             getActivity()).doPositiveClick();
  22.                 }
  23.             })
  24.             .setNegativeButton("Cancel",
  25.                     new DialogInterface.OnClickListener() {
  26.                 public void onClick(DialogInterface dialog,
  27.                         int whichButton) {
  28.                     ((DialogFragmentExampleActivity)
  29.                             getActivity()).doNegativeClick();
  30.                 }
  31.             }).create();
  32.         }       
  33.     }
3、DialogFragmentExampleActivity.java中的代码
  1. public class DialogFragmentExampleActivity extends Activity {
  2.         /** Called when the activity is first created. */
  3.         @Override
  4.         public void onCreate(Bundle savedInstanceState) {
  5.             super.onCreate(savedInstanceState);
  6.             setContentView(R.layout.main);
  7.                
  8.             Fragment1 dialogFragment = Fragment1.newInstance(
  9.                     "Are you sure you want to do this?");
  10.                 dialogFragment.show(getFragmentManager(), "dialog");
  11.         }
  12.           
  13.         public void doPositiveClick() {
  14.             //---perform steps when user clicks on OK---
  15.             Log.d("DialogFragmentExample", "User clicks on OK");
  16.         }
  17.        
  18.         public void doNegativeClick() {
  19.             //---perform steps when user clicks on Cancel---
  20.             Log.d("DialogFragmentExample", "User clicks on Cancel");
  21.         }
  22.        
  23.     }
4、按F11在模拟器上调试,会看到一个对话框,点击OK或Cancel按钮会弹出消息。


注:在Android 4.0版本以后,官方推荐使用DialogFragment去替换Dialog。大家可以看看Android 4.0的Launcher等源码,这些源码中大量使用了Fragment类。


阅读(11359) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~