AndroidGUI14:各种Dialog常用技巧

<!-- [if !mso]> <style> v/:* {behavior:url(#default#VML);} o/:* {behavior:url(#default#VML);} w/:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} </style> <![endif]--><!-- [if gte mso 9]><xml> <o:OfficeDocumentSettings> <o:RelyOnVML/> <o:AllowPNG/> </o:OfficeDocumentSettings> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:TrackMoves>false</w:TrackMoves> <w:TrackFormatting/> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:DoNotPromoteQF/> <w:LidThemeOther>EN-US</w:LidThemeOther> <w:LidThemeAsian>ZH-CN</w:LidThemeAsian> <w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:SplitPgBreakAndParaMark/> <w:DontVertAlignCellWithSp/> <w:DontBreakConstrainedForcedTables/> <w:DontVertAlignInTxbx/> <w:Word11KerningPairs/> <w:CachedColBalance/> <w:UseFELayout/> </w:Compatibility> <m:mathPr> <m:mathFont m:val="Cambria Math"/> <m:brkBin m:val="before"/> <m:brkBinSub m:val="--"/> <m:smallFrac m:val="off"/> <m:dispDef/> <m:lMargin m:val="0"/> <m:rMargin m:val="0"/> <m:defJc m:val="centerGroup"/> <m:wrapIndent m:val="1440"/> <m:intLim m:val="subSup"/> <m:naryLim m:val="undOvr"/> </m:mathPr></w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" DefUnhideWhenUsed="true" DefSemiHidden="true" DefQFormat="false" DefPriority="99" LatentStyleCount="267"> <w:LsdException Locked="false" Priority="0" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Normal"/> <w:LsdException Locked="false" Priority="9" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="heading 1"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 2"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 3"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 4"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 5"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 6"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 7"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 8"/> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 9"/> <w:LsdException Locked="false" Priority="39" Name="toc 1"/> <w:LsdException Locked="false" Priority="39" Name="toc 2"/> <w:LsdException Locked="false" Priority="39" Name="toc 3"/> <w:LsdException Locked="false" Priority="39" Name="toc 4"/> <w:LsdException Locked="false" Priority="39" Name="toc 5"/> <w:LsdException Locked="false" Priority="39" Name="toc 6"/> <w:LsdException Locked="false" Priority="39" Name="toc 7"/> <w:LsdException Locked="false" Priority="39" Name="toc 8"/> <w:LsdException Locked="false" Priority="39" Name="toc 9"/> <w:LsdException Locked="false" Priority="35" QFormat="true" Name="caption"/> <w:LsdException Locked="false" Priority="10" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Title"/> <w:LsdException Locked="false" Priority="1" Name="Default Paragraph Font"/> <w:LsdException Locked="false" Priority="11" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Subtitle"/> <w:LsdException Locked="false" Priority="22" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Strong"/> <w:LsdException Locked="false" Priority="20" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Emphasis"/> <w:LsdException Locked="false" Priority="59" SemiHidden="false" UnhideWhenUsed="false" Name="Table Grid"/> <w:LsdException Locked="false" UnhideWhenUsed="false" Name="Placeholder Text"/> <w:LsdException Locked="false" Priority="1" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="No Spacing"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 1"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 1"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 1"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 1"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 1"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 1"/> <w:LsdException Locked="false" UnhideWhenUsed="false" Name="Revision"/> <w:LsdException Locked="false" Priority="34" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="List Paragraph"/> <w:LsdException Locked="false" Priority="29" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Quote"/> <w:LsdException Locked="false" Priority="30" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Intense Quote"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 1"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 1"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 1"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 1"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 1"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 1"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 1"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 1"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 2"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 2"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 2"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 2"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 2"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 2"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 2"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 2"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 2"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 2"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 2"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 2"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 2"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 2"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 3"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 3"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 3"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 3"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 3"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 3"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 3"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 3"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 3"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 3"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 3"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 3"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 3"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 3"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 4"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 4"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 4"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 4"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 4"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 4"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 4"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 4"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 4"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 4"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 4"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 4"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 4"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 4"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 5"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 5"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 5"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 5"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 5"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 5"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 5"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 5"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 5"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 5"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 5"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 5"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 5"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 5"/> <w:LsdException Locked="false" Priority="60" SemiHidden="false" UnhideWhenUsed="false" Name="Light Shading Accent 6"/> <w:LsdException Locked="false" Priority="61" SemiHidden="false" UnhideWhenUsed="false" Name="Light List Accent 6"/> <w:LsdException Locked="false" Priority="62" SemiHidden="false" UnhideWhenUsed="false" Name="Light Grid Accent 6"/> <w:LsdException Locked="false" Priority="63" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 1 Accent 6"/> <w:LsdException Locked="false" Priority="64" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Shading 2 Accent 6"/> <w:LsdException Locked="false" Priority="65" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 1 Accent 6"/> <w:LsdException Locked="false" Priority="66" SemiHidden="false" UnhideWhenUsed="false" Name="Medium List 2 Accent 6"/> <w:LsdException Locked="false" Priority="67" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 1 Accent 6"/> <w:LsdException Locked="false" Priority="68" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 2 Accent 6"/> <w:LsdException Locked="false" Priority="69" SemiHidden="false" UnhideWhenUsed="false" Name="Medium Grid 3 Accent 6"/> <w:LsdException Locked="false" Priority="70" SemiHidden="false" UnhideWhenUsed="false" Name="Dark List Accent 6"/> <w:LsdException Locked="false" Priority="71" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Shading Accent 6"/> <w:LsdException Locked="false" Priority="72" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful List Accent 6"/> <w:LsdException Locked="false" Priority="73" SemiHidden="false" UnhideWhenUsed="false" Name="Colorful Grid Accent 6"/> <w:LsdException Locked="false" Priority="19" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Subtle Emphasis"/> <w:LsdException Locked="false" Priority="21" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Intense Emphasis"/> <w:LsdException Locked="false" Priority="31" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Subtle Reference"/> <w:LsdException Locked="false" Priority="32" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Intense Reference"/> <w:LsdException Locked="false" Priority="33" SemiHidden="false" UnhideWhenUsed="false" QFormat="true" Name="Book Title"/> <w:LsdException Locked="false" Priority="37" Name="Bibliography"/> <w:LsdException Locked="false" Priority="39" QFormat="true" Name="TOC Heading"/> </w:LatentStyles> </xml><![endif]--><!-- [if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.5pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-font-kerning:1.0pt;} </style> <![endif]-->

Android 中,对话框有以下五种:

+ Dialog

+ AlertDialog

+ CharacterPickerDialog

+ DatePickerDialog

+ ProgressDialog

其中 Dialog 是所有对话框的基类。 AlertDialogCharacterPickerDialogDialog 的直接派生类, DatePickerDialogProgressDialogTimePickerDialogDialog 的间接派生类。 CharacterPickerDialog 的用处感觉不大,而且在 Android2.2 之前的版本似乎有 bug

下面文字中 对话框 的含义,包括 Dialog 自己及其所有的派生类。

+ 对话框必须在使用它的 Activity 中定义;

+ Activity 中有三个和对话框显示有关的方法,即 showDialog() dismissDialog()removeDialog() 。其中 showDialog() 方法用于显示一个对话框; dismissDialog() 使对话框消失,但仍然处于内存中,只是不显示而已,如果再次调用 showDialog() 方法,则缓存在内容的对话框,会重新显示,而不需要重新创建。 removeDialog() 使对话框消失,并从内存中将对话框清除,如果再次调用 showDialog() 方法来显示它,则在显示之前需要重新创建该对话框。相关函数的原型如下:

public final void showDialog (int id)

public final boolean showDialog (int id, Bundle args)

public final void dismissDialog (int id)

public final void removeDialog (int id)

其中第一个参数 id ,均为对话框所对应的 ID

+ Activity 中增加一个对话框的步骤大致如下:

- Activity 中为对话框定义唯一的标识符 (ID)

- 重写 Activity 中的 onCreateDialog() 方法,并返回一个对话框实例

- 重写 Activity 中的 onPrepareDialog() 方法。该方法不是必须重写的,但可以在该方法中为相关的对话框改变一些属性设置

- 调用 showDialog() 方法,显示该对话框

需要注意的是,在一个对话框的声明周期内, onCreateDialog() 仅被调用一次,但 onPrepareDialog() 方法可以被多次调用。比如,第一次调用 showDialog() 方法显示某个对话框时,会先触发 onCreateDialog() 方法的执行,然后再触发 onPrepareDialog() 方法的执行;如果将某个对话框 removeDialog() 后,再次用 showDialog() 显示它是,也会先触发 onCreateDialog() 方法的执行,然后再触发 onPrepareDialog() 方法的执行;如果某个对话框被 dismissDialog() 在屏幕上消失后,在调用 showDialog() 方法来显示它的话,则不会触发 onCreateDialog() 方法的执行,但会触发 onPrepareDialog() 方法的执行。正因为如此,可以在该方法中为相关的对话框 ( 同一个 ID) 改变一些属性设置。

+ Dialog 中也存在功能分别和 Activity 中的 showDialog()dismissDialog() 方法一样的方法,它们是 show()dismiss() ,其原型如下:

public void show ()

public void dismiss ()

但没有对应的 remove() 方法,理由很简单, removeDialog() 方法调用后,对话框实例已经不存在了,所以此时在对话框中的 remove 方法已经没有意义了。


1. 创建 Android Project ,修改其 main.xml ,使之如下:

<? xml version = "1.0" encoding = "utf-8" ?>

< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"

android:orientation = "vertical"

android:layout_width = "fill_parent"

android:layout_height = "fill_parent"

android:gravity = "center_horizontal"

>

< Button

android:id = "@+id/show_button1"

android:layout_width = "220px"

android:layout_height = "38px"

android:text = "Dialog (1)"

/>

< Button

android:id = "@+id/show_button2"

android:layout_width = "220px"

android:layout_height = "38px"

android:text = "AlertDialog (2)"

/>

< Button

android:id = "@+id/show_button3"

android:layout_width = "220px"

android:layout_height = "38px"

android:text = "AlertDialog (3)"

/>

< Button

android:id = "@+id/show_button4"

android:layout_width = "220px"

android:layout_height = "38px"

android:text = "AlertDialog (4)"

/>

< Button

android:id = "@+id/show_button5"

android:layout_width = "220px"

android:layout_height = "38px"

android:text = "AlertDialog (5)"

/>

< Button

android:id = "@+id/show_button6"

android:layout_width = "220px"

android:layout_height = "38px"

android:text = "AlertDialog (6)"

/>

< Button

android:id = "@+id/show_button7"

android:layout_width = "220px"

android:layout_height = "38px"

android:text = "CharacterPickerDialog (7)"

/>

< Button

android:id = "@+id/show_button8"

android:layout_width = "220px"

android:layout_height = "38px"

android:text = "DatePickerDialog (8)"

/>

< Button

android:id = "@+id/show_button9"

android:layout_width = "220px"

android:layout_height = "38px"

android:text = "TimePickerDialog (9)"

/>

< Button

android:id = "@+id/show_button10"

android:layout_width = "220px"

android:layout_height = "38px"

android:text = "ProgressDialog (10)"

/>

< Button

android:id = "@+id/show_button11"

android:layout_width = "220px"

android:layout_height = "38px"

android:text = "Customized Dialog (11)"

/>

</ LinearLayout >

2. 创建一个 layout 文件: custom_dialog.xml ,使其内容如下:

<? xml version = "1.0" encoding = "utf-8" ?>

< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"

android:layout_height = "wrap_content"

android:layout_width = "wrap_content"

android:background = "#ffffffff"

android:orientation = "horizontal"

android:gravity = "center_horizontal"

android:id = "@+id/dialog" >

< TextView

android:layout_height = "wrap_content"

android:layout_width = "wrap_content"

android:id = "@+id/tvname"

android:text = " 姓名 "

/>

< EditText

android:layout_height = "wrap_content"

android:layout_width = "wrap_content"

android:id = "@+id/etname"

android:minWidth = "140px"

/>

</ LinearLayout >

这个 layout 将用于自定义的对话框。

3. 修改 Activity 所对应的代码,使之如下:

package com.pnf.gui;

import android.app.Activity;

import android.app.AlertDialog;

import android.app.DatePickerDialog;

import android.app.Dialog;

import android.app.ProgressDialog;

import android.app.TimePickerDialog;

import android.content.DialogInterface;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.text.method.CharacterPickerDialog;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.AdapterView;

import android.widget.Button;

import android.widget.DatePicker;

import android.widget.EditText;

import android.widget.TimePicker;

import android.widget.Toast;

public class ControlDialog extends Activity

implements

OnClickListener

{

// 声明一个 Dialog 对象

private Dialog dialog = null ;

// 声明 11Button 对象 , 他们分别用于显示 11 个不同的对话框

private Button show_button1 ;

private Button show_button2 ;

private Button show_button3 ;

private Button show_button4 ;

private Button show_button5 ;

private Button show_button6 ;

private Button show_button7 ;

private Button show_button8 ;

private Button show_button9 ;

private Button show_button10 ;

private Button show_button11 ;

// 11 个不同的对话框设定 ID

private static final int ID_DIALOG1 = 1;

private static final int ID_DIALOG2 = 2;

private static final int ID_DIALOG3 = 3;

private static final int ID_DIALOG4 = 4;

private static final int ID_DIALOG5 = 5;

private static final int ID_DIALOG6 = 6;

private static final int ID_DIALOG7 = 7;

private static final int ID_DIALOG8 = 8;

private static final int ID_DIALOG9 = 9;

private static final int ID_DIALOG10 = 10;

private static final int ID_DIALOG11 = 11;

// 用于保存 IDID_DIALOG5 的对话框的状态

private boolean id_dialog5 [] = { false , false };

// 用于保存 IDID_DIALOG6 的对话框的状态

private int id_dialog6 = 0;

// 定义一个 Handler 对象 , 用于更新 ProgressDialog(IDID_DIALOG10) 中的进度条

// 相关做法可以参考 : 《 AndroidGUI06ProgressBar 常用技巧》

private Handler progressHandler = new Handler()

{

public void handleMessage(Message msg) // 重写 handleMessage 方法

{

((ProgressDialog) dialog ).setProgress(msg. what * 10);

if (msg. what == 1000) dismissDialog( ID_DIALOG10 );

}

};

public void onCreate(Bundle savedInstanceState)

{

super .onCreate(savedInstanceState);

setContentView(R.layout. main );

// 获取 11 按钮对象 , 并分别为他们设置 OnClickListener

show_button1 = (Button) this .findViewById(R.id. show_button1 );

show_button1 .setOnClickListener( this );

show_button2 = (Button) this .findViewById(R.id. show_button2 );

show_button2 .setOnClickListener( this );

show_button3 = (Button) this .findViewById(R.id. show_button3 );

show_button3 .setOnClickListener( this );

show_button4 = (Button) this .findViewById(R.id. show_button4 );

show_button4 .setOnClickListener( this );

show_button5 = (Button) this .findViewById(R.id. show_button5 );

show_button5 .setOnClickListener( this );

show_button6 = (Button) this .findViewById(R.id. show_button6 );

show_button6 .setOnClickListener( this );

show_button7 = (Button) this .findViewById(R.id. show_button7 );

show_button7 .setOnClickListener( this );

show_button8 = (Button) this .findViewById(R.id. show_button8 );

show_button8 .setOnClickListener( this );

show_button9 = (Button) this .findViewById(R.id. show_button9 );

show_button9 .setOnClickListener( this );

show_button10 = (Button) this .findViewById(R.id. show_button10 );

show_button10 .setOnClickListener( this );

show_button11 = (Button) this .findViewById(R.id. show_button11 );

show_button11 .setOnClickListener( this );

}

// OnClick 方法中 , 调用 showDialog(int id, Bundle args) 显示一个对话框时 ,

// showDialog 的两个参数 , 会分别传递给 onCreateDialog

protected Dialog onCreateDialog( int id, Bundle args)

{

// 定义一个 AlertDialog.Builder 对象。创建 AlertDialog 时,都要用到这个

// AlertDialog.Builder 对象。自定义的对话框,本质上也是一个 AlertDialog ,

// 因此创建自定义对话框,也需要用到 AlertDialog.Builder

AlertDialog.Builder builder;

switch (id)

{

case ID_DIALOG1 : // 创建一个基本的对话框

dialog = new Dialog( this );

break ;

case ID_DIALOG2 : // 创建一个 AlertDialog , 有两个按钮

builder = new AlertDialog.Builder(ControlDialog. this );

builder.setTitle( "Prompt" );

builder.setMessage( "Are you sure to quit?" );

builder.setPositiveButton( "OK" , new DialogInterface.OnClickListener()

{

public void onClick(DialogInterface dialog, int which)

{

// which=-1(BUTTON_POSITIVE) , 说明是 positive 按钮

// which=-2(BUTTON_NEGATIVE) , 说明是 negative 按钮

Log.e ( "AlertDialog" , dialog.toString()+ " " + which);

// do something else

dialog.dismiss(); // 可以没有这句

}

});

builder.setNegativeButton( "Cancel" , new DialogInterface.OnClickListener()

{

public void onClick(DialogInterface dialog, int which)

{

// which=-1(BUTTON_POSITIVE) , 说明是 positive 按钮

// which=-2(BUTTON_NEGATIVE) , 说明是 negative 按钮

Log.e ( "AlertDialog" , dialog.toString()+ " " + which);

// do something else

dialog.dismiss(); // 可以没有这句

}

});

dialog = builder.create();

break ;

case ID_DIALOG3 : // 创建一个 AlertDialog , 有三个按钮

builder = new AlertDialog.Builder(ControlDialog. this );

builder.setIcon(android.R.drawable. btn_star );

builder.setTitle( "Survey of Favorite Star" );

builder.setMessage( "Do you like Jet Lee?" );

builder.setPositiveButton( "Great!" , new DialogInterface.OnClickListener()

{ // which=-1(BUTTON_POSITIVE) , 说明是 positive 按钮

// which=-2(BUTTON_NEGATIVE) , 说明是 negative 按钮

// which=-3(BUTTON_NEUTRAL) , 说明是 neutral 按钮

public void onClick(DialogInterface dialog, int which)

{

Toast.makeText (ControlDialog. this , "I love him..." , Toast. LENGTH_LONG ).show();

dialog.dismiss(); // 可以没有这句

}

});

builder.setNegativeButton( "Hate!" , new DialogInterface.OnClickListener()

{ // which=-1(BUTTON_POSITIVE) , 说明是 positive 按钮

// which=-2(BUTTON_NEGATIVE) , 说明是 negative 按钮

// which=-3(BUTTON_NEUTRAL) , 说明是 neutral 按钮

public void onClick(DialogInterface dialog, int which)

{

Toast.makeText (ControlDialog. this , "I hate him..." , Toast. LENGTH_LONG ).show();

dialog.dismiss(); // 可以没有这句

}

});

builder.setNeutralButton( "OK!" , new DialogInterface.OnClickListener()

{ // which=-1(BUTTON_POSITIVE) , 说明是 positive 按钮

// which=-2(BUTTON_NEGATIVE) , 说明是 negative 按钮

// which=-3(BUTTON_NEUTRAL) , 说明是 neutral 按钮

public void onClick(DialogInterface dialog, int which)

{

Toast.makeText (ControlDialog. this , "Let me think..." , Toast. LENGTH_LONG ).show();

dialog.dismiss(); // 可以没有这句

}

});

dialog = builder.create();

break ;

case ID_DIALOG4 : // 创建一个 AlertDialog , 有一个按钮和一个输入框

builder = new AlertDialog.Builder(ControlDialog. this );

builder.setIcon(android.R.drawable. ic_dialog_info );

builder.setTitle( "Please input something" );

final EditText edit = new EditText( this );

builder.setView(edit);

builder.setPositiveButton( "OK" , new DialogInterface.OnClickListener()

{

public void onClick(DialogInterface dialog, int which)

{

Toast.makeText (ControlDialog. this , edit.getText().toString(), Toast. LENGTH_LONG ).show();

dialog.dismiss(); // 可以没有这句

}

});

dialog = builder.create();

break ;

case ID_DIALOG5 : // 创建一个 AlertDialog , 有一个按钮和两个 CheckBox

builder = new AlertDialog.Builder(ControlDialog. this );

builder.setIcon(android.R.drawable. ic_dialog_info );

builder.setTitle( "CheckBox in AlertDialog" );

builder.setMultiChoiceItems( new String[]{ "Item1" , "Item2" },

id_dialog5 , // id_dialog5 数组记录 CheckBox 的状态

new DialogInterface.OnMultiChoiceClickListener()

{

public void onClick(DialogInterface dialog, int which, boolean isChecked)

{

switch (which)

{

case 0:

if (isChecked) id_dialog5 [0] = true ;

else id_dialog5 [0] = false ;

break ;

case 1:

if (isChecked) id_dialog5 [1] = true ;

else id_dialog5 [1] = false ;

break ;

}

}

});

builder.setPositiveButton( "OK" , new DialogInterface.OnClickListener()

{

public void onClick(DialogInterface dialog, int which)

{

String tmpstr = "" ;

if ( id_dialog5 [0]) tmpstr = "Item1 is checked" ;

else tmpstr = "Item1 is not checked." ;

tmpstr += "/n" ;

if ( id_dialog5 [1]) tmpstr += "Item2 is checked" ;

else tmpstr += "Item2 is not checked." ;

Toast.makeText (ControlDialog. this , tmpstr, Toast. LENGTH_LONG ).show();

dialog.dismiss(); // 可以没有这句

}

});

dialog = builder.create();

break ;

case ID_DIALOG6 : // 创建一个 AlertDialog ,有一个按钮和两个 RadioButton

builder = new AlertDialog.Builder(ControlDialog. this );

builder.setIcon(android.R.drawable. ic_dialog_info );

builder.setTitle( "RadioButton in AlertDialog" );

builder.setSingleChoiceItems( new String[]{ "Item1" , "Item2" },

id_dialog6 , // 用 id_dialog6 记录那个 RadioButton 被选中

new DialogInterface.OnClickListener()

{

public void onClick(DialogInterface dialog, int which)

{

id_dialog6 = which;

}

});

builder.setPositiveButton( "OK" , new DialogInterface.OnClickListener()

{

public void onClick(DialogInterface dialog, int which)

{

String tmpstr = "" ;

if ( id_dialog6 == 0) tmpstr = "Item1 is selected" ;

else tmpstr = "Item2 is selected" ;

Toast.makeText (ControlDialog. this , tmpstr, Toast. LENGTH_LONG ).show();

dialog.dismiss(); // 可以没有这句

}

});

dialog = builder.create();

break ;

case ID_DIALOG7 : // 创建一个 CharacterPickerDialog

dialog = new CharacterPickerDialog( this , new View( this ), null , "0123" , false )

{

/* if we don't override these, pressing one of the buttons crashes */

@Override

public void onItemClick(AdapterView parent,

View view,

int position, long id)

{

Log.e ( "1" , "" + position + "/" + id);

}

@Override

public void onClick(View v)

{

Log.e ( "2" , ((Button)v).getText().toString());

dismiss();

}

};

break ;

case ID_DIALOG8 : // 创建一个 DatePickerDialog

dialog = new DatePickerDialog( this , new DatePickerDialog.OnDateSetListener()

{

public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)

{

String tmpstr = "" + year + "/" + (monthOfYear + 1) + "/" + dayOfMonth;

Toast.makeText (ControlDialog. this , tmpstr, Toast. LENGTH_SHORT ).show();

}

}, 2011,0, 1); // 注意:月份取值空间: 0~11

break ;

case ID_DIALOG9 : // 创建一个 TimePickerDialog

dialog = new TimePickerDialog( this , new TimePickerDialog.OnTimeSetListener()

{

public void onTimeSet(TimePicker view, int hourOfDay, int minute)

{

Toast.makeText (ControlDialog. this ,

"You picked " +hourOfDay+ " hours and " +minute+ " minutes." ,

Toast. LENGTH_SHORT ).show();

}

}, 0, 0, true );

break ;

case ID_DIALOG10 : // 创建一个 ProgressDialog

dialog = (ProgressDialog) new ProgressDialog(ControlDialog. this );

((ProgressDialog) dialog ).setIndeterminate( false );

((ProgressDialog) dialog ).setMax(10000);

((ProgressDialog) dialog ).setProgressStyle(ProgressDialog. STYLE_HORIZONTAL );

((ProgressDialog) dialog ).setTitle( " 显示进度 " );

((ProgressDialog) dialog ).setMessage( " 我们一直在进步 ! " );

break ;

case ID_DIALOG11 : // 创建一个自定义的对话框

builder = new AlertDialog.Builder(ControlDialog. this );

LayoutInflater inflater = getLayoutInflater();

final View layout = inflater.inflate(R.layout. custom_dialog , null );

builder.setTitle( " 自定义对话框 " );

builder.setView(layout);

builder.setPositiveButton( " 确定 " , new DialogInterface.OnClickListener()

{

public void onClick(DialogInterface dialog, int which)

{

EditText ed = (EditText)layout.findViewById(R.id. etname );

Toast.makeText (ControlDialog. this , ed.getText().toString(), Toast. LENGTH_LONG ).show();

dialog.dismiss(); // 可以没有这句

}

});

dialog = builder.create();

break ;

}

// 返回创建的对话框实例

return dialog ;

}

// 在显示之前 , 对部分对话框的属性进行修改。

// 但, 对 CharacterPickerDialog 的属性修改似乎无效 , 有人认为这是 CharacterPickerDialog 本身的 bug

protected void onPrepareDialog( int id, Dialog dialog)

{

switch (id)

{

case ID_DIALOG1 :

dialog.setTitle( "A dialog with title1" );

break ;

case ID_DIALOG2 :

((AlertDialog)dialog).setTitle( "Title" );

((AlertDialog)dialog).setMessage( "Simple Information" );

break ;

case ID_DIALOG8 :

((DatePickerDialog)dialog).setTitle( " 选择日期 " );

break ;

case ID_DIALOG9 :

((TimePickerDialog)dialog).setTitle( " 选择时间 " );

break ;

}

}

public void onClick(View v)

{

// 点击不同的按钮,显示不同的对话框

switch (v.getId())

{

case R.id. show_button1 :

this .showDialog( ID_DIALOG1 , null );

break ;

case R.id. show_button2 :

this .showDialog( ID_DIALOG2 , null );

break ;

case R.id. show_button3 :

this .showDialog( ID_DIALOG3 , null );

break ;

case R.id. show_button4 :

this .showDialog( ID_DIALOG4 , null );

break ;

case R.id. show_button5 :

this .showDialog( ID_DIALOG5 , null );

break ;

case R.id. show_button6 :

this .showDialog( ID_DIALOG6 , null );

break ;

case R.id. show_button7 :

this .showDialog( ID_DIALOG7 , null );

break ;

case R.id. show_button8 :

this .showDialog( ID_DIALOG8 , null );

break ;

case R.id. show_button9 :

this .showDialog( ID_DIALOG9 , null );

break ;

case R.id. show_button10 :

// 显示 ProgressDialog

this .showDialog( ID_DIALOG10 , null );

// 创建线程 pt

ProgressThread pt = new ProgressThread( progressHandler );

// 启动线程

pt.start();

break ;

case R.id. show_button11 :

this .showDialog( ID_DIALOG11 , null );

break ;

}

}

}

// 线程类:用于更新 ProgressDialog 中的进度条

class ProgressThread extends Thread

{

private Handler handler ;

public ProgressThread(Handler handler) // 传入一个 Handler 对象

{

this . handler = handler;

}

@Override

public void run()

{

int i = 0;

while (i <= 1000)

{

handler .sendEmptyMessage(i); // i 就是 Message what 的值 , handler.sendEmptyMessage

// 会触发 handler 的 handleMessage 执行

++i;

try

{

sleep (100); // 休息 100ms

}

catch (InterruptedException e)

{

e.printStackTrace();

}

}

}

}

4. 运行结果:


点击第一个按钮:


点击第二个按钮:

试着点击 OK 或者 Cancel 按钮,看看 LogCat 中有什么输出。

点击第三个按钮:


点击对话框上的三个按钮,会有对应的 Toast 出现。

点击第四个按钮:


EditText 中输入“ hello, android ”,在点击 OK 按钮,则会出现:


点击第五个按钮:

点击 Item1 ,然后点击 OK ,会出现:


点击第六个按钮:


选中 Item2 ,在点击 OK 按钮,会出现:


点击第七个按钮:


点击“ 0 ”按钮,观察在 LogCat 中输出。

点击第八个按钮:


将月份调整到 5 ,日期调整到 23 后,点击“设置”按钮,则会出现:


点击第九个按钮:


选择不同的时间,然后点击“设置”按钮,观察输出结果。

点击第十个按钮:


当进度达到 100% 的时候,对话框会自动消失。

点击第十一个按钮:


EditText 中输入一些文字,点击“确定”按钮,观察输出结果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值