AlertDialog,对话框,用于给用户提示信息或者做出一些选择,同时它也是其他Dialog的的父类:比如ProgressDialog,TimePickerDialog等。
AlertDialog的父类是:Dialog。
AlertDialog在使用时并不能像其他的控件一样,例如Toast和Notification,这些控件是new出来的,但是AlertDialog不能通过new来使用,而是需要使用到该类中的一个静态内部类:public static class Builder,然后来调用AlertDialog里的相关方法,来对AlertDialog进行设置,最后调用show()方法来显示我们的AlertDialog对话框。
使用对话框的步骤:
1.创建AlertDialog.Builder对象
2.给对话框添加内容,icon,title,message,button等等
3.调用AlertDialog.Builder对象的creat()方法,创建这个对话框对象,并用show()方法显示。
实践一下:
先定义一个布局文件,添加几个button,用于点击显示对话框:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.quaie.wms.myapplication.Main5Activity">
<!--对话框选择-->
<Button
android:id="@+id/AlertDialog01"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="center"
android:text="一个基本的对话框" />
<Button
android:id="@+id/AlertDialog02"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="center"
android:text="在对话框中添加一个列表" />
<Button
android:id="@+id/AlertDialog03"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="center"
android:text="在对话框中添加单选列表" />
<Button
android:id="@+id/AlertDialog04"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="center"
android:text="在对话框中添加复选列表" />
<Button
android:id="@+id/AlertDialog05"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="center"
android:text="显示一个自定义的对话框" />
</LinearLayout>
在activity中声明这些控件,此处省略千行。
声明对话框对象:
private AlertDialog mDialog = null;
private AlertDialog.Builder mBuilder = null;
显示一个基本的对话框,包括icon,标题,包含的信息还有三个按钮,积极的,消极的,中立的:
//开始默念
mDialog = null;
//第一步
mBuilder = new AlertDialog.Builder(Main5Activity.this);
//第二步
mDialog = mBuilder.setIcon(R.mipmap.iv_lol_icon1)
.setTitle("系统提示:")
.setMessage("你已经进入对话框范围。")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Main5Activity.this, "你点击了确定按钮", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Main5Activity.this, "你点击了取消按钮", Toast.LENGTH_SHORT).show();
}
})
.setNeutralButton("One More", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Main5Activity.this, "你点击了中立按钮", Toast.LENGTH_SHORT).show();
}
})
//第三步
.create();
mDialog.show();
在对话框中添加一个列表,其实很简单,不需要listview也能实现,用android自带的setItems()就可以。下面是代码:
//准备数据源
final String[] names = new String[]{"Teemo", "Noble", "Able", "Bear", "Pen", "Sibley", "Haber", "Teemo", "Noble", "Able", "Bear", "Pen", "Sibley", "Haber", "Teemo", "Noble", "Able", "Bear", "Pen", "Sibley", "Haber"};
//默念三部
mDialog = null;
mBuilder = new AlertDialog.Builder(Main5Activity.this);
mDialog = mBuilder.setIcon(R.mipmap.iv_lol_icon2)
.setTitle("取一个你喜欢的名字吧")
//这里添加一个列表
.setItems(names, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Main5Activity.this, "你选取的名字是:" + names[which], Toast.LENGTH_SHORT).show();
}
}).create();
mDialog.show();
同样的,在对话框中添加一个单选列表,用setSingleChoiceItems():
final String[] names03 = new String[]{"Teemo", "Noble", "Able", "Bear", "Pen", "Sibley", "Haber", "Teemo", "Noble", "Able", "Bear", "Pen", "Sibley", "Haber", "Teemo", "Noble", "Able", "Bear", "Pen", "Sibley", "Haber"};
mDialog = null;
mBuilder = new AlertDialog.Builder(Main5Activity.this);
mDialog = mBuilder.setIcon(R.mipmap.iv_lol_icon3)
.setTitle("再来选择一次吧")
.setSingleChoiceItems(names03, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Main5Activity.this, "你选取的名字是:" + names03[which], Toast.LENGTH_SHORT).show();
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Main5Activity.this, "确定", Toast.LENGTH_SHORT).show();
}
}).create();
mDialog.show();
复选列表只是多了一个给按钮初始化的步骤:
final String[] names04 = new String[]{"Teemo", "Noble", "Able", "Bear", "Pen", "Sibley", "Haber",
"Teemo1", "Noble1", "Able1", "Bear1", "Pen1", "Sibley1", "Haber1"};
//给复选按钮赋初始值
boolean[] checkItems = new boolean[names04.length];
for (int i = 0; i < names04.length; i++) {
if (i < names04.length / 2) {
checkItems[i] = false;
} else {
checkItems[i] = true;
}
}
mDialog = null;
mBuilder = new AlertDialog.Builder(Main5Activity.this);
mDialog = mBuilder.setIcon(R.mipmap.iv_lol_icon3)
.setTitle("再来选择一次吧")
.setMultiChoiceItems(names04, checkItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(Main5Activity.this, "你选取的名字是:" + names04[which], Toast.LENGTH_SHORT).show();
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Main5Activity.this, "确定", Toast.LENGTH_SHORT).show();
}
}).create();
mDialog.show();
在进行自定义对话框之前,需要自定义一个对话框布局,就想平常的布局一样:
<?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:background="#d3d3d3"
android:orientation="vertical">
<!--自定义的对话框-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/dialog_title"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center_vertical"
android:text="这里是显示标题的地方。"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="@+id/dialog_close"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentEnd="true"
android:background="@mipmap/button_close" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#fff">
</View>
<TextView
android:id="@+id/dialog_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:text="这里是显示信息的地方。"
android:textColor="#000"
android:textSize="18sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<Button
android:id="@+id/dialog_ok"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#000"
android:text="ok"
android:textColor="#ffffff"
android:textSize="16sp"
android:textStyle="bold" />
<Button
android:id="@+id/dialog_notok"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#000"
android:text="notok"
android:textColor="#ffffff"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
在activity中,给对话框进行操作:
mDialog = null;
mBuilder = new AlertDialog.Builder(Main5Activity.this);
//加载布局加载器
LayoutInflater inflater = getLayoutInflater();
//后面需要用这个view进行find等一系列操作
View view_custom = inflater.inflate(R.layout.dialogview, null, false);
//加载自定义布局
mBuilder.setView(view_custom);
//点击非对话框区域对话框不会消失,默认是会消失
mBuilder.setCancelable(false);
mDialog = mBuilder.create();
view_custom.findViewById(R.id.dialog_close).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDialog.dismiss();
}
});
view_custom.findViewById(R.id.dialog_ok).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Main5Activity.this, "ok", Toast.LENGTH_SHORT).show();
mDialog.dismiss();
}
});
view_custom.findViewById(R.id.dialog_notok).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Main5Activity.this, "not ok", Toast.LENGTH_SHORT).show();
mDialog.dismiss();
}
});
TextView title = (TextView) view_custom.findViewById(R.id.dialog_title);
TextView message = (TextView) view_custom.findViewById(R.id.dialog_message);
title.setText("我是后来加的标题。");
message.setText("我是后来加的新信息。");
mDialog.show();
over
这只是基本用法。