androidUI第二部分---1.2.1Dialog,AlertDialog如何生成
来源:互联网 发布:华为自己的网络 编辑:程序博客网 时间:2024/05/21 15:48
Android.app 包AlertDialog继承至Dialog,Activity中有可选的管理Dialog的几个回调函数onCreateDialog(int) 和onPrepareDialog(int, Dialog) 另外对Dialog有两个接口进行可进行监听android.app.SearchManager.OnDismissListener和android.app.SearchManager.OnCancelListener监听事件触发后调用的方法分别为onDismiss(DialogInterface dialog)和onCancel()
如何创建AlertDialog对象 使用AlertDialog.Builder 子类。通过AlertDialog.Builder(Context)获取一个builder的对象然后使用这个类的方法来设置警告对话框的title,icon等属性。当得到构造器后,通过create()方法来获取警告对话框对象。步骤如下: AlertDialog.Builder builder = new AlertDialog.Builder(this); 生成一个builder对象,然后通过builder对象来setTitle,setPositiveButton,setNegativeButton等等来设置按钮响应事件和title,最后调用 Builder.create()方法生成AlertDialog对象。 public void createDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this);//1步 builder.setIcon(R.drawable.alert_dialog_icon); //2步设置图标,title,button事件等 builder.setTitle("hellohow"); builder.setPositiveButton(R.string.hello, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { setTitle("dialog"); } }); builder.setNegativeButton(R.string.button2, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { setTitle("ca"); } }); dialog= builder.create(); //3步 } 后面想显示的话直接调用dialog 的show()方法即可 Tips:仅仅知道步骤不知道内部原理是不不太解渴,通过分析android源码中AlertDialog类简单了解下生成builder和create()方法究竟做了什么: AlertDialog类中主要有一个AlertController的私有成员,builder为AlertDialog的一个内部类主要有一个AlertController.AlertParams的成员,很明显AlertParams又是AlertController的内部类,简单的类结构应该如下: Class AlertDialog { private AlertController mAlert; public static class Builder { private final AlertController.AlertParams P; } public AlertDialog create() { final AlertDialog dialog = new AlertDialog(P.mContext); P.apply(dialog.mAlert); …… return dialog; } Builder的create()方法中调用p的apply方法就是在将之前设置的title等应用到dialog上,android中源码如下: public void apply(AlertController dialog) { if (mCustomTitleView != null) { dialog.setCustomTitle(mCustomTitleView); } else { if (mTitle != null) { dialog.setTitle(mTitle); } … } if (mPositiveButtonText != null) { dialog.setButton(DialogInterface.BUTTON_POSITIVE, mPositiveButtonText, mPositiveButtonListener, null); } … }
- androidUI第二部分---1.2.1Dialog,AlertDialog如何生成
- androidUI第二部分---1.2.2Activity对Dialog的管理及Dialog的自定义布局
- AndroidUI组件之AlertDialog
- androidUI第四部分--1.4.1ListView用法—利用ArrayAdapter生成ListView
- androidUI第三部分---1.3.1布局管理
- androidUI第五部分---1.5.1notification和notificationmanager的使用
- AndroidUI控件-AlertDialog弹窗控件
- androidUI第四部分---1.4.2ListView用法—利用SimpleAdapter生成ListView
- androidUI第四部分---1.4.3ListView用法—利用CursorAdapter生成ListView
- Android--Dialog(AlertDialog)
- 第二部分:如何配置ActiveMQ
- AndroidUI之如何自定义控件
- android开发-使用Dialog AlertDialog
- android alertdialog布局 dialog布局
- android开发-使用Dialog AlertDialog
- Android之AlertDialog和Dialog
- android alertdialog布局 dialog布局
- android开发-使用Dialog AlertDialog
- 函数模板与类模板
- HGE系列之八管中窥豹(粒子系统)
- insomniac是我觉得最具黑客精神的公司
- window常用命令(待后续补充)
- 快播(Qvod)也开始耍流氓了
- androidUI第二部分---1.2.1Dialog,AlertDialog如何生成
- 生成一棵最少结点,高度为height的AVL树C语言
- [HDU] 3660 Alice and Bob's Trip -- 树形DP?
- Filter路径的匹配模式
- mysql性能优化经验
- 更快更强mysql性能优化宝典
- 正式落户CSDN
- MMU和cache学习
- 数据库并发控制