androidUI第二部分---1.2.1Dialog,AlertDialog如何生成

Android.app AlertDialog继承至Dialog,Activity中有可选的管理Dialog的几个回调函数onCreateDialog(int) onPrepareDialog(int, Dialog)  另外对Dialog有两个接口进行可进行监听android.app.SearchManager.OnDismissListenerandroid.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对象来setTitlesetPositiveButtonsetNegativeButton等等来设置按钮响应事件和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类简单了解下生成buildercreate()方法究竟做了什么:

AlertDialog类中主要有一个AlertController的私有成员,builderAlertDialog的一个内部类主要有一个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;

 

}

 Buildercreate()方法中调用papply方法就是在将之前设置的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如何生成)