android中的AlertDialog具体概述
-
AlertDialog的构造方法所有是Protected的。所以不能直接通过new一个AlertDialog来创建出一个AlertDialog。
要创建一个AlertDialog,就要用到AlertDialog.Builder中的create()方法。
使用AlertDialog.Builder创建对话框须要了解下面几个方法:
setTitle :为对话框设置标题
setIcon :为对话框设置图标
setMessage:为对话框设置内容
setView : 给对话框设置自己定义样式
setItems :设置对话框要显示的一个list。一般用于显示几个命令时
setMultiChoiceItems :用来设置对话框显示一系列的复选框
setNeutralButton :普通buttonsetPositiveButton :给对话框加入"Yes"button
setNegativeButton :对话框加入"No"button
create : 创建对话框
show :显示对话框
一、简单的AlertDialog以下,创建一个简单的ALertDialog并显示它:
[java] package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
public class Dialog_AlertDialogDemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("对话框的标题").
setMessage("对话框的内容").
setIcon(R.drawable.ic_launcher).
create();
alertDialog.show();
}
}
package com.tianjf;import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;public class Dialog_AlertDialogDemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("对话框的标题").
setMessage("对话框的内容").
setIcon(R.drawable.ic_launcher).
create();
alertDialog.show();
}
}执行结果例如以下:
二、带button的AlertDialog
上面的样例非常easy,以下我们在这个AlertDialog上面加几个Button,实现删除操作的提示对话框
[java] package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
public class Dialog_AlertDialogDemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("确定删除?").
setMessage("您确定删除该条信息吗?").
setIcon(R.drawable.ic_launcher).
setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNeutralButton("查看详情", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}
package com.tianjf;import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;public class Dialog_AlertDialogDemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("确定删除?").
setMessage("您确定删除该条信息吗?").
setIcon(R.drawable.ic_launcher).
setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNeutralButton("查看详情", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}在这个样例中,我们定义了三个button,各自是"Yes"button,"No"button以及一个普通button,每一个button都有onClick事件,TODO的地方能够放点了button之后想要做的一些处理看一下执行结果:

能够看到三个button加入到了AlertDialog上,三个没有加入事件处理的button。点了仅仅是关闭对话框,没有不论什么其它操作。
三、类似ListView的AlertDialog
用setItems(CharSequence[] items, final OnClickListener listener)方法来实现类似ListView的AlertDialog第一个參数是要显示的数据的数组。第二个參数是点击某个item的触发事件
[java] package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String[] arrayFruit = new String[] { "苹果", "橘子", "草莓", "香蕉" };
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("你喜欢吃哪种水果?").
setIcon(R.drawable.ic_launcher)
.setItems(arrayFruit, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[which], Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}
package com.tianjf;import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;public class Dialog_AlertDialogDemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);final String[] arrayFruit = new String[] { "苹果", "橘子", "草莓", "香蕉" };
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("你喜欢吃哪种水果?").
setIcon(R.drawable.ic_launcher)
.setItems(arrayFruit, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[which], Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}执行结果例如以下:
四、类似RadioButton的AlertDialog
用setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener)方法来实现类似RadioButton的AlertDialog第一个參数是要显示的数据的数组,第二个參数是初始值(初始被选中的item),第三个參数是点击某个item的触发事件
在这个样例里面我们设了一个selectedFruitIndex用来记住选中的item的index
[java] package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {
private int selectedFruitIndex = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String[] arrayFruit = new String[] { "苹果", "橘子", "草莓", "香蕉" };
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("你喜欢吃哪种水果?").
setIcon(R.drawable.ic_launcher)
.setSingleChoiceItems(arrayFruit, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
selectedFruitIndex = which;
}
}).
setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[selectedFruitIndex], Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}
package com.tianjf;import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;public class Dialog_AlertDialogDemoActivity extends Activity {
private int selectedFruitIndex = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);final String[] arrayFruit = new String[] { "苹果", "橘子", "草莓", "香蕉" };
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("你喜欢吃哪种水果?").
setIcon(R.drawable.ic_launcher)
.setSingleChoiceItems(arrayFruit, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
selectedFruitIndex = which;
}
}).
setPositiveButton("确认", new DialogInterface.OnClickListener() {@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[selectedFruitIndex], Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}执行结果例如以下:

五、类似CheckBox的AlertDialog
用setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, final OnMultiChoiceClickListener listener)方法来实现类似CheckBox的AlertDialog
第一个參数是要显示的数据的数组,第二个參数是选中状态的数组。第三个參数是点击某个item的触发事件[java] package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String[] arrayFruit = new String[] { "苹果", "橘子", "草莓", "香蕉" };
final boolean[] arrayFruitSelected = new boolean[] {true, true, false, false};
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("你喜欢吃哪种水果?").
setIcon(R.drawable.ic_launcher)
.setMultiChoiceItems(arrayFruit, arrayFruitSelected, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
arrayFruitSelected[which] = isChecked;
}
}).
setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < arrayFruitSelected.length; i++) {
if (arrayFruitSelected[i] == true)
{
stringBuilder.append(arrayFruit[i] + "、");
}
}
Toast.makeText(Dialog_AlertDialogDemoActivity.this, stringBuilder.toString(), Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}
package com.tianjf;import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;public class Dialog_AlertDialogDemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);final String[] arrayFruit = new String[] { "苹果", "橘子", "草莓", "香蕉" };
final boolean[] arrayFruitSelected = new boolean[] {true, true, false, false};Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("你喜欢吃哪种水果?").
setIcon(R.drawable.ic_launcher)
.setMultiChoiceItems(arrayFruit, arrayFruitSelected, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
arrayFruitSelected[which] = isChecked;
}
}).
setPositiveButton("确认", new DialogInterface.OnClickListener() {@Override
public void onClick(DialogInterface dialog, int which) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < arrayFruitSelected.length; i++) {
if (arrayFruitSelected[i] == true)
{
stringBuilder.append(arrayFruit[i] + "、");
}
}
Toast.makeText(Dialog_AlertDialogDemoActivity.this, stringBuilder.toString(), Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}执行结果例如以下:
六、自己定义View的AlertDialog
有时候我们不能满足系统自带的AlertDialog风格,就比方说我们要实现一个Login画面,实username和password,这时我们就要用到自己定义View的AlertDialog先创建Login画面的布局文件
[html] <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/user" />
<EditText
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" >
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/passward" />
<EditText
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" ><LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" ><TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/user" /><EditText
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" ><TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/passward" /><EditText
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout></LinearLayout>
然后在Activity里面把Login画面的布局文件加入到AlertDialog上[java] package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
public class Dialog_AlertDialogDemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 取得自己定义View
LayoutInflater layoutInflater = LayoutInflater.from(this);
View myLoginView = layoutInflater.inflate(R.layout.login, null);
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("用户登录").
setIcon(R.drawable.ic_launcher).
setView(myLoginView).
setPositiveButton("登录", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}
package com.tianjf;import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;public class Dialog_AlertDialogDemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);// 取得自己定义View
LayoutInflater layoutInflater = LayoutInflater.from(this);
View myLoginView = layoutInflater.inflate(R.layout.login, null);
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("用户登录").
setIcon(R.drawable.ic_launcher).
setView(myLoginView).
setPositiveButton("登录", new DialogInterface.OnClickListener() {@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}执行结果例如以下:
android中的AlertDialog具体概述的更多相关文章
- Android 中的AlertDialog使用自定义布局
Android使用指定的View开发弹窗功能 Android开发中进程会使用到我们的AlertDialog,但是比较可惜的是我们的Android原生的AlertDialog的效果又比较的简陋,这个时候 ...
- Android中的AlertDialog使用示例五(自定义对话框)
在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式. ...
- Android中的AlertDialog使用示例四(多项选择确定对话框)
在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式. ...
- Android中的AlertDialog使用示例三(单向选择确定对话框)
在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式. ...
- Android中的AlertDialog使用示例二(普通选项对话框)
在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式. ...
- Android中的AlertDialog使用示例一(警告对话框)
在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式. ...
- Android中使用AlertDialog实现几种不同的对话框
场景 app中常见的对话框. 简单的带确定取消按钮的对话框 带列表的对话框 带单项选择的对话框 带多项选择的对话框 注: 博客: https://blog.csdn.net/badao_liumang ...
- Android中的AlertDialog和ProgressDialog用法
手机APP对话框是很多APP都有的下面来看下怎么实现的吧, 打开Android studio 然他自动创建好布局和类; 下面我们修改activity_main.xml中的代码 <?xml ver ...
- Android中的线程池概述
线程池 Android里面,耗时的网络操作,都会开子线程,在程序里面直接开过多的线程会消耗过多的资源,在众多的开源框架中也总能看到线程池的踪影,所以线程池是必须要会把握的一个知识点; 线程运行机制 开 ...
随机推荐
- CSS变量实用指南及注意事项
近年来,一些动态特性已经开始成为 CSS 语言本身的一部分. CSS变量 – 官方的术语为 "自定义属性" – 已经已经加入规范并且具有很好的浏览器支持,而 CSS mixins ...
- Python-基础-day4
深浅copy 1.先看赋值运算 h1 = [1,2,3,['aihuidi','hhhh']] h2 = h1 h1[0] = 111 print(h1) print(h2) #结果: # [111, ...
- Jquery_Validate 表单校验的使用
一.效果图: 二.JqueryValidate的好处 在做注册.或者类似以上的表单提交的时候,大家是不是都很烦那种,把数据拿到后台去判断, 可能经过了正则表达式之类的复杂判断,然后发现数据错误.接着通 ...
- PatentTips - Sleep state mechanism for virtual multithreading
BACKGROUND The present disclosure relates generally to information processing systems and, more spec ...
- WinServer-PowerShell基础
命令的参数: [-name] 这个参数必须要有,string表示name参数接受什么样的实参,<>表示参数可以接受的实参类型,通常出现set get add都会伴随着必须参数 [-name ...
- POJ 3695
可以用容斥原理来求.求两个矩形的并的时候可以使用条件 x1=max(p.x1,q.x1);y1=max(p.y1,q.y1);x2=min(p.x2,q.x2);y2=min(p.y2,q.y2); ...
- WPF中多线程统计拆箱装箱和泛型的运行效率
WPF中多线程统计拆箱装箱和泛型的执行效率.使用的知识点有泛型.多线程.托付.从样例中能够看到使用泛型的效率至少提升2倍 MainWindow.xaml <Window x:Class=&quo ...
- flex 通过htmlservices链接moss的rest(rest 的get post方式)
一:flex debug(调试)--trace() --moss导入 flex学习:1.flex出现不能使用trace调试语句的问题,控制台无信息输出.这个问题不须要改动安装文件的參量. 仅仅须要下载 ...
- [Angular] Send Data via HTTP using Angular HttpParams
Obviously in a real world application we do not only fetch data from the backend, but we also send d ...
- leveldb学习:sstable(2)
block写入:block_builder block.h和.cc里定义了block的entry存储格式和restart,提供了entry的查找接口以及迭代器.那么怎样往写block里写entry呢? ...