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. MVC4的缓存

    目录(?)[+] MVC3缓存之一:使用页面缓存 在MVC3中要如果要启用页面缓存,在页面对应的Action前面加上一个OutputCache属性即可. 我们建一个Demo来测试一下,在此Demo中, ...

  2. ueditor 控制上传图片的显示尺寸

    使用UEditor的编辑框插入图片的时候,如果图片尺寸比较大,则图片会超出编辑器边框出现滚动条,特别不方便. 解决办法:在ueditor 的 themes 文件夹下有个iframe.css 文件,在该 ...

  3. Photoshop脚本之创建文件夹

    function checkFolder(path){ var folder = Folder(path) if(!folder.exists) folder.create() }

  4. CSS3之body背景图平铺

    你再也不需要因为屏幕大小不同而去选择多张图片作为背景图了,css3教你做人: body { background: url('xx.jpg')top center no-repeat; backgro ...

  5. Tomcat (7.0)数据源配置

    在Tomcat这个Java Web容器下通过配置DataSource(数据源)对象能够解决上面所述的问题. JDBC中的javx.sql.DataSource接口负责建立于数据库的连接.程序中直接从数 ...

  6. Apache:怎样利用.htaccess文件对PHP站点或文件进行伪静态处理

    来源:http://www.ido321.com/1123.html 今天get了一招:利用.htaccess文件对PHP站点或文件进行伪静态处理. 一.检查server是否支持伪静态处理: 必需要空 ...

  7. FIR滤波器与IIR滤波器

    FIR(Finite Impulse Response)滤波器 有限长单位冲激响应滤波器,又称为非递归型滤波器 特点: FIR滤波器的最主要的特点是没有反馈回路,稳定性强,故不存在不稳定的问题: FI ...

  8. EasyUI怎么利用onBeforeRender事件

    onBeforeRender事件是view的属性,该事件发生在把ajax请求到的数据填充到表格内容中之前 将此段代码附加在DataGrid初始化后执行,即可完成在DataGrid渲染之前进行操作 // ...

  9. C#中的Form,textBox,Bitmap,PictureBox,Button,WebBrowser

    因为今天是用的家里的电脑,用的不是retena屏的mac,因此稍后截图的时候大家看到的图片可能会有一些模糊,还请大家海涵... 兄弟们,我胡汉三又回来啦! 以下開始我们的第一个C#程序.我们之前已经通 ...

  10. jmeter返回结果是乱码及Unicode转中文

    1.结果树中有乱码的处理方式 到bin目录下面修改jmeter的配置文件 使用jmeter进行压测时,如果响应数据的返回结果有乱码,则修改jmeter.properties文件中的该字段的值改为utf ...