Refer:http://www.2cto.com/kf/201205/131876.html

  (一)最简单的用法(详见注释)

 1         // 1、创建简单的AlertDialog // AlertDialog的构造方法全部是Protected的,
2 // 所以不能直接通过new一个AlertDialog来创建出一个AlertDialog; //
3 // (1)要创建一个AlertDialog,就要用到AlertDialog.Builder
4 AlertDialog.Builder dialog = new AlertDialog.Builder(this);
5
6 // (2)设置各种属性 // 注:不设置哪项属性,这个属性就默认不会显示出来
7 dialog.setTitle("这是一个简单的对话框");
8 dialog.setIcon(R.drawable.dictation2_64);
9 dialog.setMessage("欢迎使用!");
10
11 // (3)设置dialog可否被取消
12 dialog.setCancelable(true);
13
14 // (4)显示出来
15 dialog.show();

   效果如下:

 

  (二)带按钮的AlertDialog

         // 2、带按钮的AlertDialog
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("确认");
dialog.setIcon(R.drawable.dictation2_64);
dialog.setMessage("确定要删除此项吗?"); // 设置“确定”按钮,使用DialogInterface.OnClickListener接口参数
dialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
Log.d("Dialog", "点击了“确认”按钮");
}
}); // 设置“查看详情”按钮,使用DialogInterface.OnClickListener接口参数
dialog.setNeutralButton("查看详情",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
Log.d("Dialog", "点击了“查看详情”按钮");
}
}); // 设置“取消”按钮,使用DialogInterface.OnClickListener接口参数
dialog.setNegativeButton("取消",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
Log.d("Dialog", "点击了“取消”按钮");
}
}); dialog.show();

  效果如下:

  (三)类似于ListView的AlertDialog

  用setItems(CharSequence[] items, final OnClickListener listener)方法来实现类似ListView的AlertDialog。

         // 3、类似ListView的AlertDialog
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("请选择一项运动");
dialog.setIcon(R.drawable.dictation2_64);
// 设置为false说明当前dialog是不能用返回键取消的
dialog.setCancelable(false); // 列表字符串数组
final String[] sportsArray = new String[] { "跑步", "篮球", "游泳",
"自行车", "羽毛球" };
// 用于在item的点击事件中,记录选择的是哪一项,初始值设为0.这里用final数组只是因为匿名内部类中只能使用外部终态的变量
final int selectedIndex[] = { 0 }; // 用setItems方法来实现
dialog.setItems(sportsArray, new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
selectedIndex[0] = which;
Log.d("Dialog", "选择了:" + sportsArray[selectedIndex[0]]);
}
}); dialog.setNegativeButton("取消",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
Log.d("Dialog", "点击了“取消”按钮");
}
}); dialog.show();

  效果如下:

 

  (四)类似RadioButton的AlertDialog

  用setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener)方法来实现类似RadioButton的AlertDialog

  第一个参数是要显示的数据的数组,第二个参数是初始值(初始被选中的item),第三个参数是点击某个item的触发事件

         // 4、类似RadioButton的AlertDialog
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("请选择一项运动");
dialog.setIcon(R.drawable.dictation2_64); // 列表字符串数组
final String[] sportsArray = new String[] { "跑步", "篮球", "游泳",
"自行车", "羽毛球" };
// 用于在item的点击事件中,记录选择的是哪一项,初始值设为0.这里用final数组只是因为匿名内部类中只能使用外部终态的变量
final int selectedIndex[] = { 0 }; // 用setSingleChoiceItems方法来实现
dialog.setSingleChoiceItems(sportsArray, 0,
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
selectedIndex[0] = which; }
}); dialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
Log.d("Dialog", "选择了:"
+ sportsArray[selectedIndex[0]]);
}
}); dialog.setNegativeButton("取消",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
Log.d("Dialog", "点击了“取消”按钮");
}
}); dialog.show();

  效果如下:

  (五)类似CheckBox的AlertDialog

  用setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, final OnMultiChoiceClickListener listener)方法来实现类似CheckBox的AlertDialog
  第一个参数是要显示的数据的数组,第二个参数是选中状态的数组,第三个参数是点击某个item的触发事件

         // 5、类似CheckBox的AlertDialog
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("请选择喜欢的运动(可多选)");
dialog.setIcon(R.drawable.dictation2_64); // 列表字符串数组
final String[] sportsArray = new String[] { "跑步", "篮球", "游泳",
"自行车", "羽毛球" };
// 用于在item的点击事件中,记录选择了哪些项.
final boolean[] selectedIndex = { true, true, false, false, false }; // 用setMultiChoiceItems方法来实现
dialog.setMultiChoiceItems(sportsArray, selectedIndex,
new DialogInterface.OnMultiChoiceClickListener() { @Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
selectedIndex[which] = isChecked;
}
}); dialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i < selectedIndex.length; i++) {
if (selectedIndex[i]) {
Log.d("Dialog", "选择了:" + sportsArray[i]);
}
}
}
}); dialog.setNegativeButton("取消",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
Log.d("Dialog", "点击了“取消”按钮");
}
}); dialog.show();

  效果如下:

  (六)自定义View的AlertDialog

  有时候系统自带的AlertDialog风格不能满足我们的需求,就比如说我们要实现一个Login画面,有用户名和密码,这时我们就要用到自定义View的AlertDialog

  1、先创建自定义登录框的布局文件my_login_view.xml:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号:" /> <EditText
android:id="@+id/my_login_account_et"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:" /> <EditText
android:id="@+id/my_login_password_et"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberPassword" />
</LinearLayout> </LinearLayout>

  2、在Activity的合适地方创建自定义的AlertDialog(比如按钮的点击事件中):

         // 6、自定义View的AlertDialog
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("用户登录"); // 取得自定义View
LayoutInflater layoutInflater = LayoutInflater.from(this);
final View myLoginView = layoutInflater.inflate(
R.layout.my_login_view, null);
dialog.setView(myLoginView); dialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
EditText loginAccountEt = (EditText) myLoginView
.findViewById(R.id.my_login_account_et);
EditText loginPasswordEt = (EditText) myLoginView
.findViewById(R.id.my_login_password_et);
Log.d("MyLogin Dialog", "输入的用户名是:"
+ loginAccountEt.getText().toString());
Log.d("MyLogin Dialog", "输入的密码是:"
+ loginPasswordEt.getText().toString());
}
}); dialog.setNegativeButton("取消",
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) { }
}); dialog.show();

  效果如下:

 

  点击“确定”按钮后LogCat中的内容:

随机推荐

  1. Modification of UCT with Patterns in Monte-Carlo Go(论文阅读)

    摘要:用于解决多臂赌博机UCB1算法已经被扩展成了解决极大极小树搜索的UCT算法.我们开发了一套Monte-Carlo围棋程序,MoGo,这是第一个使用UCT算法实现的计算机围棋程序.我们解释了为了围 ...

  2. Hibernate每个层次类一张表(使用注释)

    在上一文章中,我们使用xml文件将继承层次映射到一个表. 在这里,我们将使用注释来执行同样的任务.需要使用@Inheritance(strategy = InheritanceType.SINGLE_ ...

  3. WinRAR 5.01 正式版 (简体中文)附注册机及注册码

    软件分类:数据压缩软件大小:1.91 MB 软件类别:国外软件 软件授权:注册版软件语言:简体中文点击进入:官方主页  应用平台:Win 8.Win 7.Win 2008 R2.Win 2008.Wi ...

  4. nginx配置后只有根目录首页index.php能访问,其他页面404

    只有首页面根目录可以访问,其他页面地址都是404 not found.网上找了半天url重定向,url重写都试了无效,要不就是重定向过多,下图为跳坑历程. location / { #if ($htt ...

  5. 关于 JavaScript 学习 —— 好的博客或者网站推荐

    作者:Tw93链接:https://www.zhihu.com/question/19651401/answer/46211739来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...

  6. [读书笔记]JavaScript 闭包(Closures)

    1. 什么是闭包? 参考MDN. 2. 闭包的使用示例 2.1 示例1 <div>1</div> <div>2</div> <div>3&l ...

  7. 32位Win7下安装与配置PHP环境(一)

    运行PHP网站,主要需要安装.配置三个软件,Apache.PHP和MySQL.如果需要编辑调试PHP程序,还要安装一个编辑调试软件. 一. Apache Apache是和IIS类似的一个软件,是运行在 ...

  8. 洛谷P1122 最大子树和

    P1122 最大子树和 题目提供者该用户不存在 标签动态规划树形结构 难度普及/提高- 通过/提交54/100 提交该题 讨论 题解 记录 题目描述 小明对数学饱有兴趣,并且是个勤奋好学的学生,总是在 ...

  9. codevs1058 合唱队形==洛谷P1091 合唱队形

    P1091 合唱队形 题目描述 N位同学站成一排,音乐老师要请其中的(N-K)位同学出列,使得剩下的K位同学排成合唱队形. 合唱队形是指这样的一种队形:设K位同学从左到右依次编号为1,2…,K,他们的 ...

  10. js获取滚动条的位置

    页面具有 DTD,或者说指定了 DOCTYPE 时,使用 document.documentElement. 页面不具有 DTD,或者说没有指定了 DOCTYPE,时,使用 document.body ...