近日,对android文档中的对话框进行了研究,简单的进行记录。

A dialog is usually a small window that appears in front of the current Activity. The underlying Activity loses focus and the dialog accepts all user interaction. Dialogs are normally used for notifications that should interupt the user and to perform short tasks that directly relate to the application in progress (such as a progress bar or a login prompt).

android中得dialog,实际上是出现在当前activity上的一个小窗口,会使下面的activity失去焦点,然后使对话框和用户进行交互,对话框通常用于一个通知或者打断用户,或者执行简短的任务。

dialog分为:AlertdialogProgressDialogDatePickerDialogTimePickerDialog

Android DOC文档分析——Dialog_第1张图片

如图可清晰的了解到,dialog之间的关系。在这,省略了这几种dialog的使用方法。

Showing a Dialog

很简单,我们可以通过调用onCreateDialog(int)方法去建立一个dialog,建立的时候我们需要给每个dialog定义一个ID,然后通过,showDialog(int)将dialog显示在屏幕上。

Before the dialog is displayed, Android also calls the optional callback methodonPrepareDialog(int, Dialog). Define this method if you want to change any properties of the dialog each time it is opened. This method is called every time a dialog is opened, whereas onCreateDialog(int) is only called the very first time a dialog is opened. If you don't defineonPrepareDialog(), then the dialog will remain the same as it was the previous time it was opened. This method is also passed the dialog's ID, along with the Dialog object you created inonCreateDialog().

在dialog显示出来之前,Android 同样会调用onPrepareDialog(int ,Dialog),这个方法可以帮助你在每次调用dialog前改变一些属性,这个方法会在每次打开dialog前都被调用。相反的,onCreateDialog(int) 仅仅在第一个调用这个dialog前调用,如果你没有调用onPrepareDialog(int , Dialog) ,那么之后的每次打开都会和第一次打开一样。

个人理解,就是dialog对象只有在第一次打开时被创建,然后存放在activity里,如果不调用onPrepareDialog(int ,Dialog)这个方法,那么这个对象就没有改变,相反,如果调用,就可以在这个方法里对之前生成的dialog对象进行一些修改。

Dissmissing a Dialog

When you're ready to close your dialog, you can dismiss it by callingdismiss() on the Dialog object. If necessary, you can also calldismissDialog(int) from the Activity, which effectively callsdismiss() on the Dialog for you.

可以通过dialog对象调用dialog.dissmiss(),如果必要也可以通过activity调用 dismissDialog(int)来让对话框消失。

If you are using onCreateDialog(int) to manage the state of your dialogs (as discussed in the previous section), then every time your dialog is dismissed, the state of the Dialog object is retained by the Activity. If you decide that you will no longer need this object or it's important that the state is cleared, then you should callremoveDialog(int). This will remove any internal references to the object and if the dialog is showing, it will dismiss it.

通过onCreateDialog(int) 管理dialog,每次当你的dialog dismissed时候,那么这个dialog的对象将由Activity管理,如果你决定你将不再使用这个对象,可以使用removeDialog(int) 。这个将移除这个对象的任何内部引用,如果这个当前dialog显示状态,那么他将dismiss掉。调用removeDialog(int) 之后,再次打开dialog时,会重新调用onCreateDialog(int)这个方法。

If you'd like your application to perform some procedures the moment that a dialog is dismissed, then you should attach an on-dismiss listener to your Dialog.

当你想在dialog dismiss时执行一些程序,那么就监听Dialog的dismiss事件。

First define the DialogInterface.OnDismissListener interface. This interface has just one method,onDismiss(DialogInterface), which will be called when the dialog is dismissed. Then simply pass your OnDismissListener implementation to setOnDismissListener().

定义DialogInterface.OnDismissListener接口,这个接口只有一个方法,实现这个方法,来完成dismiss时的动作,

dialog的两个方法,cancel()dismiss()基本实现了同样的功能,cancel()会调用dismiss()。但是cancel()同事也会调用DialogInterface.OnCancelListener这个方法,如果注册了的话。。

Creating a Custom Dialog

If you want a customized design for a dialog, you can create your own layout for the dialog window with layout and widget elements. After you've defined your layout, pass the root View object or layout resource ID tosetContentView(View).

For example, to create the dialog shown to the right:

想定义一个自定义的dialog,你可以建立为这个dialog建立自己的布局和小元素,在定义layout之后,把根视图对象或者布局的资源ID传到 setContentView(View)中,下面就简单介绍一下如何创建一个如下图所示的dialog。

Android DOC文档分析——Dialog_第2张图片

1.Create an XML layout saved ascustom_dialog.xml:

This XML defines an ImageView and a TextViewinside a LinearLayout.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/layout_root"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="horizontal"    android:padding="10dp" >    <ImageView        android:id="@+id/image"        android:layout_width="wrap_content"        android:layout_height="fill_parent"        android:layout_marginRight="10dp" />    <TextView        android:id="@+id/text"        android:layout_width="wrap_content"        android:layout_height="fill_parent"        android:textColor="#FFF" /></LinearLayout>



2.Set the above layout as the dialog's content view and define the content for the ImageView and TextView elements:

After you instantiate the Dialog, set your custom layout as the dialog's content view withsetContentView(int), passing it the layout resource ID. Now that the Dialog has a defined layout, you can capture View objects from the layout withfindViewById(int) and modify their content.

在实例化Dialog之后,把自己的自定义布局set到dialog的content view中,传入布局的ID,现在这个dialog已经定义了布局,你可以通过findviewbyid来修改它们。

Context mContext = getApplicationContext();   Dialog dialog = new Dialog(mContext);    dialog.setContentView(R.layout.custom_dialog); dialog.setTitle("Custom Dialog");  TextView text = (TextView) dialog.findViewById(R.id.text);   text.setText("Hello, this is a custom dialog!"); ImageView image = (ImageView) dialog.findViewById(R.id.image);  image.setImageResource(R.drawable.android);


3.That's it. You can now show the dialog as described inShowing A Dialog.

A dialog made with the base Dialog class must have a title. If you don't callsetTitle(), then the space used for the title remains empty, but still visible. If you don't want a title at all, then you should create your custom dialog using theAlertDialog class. However, because an AlertDialog is created easiest with theAlertDialog.Builder class, you do not have access to thesetContentView(int) method used above. Instead, you must usesetView(View). This method accepts aView object, so you need to inflate the layout's root View object from XML.

To inflate the XML layout, retrieve the LayoutInflater withgetLayoutInflater() (orgetSystemService()), and then callinflate(int, ViewGroup), where the first parameter is the layout resource ID and the second is the ID of the root View. At this point, you can use the inflated layout to find View objects in the layout and define the content for the ImageView and TextView elements. Then instantiate the AlertDialog.Builder and set the inflated layout for the dialog withsetView(View).

如果这个dialog继承一个普通的dialog,那么就必须有一个标题,如果不调用 setTitle() ,那么这个空间是空的,但是仍然可见,如果你不想要题目,那么就建立一个自定义dialog继承 AlertDialog 。因为AlertDialog 可以通过AlertDialog.Builder 建立,那么就不需要调用setContentView(int)这个方法,代替他得时用setView(View)。这个方法接收一个View的对象,所以需要infalte XML来得到这个View对象。

inflate XML 布局,通过LayoutInflater 和 getLayoutInflater()(或者 getSystemService() ),然后调用flate(int ,ViewGroup),第一个参数是这个布局文件的ID,第二个参数为这个布局的根View的ID,然后把这个inflate的布局传递给setView( View )这个方法。

下面是一个自定义AlertDialog 的例子。

AlertDialog.Builder builder;        AlertDialog alertDialog;Context mContext = getApplicationContext();LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);View layout = inflater.inflate(R.layout.custom_dialog,(ViewGroup) findViewById(R.id.layout_root));TextView text = (TextView) layout.findViewById(R.id.text);text.setText("Hello, this is a custom dialog!");ImageView image = (ImageView) layout.findViewById(R.id.image);image.setImageResource(R.drawable.android);builder = new AlertDialog.Builder(mContext);builder.setView(layout);alertDialog = builder.create();

Using an AlertDialog for your custom layout lets you take advantage of built-in AlertDialog features like managed buttons, selectable lists, a title, an icon and so on.

使用AlertDialog的自定义布局,可以更好的利用AlertDialog的部件,比如管理按钮,选择列表,题目,图标等等。

For more information, refer to the reference documentation for theDialog andAlertDialog.Builder classes.

想了解更多的信息,请参看Dialog和AlertDialog.Builder这两篇文档。

转载请标明出处:http://blog.csdn.net/jamin0107/article/details/7056792

更多相关文章

  1. Android Notification 填充 自定义布局
  2. android中opengl es基本方法使用说明
  3. 关于android 布局中诡异的AttributeSet 搜索记录
  4. android 沉浸式状态栏的三种方法
  5. Android中添加布局和初始化布局总结
  6. JS判断终端类型的几种方法
  7. android 使用Intent传递对象 Serializable 或者 Parcelabel 《第
  8. Android非UI线程访问UI线程的方法总结

随机推荐

  1. Android(安卓)用intent传递ArrayList对象
  2. Android(安卓)输入系统解析 (2)
  3. android模仿微信朋友圈图片预览转场缩放
  4. 关于 AndroidJavaCompile.setDependencyC
  5. Android的Layout --- 布局
  6. 两分钟彻底让你明白Android(安卓)Activit
  7. Android(安卓)动画 - ScaleAnimation 缩
  8. Android(安卓)水波纹扩散效果
  9. Android通过设置颜色的透明度来获取颜色
  10. 打开Android(安卓)Studio报错 "required