1.普通的Dialog

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("今天下雨了吗?")
.setPositiveButton("yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(Demo7Activity.this,"你点击了yes",Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("no", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(Demo7Activity.this,"你点击了no",Toast.LENGTH_SHORT).show();
}
});
builder.show();

2.单选Dialog

 final String []str = {"香蕉","橘子","苹果","橙子","西瓜","凤梨"};

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择一种水果")
.setItems(str, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Demo7Activity.this,"你喜欢"+str[which]+"水果",Toast.LENGTH_SHORT).show();
}
});
builder.show();

3.多选Dialog

final String []str = {"香蕉","橘子","苹果","橙子","西瓜","凤梨"};

        final ArrayList<String>mSelectedItems = new ArrayList<String>();

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Set the dialog title
builder.setTitle("请选择一种水果")
// Specify the list array, the items to be selected by default (null for none),
// and the listener through which to receive callbacks when items are selected
.setMultiChoiceItems(str, null,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
if (isChecked) {
// If the user checked the item, add it to the selected items
mSelectedItems.add(str[which]);
} else if (mSelectedItems.contains(which)) {
// Else, if the item is already in the array, remove it
mSelectedItems.remove(str[which]);
}
}
})
// Set the action buttons
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) { if(mSelectedItems.size() <= 0){
Toast.makeText(Demo7Activity.this,"你没有喜欢的水果",Toast.LENGTH_SHORT).show();
return;
} StringBuffer buffer = new StringBuffer();
for (int i=0;i<mSelectedItems.size();i++){
buffer.append(mSelectedItems.get(i)+" ");
} Toast.makeText(Demo7Activity.this,"你喜欢的水果有"+buffer.toString(),Toast.LENGTH_SHORT).show(); }
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(Demo7Activity.this,"你取消了选择",Toast.LENGTH_SHORT).show();
}
}); builder.show();

4. 自定义Dialog


demo7_custom_alert_view.xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="@drawable/ic_launcher"
android:layout_width="match_parent"
android:layout_height="64dp"
android:scaleType="center"
android:background="#FFFFBB33"
android:contentDescription="@string/app_name" />
<EditText
android:id="@+id/username"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="请输入用户名" />
<EditText
android:id="@+id/password"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="请输入密码"/>
</LinearLayout>

主要代码:

 AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Get the layout inflater
LayoutInflater inflater = this.getLayoutInflater(); // Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.demo7_custom_alert_view, null))
// Add action buttons
.setPositiveButton("登录", new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(Demo7Activity.this,"登录",Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(Demo7Activity.this,"取消",Toast.LENGTH_SHORT).show();
}
});
builder.show();

与之相对:ProgressDialog

 方式一:new Dialog  
    final ProgressDialog dialog = new ProgressDialog(this);
dialog.show();

方式二:使用静态方式创建并显示,这种进度条只能是圆形条,设置title和Message提示内容

 ProgressDialog dialog2 = ProgressDialog.show(this, "提示", "正在登陆中");
// 方式三 使用静态方式创建并显示,这种进度条只能是圆形条,这里最后一个参数boolean indeterminate设置是否是不明确的状态
ProgressDialog dialog3 = ProgressDialog
.show(this, "提示", "正在登陆中", false);
// 方式四 使用静态方式创建并显示,这种进度条只能是圆形条,这里最后一个参数boolean cancelable 设置是否进度条是可以取消的
ProgressDialog dialog4 = ProgressDialog.show(this, "提示", "正在登陆中",
false, true);
// 方式五 使用静态方式创建并显示,这种进度条只能是圆形条,这里最后一个参数 DialogInterface.OnCancelListener
// cancelListener用于监听进度条被取消
ProgressDialog dialog5 = ProgressDialog.show(this, "提示", "正在登陆中", true,
true, cancelListener);
private OnCancelListener cancelListener = new OnCancelListener() {  

    @Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "进度条被取消", Toast.LENGTH_LONG)
.show(); }
};
 
ProgressDialog dialog = new ProgressDialog(this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置进度条的形式为圆形转动的进度条
dialog.setCancelable(true);// 设置是否可以通过点击Back键取消
dialog.setCanceledOnTouchOutside(false);// 设置在点击Dialog外是否取消Dialog进度条
dialog.setIcon(R.drawable.ic_launcher);//
// 设置提示的title的图标,默认是没有的,如果没有设置title的话只设置Icon是不会显示图标的
dialog.setTitle("提示");

android Dialog官方demo的更多相关文章

  1. 解决Android微信支付官方demo运行失败

    Android微信支付官方demo运行失败,在此简单记录一下解决步骤 1.httpclient错误 官方给的demo是eclipse的,打开之后提示httpclient的错误,我知道在as下解决htt ...

  2. android 官方demo地址

    android官方demo地址都放在了github上:https://github.com/googlesamples

  3. android开源项目之OTTO事件总线(二)官方demo解说

    官方demo见  https://github.com/square/otto 注意自己该编译版本为2.3以上,默认的1.6不支持match_parent属性,导致布局文件出错. 另外需要手动添加an ...

  4. React Native官方DEMO

    官方给我们提供了UIExplorer项目,这里边包含React Native的基本所有组件的使用介绍和方法. 运行官方DEMO步骤如下 安装react native环境 React Native项目源 ...

  5. Android组件化demo实现以及遇坑分享

    首先贴出demo的github地址:GitHub - TenzLiu/TenzModuleDemo: android组件化demo 作者:TenzLiu原文链接:https://www.jianshu ...

  6. weex官方demo weex-hackernews代码解读(上)

    一.介绍 weex 是阿里出品的一个类似RN的框架,可以使用前端技术来开发移动应用,实现一份代码支持H5,IOS和Android.最新版本的weex已默认将vue.js作为前端框架,而weex-hac ...

  7. Android蓝牙联机Demo解析

    写在前面: 手游的双人对战实现方式有很多,比如: 联网对战(需要一个服务器负责转发客户端请求,各种大型手游的做法) 分屏对战(手机上下分屏,典型的例子就是切水果的双人对战) 蓝牙联机对战(通过蓝牙联机 ...

  8. 订餐系统之微信支付,踩了官方demo的坑

        最近一个项目要增加微信支付的功能,想来这个东西出来这么久了,按微信提供的应该可以很快搞定的,结果提供的demo( JS API网页支付)中各种坑,咨询他们的客服,态度倒是非常好,就是解决不了问 ...

  9. TensorFlow 在android上的Demo(1)

    转载时请注明出处: 修雨轩陈 系统环境说明: ------------------------------------ 操作系统 : ubunt 14.03 _ x86_64 操作系统 内存: 8GB ...

随机推荐

  1. Unity-Animator(Mecanim)深入系列总索引

    花了不少时间完成了这篇Unity Animator学习系列文章,其中大多数内容都来自个人实践,包括API部分很多都是亲测,期望和网上的诸多教程达到互补. 相关参考文档 Unity Animator官方 ...

  2. TODO的用法

    visual studio提供//TODO标记,不过不会在右边标记处明显标识,需要你选择菜单栏的视图进行查看.方法如下: 1.首先在你还未完成的地方打上TODO标记,以下方式均可: 1)//TODO: ...

  3. 帝国CMS当前位置中的“首页”二字如何修改

    1.帝国CMS当前位置首页那两个字在哪里可以修改吗? 2.[!--newsnav--]该处的首页链接,请问在哪儿修改? 3.导航条[!--newsnav--]默认首页为:“首页”可以更改么? 4.导航 ...

  4. 关于BroadCastReceiver安全性的思考

    尊重原创:http://blog.csdn.net/yuanzeyao/article/details/38948863 BroadCastReceiver是Android 四大组件之中的一个,应用非 ...

  5. CentOS6.4环境下布署LVS+keepalived笔记

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://400053.blog.51cto.com/390053/713566 环境: 1 ...

  6. hadoop错误各种原因

    NoRouteToHostException 错误描述: INFO hdfs.DFSClient: Exception in createBlockOutputStream java.net.NoRo ...

  7. 演示PostgreSQL的详细安装及配置图解

    右击文件选择以管理员身份运行 2 开始执行程序的安装 3 设置安装目录 4 设置数据的保存目录 5 设置数据库管理员密码,请牢记此密码. 6 设置端口号,选择默认的端口号即可 7 根据自己选择设置地区 ...

  8. openstack将本地实例迁移至ceph存储中

    需求: 最近在openstack上线了ceph存储,创建虚拟机和云硬盘都基于ceph卷进行存储和创建,但是之前openstack用的是本地存储,创建的所有实例都在本地文件中,当实例重启之后,opens ...

  9. cache buffers chains以及热块解决方案

    cache buffers chains以及热块解决方案 今天是2013-10-10,今天下午我调休了,中午饭过后从14点一直睡到16点,这种感觉真爽.  之前学习过关于buffer cache的ca ...

  10. Apache服务器最新版下载、安装及配置(win版)

    Apache服务器最新版下载.安装及配置(win版) Apache的下载: 登录http://httpd.apache.org/download.cgi 这个地址,找到2.4.10,如下图位置:   ...