Android中DialogFragment的使用
来源:互联网 发布:岛风go mac版 编辑:程序博客网 时间:2024/05/16 11:23
一.概述:
DialogFragment是Android3.0(API 11)才出现的,它不像以前版本中生成AlertDialog一样,通过AlertDialog.Builder生成Dialog,而是在fragment中弹出Dialog的窗口,即DialogFragment是一个Fragment,而且这个fragment本身就含有一个Dialog对象。
二.创建DialogFragment:
DialogFragment的创建有两种方式:1.用onCreateView()加载Fragment布局而创建;
2.用onCreateDialog()创建;
第二种创建方式可以自定义Dialog,加入想要的内容,如取消/确定/图标/标题。。。类似于AlertDialog;
当创建好DialogFragment之后,在activity中显示,用show(FragmentTransaction transaction, String tag)或者show(FragmentManager manager, String tag)显示即可,tag是定义的一个标签,由自己设置;关于DialogFragment的一些方法和xml属性,可查看API文档进行了解,使用难度并不大;
三.实例:
第一种:用onCreateView()创建:
xml布局中只有一个按钮点击弹出DialogFragment,在MainActivity.java中:
public class MainActivity extends Activity implements OnClickListener {private Button button;private DialogFragmentA dialog;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button = (Button) findViewById(R.id.button);button.setOnClickListener(this);}@Overridepublic void onClick(View v) {DialogFragmentA fragment;fragment = (DialogFragmentA) getFragmentManager().findFragmentByTag("dialog");if (fragment == null) {Bundle bundle = new Bundle();bundle.putString("title", "AlertDialog");fragment = DialogFragmentA.newInstance(bundle);Log.i("jia", "fragment=null");}FragmentTransaction ft = getFragmentManager().beginTransaction();fragment.show(ft, "dialog");</span> //fragment.show(getFragmentManager(), "dialog");}}
在DialogFragmentA中:
public class DialogFragmentA extends DialogFragment {private TextView textView;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view = inflater.inflate(R.layout.dialoga, container, false);textView = (TextView) view.findViewById(R.id.textView1);Bundle bundle = getArguments();String title = bundle.getString("title");textView.setText(title);return view;}public static DialogFragmentA newInstance(Bundle bundle) {DialogFragmentA fragment = new DialogFragmentA();fragment.setArguments(bundle);// DialogFragment的样式:无标题fragment.setStyle(DialogFragment.STYLE_NO_TITLE, 0);return fragment;}}
这种方式创建的DialogFragment会根据Fragment的布局生成样式。
第二种方式:用onCreateDialog()创建:
在DialogFragmentA中:
public class DialogFragmentA extends DialogFragment {public static DialogFragmentA newInstance(Bundle bundle) {DialogFragmentA fragment = new DialogFragmentA();fragment.setArguments(bundle);return fragment;}@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {// TODO Auto-generated method stubreturn new AlertDialog.Builder(getActivity()).setTitle("title").setMessage("您确定要退出吗?").setIcon(R.drawable.ic_launcher).setPositiveButton("yes", new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {getActivity().finish();}}).setNegativeButton("no", null).create();}}
和AlertDialog的创建过程是一样的。
四.运行效果:(由于第一种方式自定义布局时只定义了一个TextView,所以这里只给出第二种的运行效果)
0 0
- Android中DialogFragment的使用
- android中DialogFragment的一些使用笔记
- Android:DialogFragment的使用
- Android-----DialogFragment的使用
- Android中DialogFragment使用示例
- Android中使用DialogFragment 来创建对话框
- Android的DialogFragment的简单使用。
- Android中Dialog与DialogFragment的对比
- Android中Dialog与DialogFragment的对比
- Android中Dialog与DialogFragment的对比
- android DialogFragment简单使用
- ApiDemos--DialogFragment的使用
- DialogFragment的使用示例
- DialogFragment的相关使用
- Dialogfragment的使用
- dialogfragment 的使用
- DialogFragment && NumberPicker的使用
- DialogFragment的使用
- 基于 Python Matplotlib 模块的高质量图形输出
- HDU 1002 A + B Problem II
- coursera机器学习技法笔记(5-6)——将核方法应用于回归
- 《Linux内核设计与实现》——第4章(进程调度)
- 杭电1032:The 3n + 1 problem
- Android中DialogFragment的使用
- UITextKit框架的学习(二)
- Leetcode Edit Distance
- Android Jni 开发(Android studio)
- 绘图: matplotlib核心剖析
- 保留浮点数的两位小数点
- matplotlib-绘制精美的图表
- HDU 1176 免费馅饼(动规)
- SGU 144 Meeting(水~)