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. plsql programming 11 记录类型

    记录类型非常类似数据库表中的行. 记录作为一个整体本身并没有值, 不过每个单独成员或字段都有值, 记录提供了一种把这些值当做一组进行操作的方法. 例如: 1: -- create a table 2: ...

  2. Linux Linux程序练习一

    注意:在Linux中,"*"是一个通配符,代表所有字符,所以"*"必须要使用转义字符"\"

  3. 面向Internet的编程

    面向Internet的编程 1994年秋天我返回工作时,这个公司的景象已经完全改变.他们决定Oak语言——跨平台的.安全的.易传输的代码——时理想的面向Internet的语言.同时他们在制作名为Web ...

  4. Spring MVC生成JSON数据

    以下示例演示如何使用Spring Web MVC框架生成JSON数据格式.首先使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态表单的Web应用程序: ...

  5. Gmail 邮件配置备忘

    1.smtp端口号不是默认的25,smtp的是587 2.必须设置“允许使用不够安全的应用” 其他的按照网上文档设置就好了

  6. cookie细节

    设置cookie时,不像设置session,可以马上生效,它的生效时间是下一次请求页面.

  7. Uncaught Error: artDialog: Document types require more than xhtml1.0

    这需要声明html文档,加上如下声明就不会报错了! <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  8. PowerDesigner如何导出表到word的方法

    from:https://jingyan.baidu.com/article/295430f1c385970c7f005056.html PowerDesigner如何导出表到word的方法 听语音 ...

  9. Android ADB 命令链接模拟器出现 daemon not running 解决方法

    用adb命令链接远程模拟器 有时候会遇到如下问题: C:Documents and SettingsAdministrator>adb connect 192.168.0.183 * daemo ...

  10. Java多线程的两种实现方式

    Java总共有两种方式实现多线程 方式1:通过继承Thread类的方式 package com.day04; /** * 通过继承Thread类并复写run方法来是实现多线程 * * @author ...