new AlertDialog.Builder(self)
.setTitle("标题")
.setMessage("简单消息框")
.setPositiveButton("确定", null)
.show();

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

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

new AlertDialog.Builder(self)
.setTitle("确认")
.setMessage("确定吗?")
.setPositiveButton("是", null)
.setNegativeButton("否", null)
.show();


[img]http://www.eoeandroid.com/data/attachment/forum/month_1009/10090514154d3cbb9ed67e44a5.png[/img]

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

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

new AlertDialog.Builder(self)
.setTitle("请输入")
.setIcon(android.R.drawable.ic_dialog_info)
.setView(new EditText(self))
.setPositiveButton("确定", null)
.setNegativeButton("取消", null)
.show();

[img]http://www.eoeandroid.com/data/attachment/forum/month_1009/10090514150a100809698b72d8.png[/img]

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

下面是单选框与多选框,也是非常有用的两种对话框
new AlertDialog.Builder(self)
.setTitle("请选择")
.setIcon(android.R.drawable.ic_dialog_info)
.setSingleChoiceItems(new String[] {"选项1","选项2","选项3","选项4"}, 0,
new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}
)
.setNegativeButton("取消", null)
.show();

[img]http://www.eoeandroid.com/data/attachment/forum/month_1009/1009051415c024e81a4e24440c.png[/img]

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

[img]http://www.eoeandroid.com/data/attachment/forum/month_1009/100905142447d638cde26648fd.png[/img]
单选和多选对话框应该是我们平时用的非常多的,代码应该很好理解,下面再最后介绍两个、

列表对话框

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

[img]http://www.eoeandroid.com/data/attachment/forum/month_1009/100905142327942810fa5f7305.png[/img]
最后,在对话框中显示图片

ImageView img = new ImageView(self);
img.setImageResource(R.drawable.icon);
new AlertDialog.Builder(self)
.setTitle("图片框")
.setView(img)
.setPositiveButton("确定", null)
.show();

[img]http://www.eoeandroid.com/data/attachment/forum/month_1009/100905142157508feb45fa4808.png[/img]
我们传入了一个ImageView来显示图片,这里显示了一个经典的android小绿人图标~ ~,当然这里还可以放上网络图片,具体的实现方法就不介绍了,留给大家来练习吧~:lol

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

更多相关文章

  1. Android(安卓)实现 选择文件对话框
  2. android Wifi自动连接
  3. Android(安卓)ProgressBar详解
  4. Android(安卓)UI学习 - 对话框
  5. android一个项目应用另外一个项目和jar包引用
  6. Android学习笔记1
  7. Android中创建对话框(确定取消对话框、单选对话框、多选对话框)
  8. 7种形式的Android(安卓)Dialog使用举例
  9. 如何一个android工程作为另外一个android工程的lib

随机推荐

  1. Android多文件断点续传(二)——实现数据库
  2. Andoid - 开发实例(3):高仿微信的界面
  3. Android 在init.rc启动一个c++程序
  4. Android5.1系统通过包名给应用开放系统权
  5. ProgressBar的父控件为白色背景时progres
  6. Android(安卓)API中文(1) -- Android是什么
  7. Android(安卓)Studio使用Apache POI在And
  8. FileProvider无法获取外置SD卡问题解决方
  9. 一起Talk Android吧(第一百四十六回:Androi
  10. android 悬浮控件-仿360手机助手应用详情