$Android AlertDialog的各种用法总结
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中的内容:

随机推荐
- GoWeb编程之多路复用
GoWeb编程多路复用 在web编程中,比如我们要一个Url对应一个处理函数,或者一个页面对应一个静态文件,这些都需要进行处理,这个时候就是我们多路复用派上用场了. package main impo ...
- 40-语言入门-40-C小加之随机数
题目地址: http://acm.nyist.net/JudgeOnline/problem.php?pid=255 15 20 32 40 67 89 300 400 代码: #includ ...
- hdu2588 GCD 给定n,m。求x属于[1,n]。有多少个x满足gcd(x,n)>=m; 容斥或者欧拉函数
GCD Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): Accepted Sub ...
- Hibernate通过many-to-one元素的一对一映射
在hibernate中我们可以通过两种方式来执行一对一映射: 通过many-to-one元素标签 通过one-to-one元素标签 在这里,我们将通过多对一的many-to-one元素进行一对一的映射 ...
- std::condition_variable(3)复习
#include <iostream> // std::cout #include <thread> // std::thread #include <mutex> ...
- (转载)java提高篇(五)-----抽象类与接口
接口和内部类为我们提供了一种将接口与实现分离的更加结构化的方法. 本文是转载的(尊重原著),原文地址:http://www.cnblogs.com/chenssy/p/3376708.html 抽象类 ...
- 【BZOJ4825】[Hnoi2017]单旋 线段树+set
[BZOJ4825][Hnoi2017]单旋 Description H 国是一个热爱写代码的国家,那里的人们很小去学校学习写各种各样的数据结构.伸展树(splay)是一种数据结构,因为代码好写,功能 ...
- 报表生成之Hyperion SQR
SQR(Structured Query Reporter)是为从数据库管理系统生成报告而设计的一种编程语言.名称是结构化查询报告的缩写,这表明它与SQL(结构化查询语言)的关系.任何SQL语句可以嵌 ...
- Facebook背后的软件
Facebook的数据规模使得很多传统的解决方案根本不适用,或者无法分解来处理.保持一个拥有5亿用户的系统一直稳定可靠的运行,并不是一件很容易的事情.这篇文章介绍了一下Facebook使用的软件. F ...
- jdbc PreparedStatement 防止sql注入的关键代码片段
mysql-connector-java-5.1.38.jar PreparedStatement 的 setString(int parameterIndex, String x) 方法 for ( ...