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. c++如何new构造函数是protected的对象

    如果确实要new的话,可以继承这个类,然后new派生类,再转换为基类指针

  2. zookeeper 批量启动的脚本

    #!/bin/shecho "start zkServer"for i in  2 3 4dossh mini$i "source /etc/profile;/usr/l ...

  3. MFC 点击按钮,弹出另一个对话框(模态及非模态对话框)

    1. 模态对话框 资源视图->Dialog->右键->添加资源->新建->对话框->右键->添加类. 例如:在A_dialog中点击按钮弹出B_dialog  ...

  4. VMware虚拟机 Ubuntu 实用技巧 (2)桥接模式连接网络与网卡的配置

    1.先用ifconfig查看当前的网卡配置,一般没有进行设置之前,打印的信息如下所示. ens33 Link encap:以太网 硬件地址 02:0c:29:c6:be:c7 inet6 地址: fe ...

  5. 【转】Android自动化测试(UiAutomator)简要介绍

    一.一个BUG引发的问题   如果研发过程中有一个BUG:“不断的切换手机语言出现花屏现象”.这个问题我们如何验证呢?我想,最好的方式应该是自动化测试.    那么,自动化测试可以完成哪些任务呢?  ...

  6. 第一百八十九节,jQueryUI,折叠菜单 UI

    jQueryUI,折叠菜单 UI 学习要点: 1.使用 accordion 2.修改 accordion 样式 3.accordion()方法的属性 4.accordion()方法的事件 5.acco ...

  7. [浪风JQuery开发]jquery最有意思的IFrame类似应用--值得深入研究

    前几天一时兴起答应朋友的需求--做一个外国的企业网站: 本想做就做呗,可没想我辛辛苦苦用浪风认真php平台开发后,对方来一句我服务器不能安装其他程序,请给我用frame框架开发. 浪风那是一个苦字难言 ...

  8. dm8148 开发之---IDR帧

    IDR帧属于I帧. 解码器收到IDR帧时,将所有的参考帧队列丢弃(用x264_reference_reset函 数实现——在encoder.c文件中).这点是所有I帧共有的特性,但是收到IDR帧 时, ...

  9. 光圈与F数相关知识

    一.F数 F数.光圈数.相对孔径的倒数. F数代表的意义 F数小(光圈大).F数大(光圈小). F数又称为镜头速度,F数小的镜头速度快.因为拍摄的曝光时间△t 正比于F数的平方. F数还能表征镜头的分 ...

  10. C++中的return返回值:return0 or return -1?

    C++98 中定义了如下两种 main 函数的定义方式: int main( ) int main( int argc, char *argv[] )   (参考资料:ISO/IEC 14882(19 ...