各种对话框的使用

转自:http://blog.csdn.net/speedme/article/details/8041677

我们在平时做开发的时候,免不了会用到各种各样的对话框,相信有过其他平台开发经验的朋友都会知道,大部分的平台都只提供了几个最简单的实现,如果我们想实现自己特定需求的对话框,大家可能首先会想到,通过继承等方式,重写我们自己的对话框。当然,这也是不失为一个不错的解决方式,但是一般的情况却是这样,我们重写的对话框,也许只在一个特定的地方会用到,为了这一次的使用,而去创建一个新类,往往有点杀鸡用牛刀的感觉,甚至会对我们的程序增加不必要的复杂性,对于这种情形的对话框有没有更优雅的解决方案呢? 
    幸运的是,android提供了这种问题的解决方案,刚开始接触android的时候,我在做一个自定义对话框的时候,也是通过继承的方式来实现,后来随着对文档了解的深入,发现了android起始已经提供了相应的接口Dialog Builder ,下面我就吧相关的内容在这里分享一下,也能让更多的初学者少走弯路。

首先是一个最简单的应用,就是弹出一个消息框,在android中可以这样实现

[java] view plaincopy
  1. 1  
  2. new AlertDialog.Builder(self)    
  3. 2  
  4.                 .setTitle("标题")  
  5. 3  
  6.                 .setMessage("简单消息框")  
  7. 4  
  8.                 .setPositiveButton("确定"null)  
  9. 5  
  10.                 .show();  


效果如下:

1.png 


上面的代码中我们新建了一个AlertDialog,并用Builder方法形成了一个对象链,通过一系列的设置方法,构造出我们需要的对话框,然后调用show方法显示出来,注意到Builder方法的参数 self,这个其实是Activity对象的引用,根据你所处的上下文来传入相应的引用就可以了。例如在onCreate方法中调用,只需传入this即可。


下面是带确认和取消按钮的对话框

  1. [java] view plaincopy
    1. new AlertDialog.Builder(self)   
    2. .setTitle("确认")  
    3. .setMessage("确定吗?")  
    4. .setPositiveButton("是"null)  
    5. .setNegativeButton("否"null)  
    6. .show();  
     
复制代码


2.png 

注意到,这里有两个null参数,这里要放的其实是这两个按钮点击的监听程序,由于我们这里不需要监听这些动作,所以传入null值简单忽略掉,但是实际开发的时候一般都是需要传入监听器的,用来响应用户的操作。

下面是一个可以输入文本的对话框

  1. [java] view plaincopy
    1. new AlertDialog.Builder(self)  
    2. .setTitle("请输入")  
    3. .setIcon(android.R.drawable.ic_dialog_info)  
    4. .setView(new EditText(self))  
    5. .setPositiveButton("确定"null)  
    6. .setNegativeButton("取消"null)  
    7. .show();  
     



3.png 

如上代码,我们用setView方法,为我们的对话框传入了一个文本编辑框,当然,你可以传入任何的视图对象,比如图片框,WebView等。。尽情发挥你的想象力吧~:lol

下面是单选框与多选框,也是非常有用的两种对话框

  1. [java] view plaincopy
    1. new AlertDialog.Builder(self)  
    2. .setTitle("请选择")  
    3. .setIcon(android.R.drawable.ic_dialog_info)                  
    4. .setSingleChoiceItems(new String[] {"选项1","选项2","选项3","选项4"}, 0,   
    5.   new DialogInterface.OnClickListener() {  
    6.                               
    7.      public void onClick(DialogInterface dialog, int which) {  
    8.         dialog.dismiss();  
    9.      }  
    10.   }  
    11. )  
    12. .setNegativeButton("取消"null)  
    13. .show();  
     
  2.                


4.png 


  1. [java] view plaincopy
    1.                  
    2. new AlertDialog.Builder(self)  
    3. .setTitle("多选框")  
    4. .setMultiChoiceItems(new String[] {"选项1","选项2","选项3","选项4"}, nullnull)  
    5. .setPositiveButton("确定"null)                  
    6. .setNegativeButton("取消"null)  
    7. .show();  
                   



多选对话框 

单选和多选对话框应该是我们平时用的非常多的,代码应该很好理解,下面再最后介绍两个、

列表对话框

  1. [java] view plaincopy
    1. new AlertDialog.Builder(self)  
    2. .setTitle("列表框")  
    3. .setItems(new String[] {"列表项1","列表项2","列表项3"}, null)  
    4. .setNegativeButton("确定"null)  
    5. .show();  
     



6.png 


最后,在对话框中显示图片

  1. [java] view plaincopy
    1. ImageView img = new ImageView(self);  
    2. img.setImageResource(R.drawable.icon);  
    3. new AlertDialog.Builder(self)  
    4. .setTitle("图片框")  
    5. .setView(img)  
    6. .setPositiveButton("确定"null)  
    7. .show();  
     


7.png 

      我们传入了一个ImageView来显示图片,这里显示了一个经典的android小绿人图标~ ~,当然这里还可以放上网络图片,具体的实现方法就不介绍了,留给大家来练习吧~:lol

      最后总结一下,android平台为我们开发提供了极大的便利,DialogBuilder能做的不止这些,这里给大家展示的只是冰山一角,我们可以尽情的发挥想象,创造我们自己的对话框。

展开阅读全文

各种对话框对话框(总结)

03-12

rn'rn'标准对话框(SmDialog)rn'rnrnOption Explicitrnrn''定义一个全局变量,用于保存字体的各种属性rnPublic Type SmFontAttrrn FontName As String '字体名rn FontSize As Integer '字体大小rn FontBod As Boolean '是否黑体rn FontItalic As Boolean '是否斜体rn FontUnderLine As Boolean '是否下划线rn FontStrikeou As Booleanrn FontColor As Longrn WinHwnd As LongrnEnd TypernrnDim M_GetFont As SmFontAttrrnrn''**系统常量------------------------------------------rnPrivate Const SWP_NOOWNERZORDER = &H200rnPrivate Const SWP_HIDEWINDOW = &H80rnPrivate Const SWP_NOACTIVATE = &H10rnPrivate Const SWP_NOMOVE = &H2rnPrivate Const SWP_NOREDRAW = &H8rnPrivate Const SWP_NOREPOSITION = SWP_NOOWNERZORDERrnPrivate Const SWP_NOSIZE = &H1rnPrivate Const SWP_NOZORDER = &H4rnPrivate Const SWP_SHOWWINDOW = &H40rnPrivate Const RESOURCETYPE_DISK = &H1 '网络驱动器rnPrivate Const RESOURCETYPE_PRINT = &H2 '网络打印机rn'/------------------------------------------------------------rnPrivate Const NoError = 0rnPrivate Const CSIDL_DESKTOP = &H0rnPrivate Const CSIDL_PROGRAMS = &H2rnPrivate Const CSIDL_CONTROLS = &H3rnPrivate Const CSIDL_PRINTERS = &H4rnPrivate Const CSIDL_PERSONAL = &H5rnPrivate Const CSIDL_FAVORITES = &H6rnPrivate Const CSIDL_STARTUP = &H7rnPrivate Const CSIDL_RECENT = &H8rnPrivate Const CSIDL_SENDTO = &H9rnPrivate Const CSIDL_BITBUCKET = &HArnPrivate Const CSIDL_STARTMENU = &HBrnPrivate Const CSIDL_DESKTOPDIRECTORY = &H10rnPrivate Const CSIDL_DRIVES = &H11rnPrivate Const CSIDL_NETWORK = &H12rnPrivate Const CSIDL_NETHOOD = &H13rnPrivate Const CSIDL_FONTS = &H14rnPrivate Const CSIDL_TEMPLATES = &H15rnPrivate Const LF_FACESIZE = 32rnPrivate Const MAX_PATH = 260rnPrivate Const CF_INITTOLOGFONTSTRUCT = &H40&rnPrivate Const CF_FIXEDPITCHONLY = &H4000&rnPrivate Const CF_EFFECTS = &H100&rnPrivate Const ITALIC_FONTTYPE = &H200rnPrivate Const BOLD_FONTTYPE = &H100rnPrivate Const CF_NOFACESEL = &H80000rnPrivate Const CF_NOSCRIPTSEL = &H800000rnPrivate Const CF_PRINTERFONTS = &H2rnPrivate Const CF_SCALABLEONLY = &H20000rnPrivate Const CF_SCREENFONTS = &H1rnPrivate Const CF_SHOWHELP = &H4&rnPrivate Const CF_BOTH = (CF_SCREENFONTS Or CF_PRINTERFONTS)rn'/------------------------------------------rnPrivate Type CHOOSECOLORrn lStructSize As Longrn hwndOwner As Longrn hInstance As Longrn rgbResult As Longrn lpCustColors As Stringrn flags As Longrn lCustData As Longrn lpfnHook As Longrn lpTemplateName As StringrnEnd TypernrnPrivate Type OPENFILENAMErn lStructSize As Longrn hwndOwner As Longrn hInstance As Longrn lpstrFilter As Stringrn lpstrCustomFilter As Stringrn nMaxCustFilter As Longrn nFilterIndex As Longrn lpstrFile As Stringrn nMaxFile As Longrn lpstrFileTitle As Stringrn nMaxFileTitle As Longrn lpstrInitialDir As Stringrn lpstrTitle As Stringrn flags As Longrn nFileOffset As Integerrn nFileExtension As Integerrn lpstrDefExt As Stringrn lCustData As Longrn lpfnHook As Longrn lpTemplateName As StringrnEnd Typern'/-----------------------------------------------------------rnPrivate Type LOGFONTrn lfHeight As Longrn lfWidth As Longrn lfEscapement As Longrn lfOrientation As Longrn lfWeight As Longrn lfItalic As Bytern lfUnderline As Bytern lfStrikeOut As Bytern lfCharSet As Bytern lfOutPrecision As Bytern lfClipPrecision As Bytern lfQuality As Bytern lfPitchAndFamily As Bytern lfFaceName As String * LF_FACESIZErnEnd TypernrnPrivate Type CHOOSEFONTrn lStructSize As Longrn hwndOwner As Longrn hdc As Longrn lpLogFont As Longrn iPointSize As Longrn flags As Longrn rgbColors As Longrn lCustData As Longrn lpfnHook As Longrn lpTemplateName As Stringrn hInstance As Longrn lpszStyle As Stringrn nFontType As Integerrn MISSING_ALIGNMENT As Integerrn nSizeMin As Longrn nSizeMax As Longrn End Typern'/--------------rnPrivate Type SHITEMIDrn cb As Longrn abID() As ByternEnd TypernrnPrivate Type ITEMIDLISTrn mkid As SHITEMIDrnEnd Typern'/------------------------------------------rnPrivate Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" _rn (ByVal Pidl As Long, ByVal pszPath As String) As LongrnrnPrivate Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" _rn (ByVal hwndOwner As Long, ByVal nFolder As Long, _rn Pidl As ITEMIDLIST) As Longrn'/------------------------------------------rnPrivate Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As LongrnPrivate Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As LongrnPrivate Declare Function CHOOSECOLOR Lib "comdlg32.dll" Alias "ChooseColorA" (pChoosecolor As CHOOSECOLOR) As LongrnPrivate Declare Function WNetConnectionDialog Lib "mpr.dll" (ByVal hWnd As Long, ByVal dwType As Long) As LongrnPrivate Declare Function CHOOSEFONT Lib "comdlg32.dll" Alias "ChooseFontA" (pChooseFont As CHOOSEFONT) As Longrnrn'/=======显示断开网络资源对话框============rnPrivate Declare Function WNetDisconnectDialog Lib "mpr.dll" _rn (ByVal hWnd As Long, ByVal dwType As Long) As Longrn'/================================================================================rnPrivate Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal pv As Long)rnPrivate Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" _rn (lpBrowseInfo As BROWSEINFO) As LongrnPrivate Type BROWSEINFOrn hOwner As Longrn pidlRoot As Longrn pszDisplayName As Stringrn lpszTitle As Stringrn ulFlags As Longrn lpfn As Longrn lParam As Longrn iImage As LongrnEnd Typern'/结构说明: _rn hOwner 调用这个对话框的窗口的句柄 _rn pidlRoot 指向你希望浏览的最上面的文件夹的符列表 _rn pszDisplayName 用于保存用户所选择的文件夹的显示名的缓冲区 _rn lpszTitle 浏览对话框的标题 _rn ulFlags 决定浏览什么的标志(见下) _rn lpfn 当事件发生时对话框调用的回调函数的地址.可将它设定为NULL _rn lparam 若定义了回调函数,则为传递给回调函数的值 _rn iImage As Long 保存所选文件夹映像索引的缓冲区 _rnulFlags参数(见下:)rnPrivate Const BIF_RETURNONLYFSDIRS = &H1 '仅允许浏览文件系统文件夹rnPrivate Const BIF_DONTGOBELOWDOMAIN = &H2 '利用这个值强制用户仪在网上邻居的域级别中rnPrivate Const BIF_STATUSTEXT = &H4 '在选择对话中显示状态栏rnPrivate Const BIF_RETURNFSANCESTORS = &H8 '返回文件系统祖先rnPrivate Const BIF_BROWSEFORCOMPUTER = &H1000 '允许浏览计算机rnPrivate Const BIF_BROWSEFORPRINTER = &H2000 '允许游览打印机文件夹rn'/--------------------------------------------------------------------------------rnDim FontInfo As SmFontAttr '字体rn'/--------------------------------------------------------------------------------rnrn 论坛

没有更多推荐了,返回首页