Android提供了功能丰富,应用广泛的对话框:AlertDialog。
类图如下:
AlertDialog
AlertDialog对话框分为三个区域:
1.标题区。包括图标和文本,setIcon()设置图标,setTitle()设置文本。
2.内容区。可以是简单的文本,也可以是复杂的View。AlertDialog提供了6个方法来设置不同的内容。
1)setMessage(),简单的文本。
2)setItems(),简单列表项。
3)setSingleChoiceItems(),单选列表项。
4)setMultiChoiceItems(),多选列表项。
5)setAdapter(),自定义列表项。
6)setView(),自定义View。
3.按钮区。可通过三个方法分别设置三个按钮,setPositiveButton()、setNegativeButton()或setNeutralButton()。
下面通过一个例子展示6种不同内容的AlertDialog。
Activity类:
- package com.zzj.ui.dialogdemo;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.content.DialogInterface.OnMultiChoiceClickListener;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.ArrayAdapter;
- import android.widget.EditText;
- import android.widget.Toast;
- import com.zzj.ui.R;
- public class DialogDemoActivity extends Activity {
- private String singleChoice;
- private List<String> multiChoices = new ArrayList<String>();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.dialogdemo_activity);
- }
- public void click(View v) {
- AlertDialog.Builder builder = new AlertDialog.Builder(
- DialogDemoActivity.this);
- builder.setIcon(R.drawable.ic_launcher);
- switch (v.getId()) {
- case R.id.text_dialog:// 简单对话框
- builder.setTitle("提示");
- builder.setMessage("你想干什么?");
- builder.setPositiveButton("保存",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(DialogDemoActivity.this,
- "你点击了 -- > " + which, Toast.LENGTH_LONG)
- .show();
- return;
- }
- });
- builder.setNegativeButton("删除",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(DialogDemoActivity.this,
- "你点击了 -- > " + which, Toast.LENGTH_LONG)
- .show();
- return;
- }
- });
- builder.setNeutralButton("取消",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(DialogDemoActivity.this,
- "你点击了 -- > " + which, Toast.LENGTH_LONG)
- .show();
- return;
- }
- });
- break;
- case R.id.list_dialog:// 列表对话框
- builder.setTitle("请选择省份");
- final String[] items = new String[] { "湖南", "广东", "北京", "上海", "辽宁" };
- builder.setItems(items, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(DialogDemoActivity.this,
- "你选择了 -- > " + items[which], Toast.LENGTH_LONG)
- .show();
- return;
- }
- });
- builder.setNegativeButton("取消",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- return;
- }
- });
- break;
- case R.id.single_choice:// 单选对话框
- singleChoice = null;
- builder.setTitle("请选择省份");
- final String[] singleItems = new String[] { "湖南", "广东", "北京", "上海",
- "辽宁" };
- builder.setSingleChoiceItems(singleItems, 0,
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- singleChoice = singleItems[which];
- return;
- }
- });
- singleChoice = singleItems[0];
- builder.setPositiveButton("确定",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- if (singleChoice != null) {
- Toast.makeText(DialogDemoActivity.this,
- "你选择了 -- > " + singleChoice,
- Toast.LENGTH_LONG).show();
- }
- return;
- }
- });
- builder.setNegativeButton("取消",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- singleChoice = null;
- return;
- }
- });
- break;
- case R.id.muti_choice:// 多选对话框
- multiChoices.clear();
- builder.setTitle("请选择省份");
- final String[] multiItems = new String[] { "湖南", "广东", "北京", "上海",
- "辽宁" };
- builder.setMultiChoiceItems(multiItems, new boolean[] { true,
- false, false, false, false },
- new OnMultiChoiceClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which,
- boolean isChecked) {
- if (isChecked) {
- multiChoices.add(multiItems[which]);
- } else {
- multiChoices.remove(multiItems[which]);
- }
- return;
- }
- });
- multiChoices.add(multiItems[0]);
- builder.setPositiveButton("确定",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- if (!multiChoices.isEmpty()) {
- Toast.makeText(DialogDemoActivity.this,
- "你选择了 -- > " + multiChoices,
- Toast.LENGTH_LONG).show();
- }
- return;
- }
- });
- builder.setNegativeButton("取消",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- multiChoices.clear();
- return;
- }
- });
- break;
- case R.id.custom_item:// 自定义列表项对话框
- builder.setTitle("请选择省份");
- final String[] customItems = new String[] { "湖南", "广东", "北京", "上海",
- "辽宁" };
- builder.setAdapter(new ArrayAdapter<String>(
- DialogDemoActivity.this,
- android.R.layout.simple_list_item_1, customItems),
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(DialogDemoActivity.this,
- "你选择了 --> " + customItems[which],
- Toast.LENGTH_LONG).show();
- return;
- }
- });
- builder.setPositiveButton("取消",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- return;
- }
- });
- break;
- case R.id.custom_view: // 自定义View对话框
- builder.setTitle("登录");
- LayoutInflater inflater = LayoutInflater
- .from(DialogDemoActivity.this);
- final View view = inflater.inflate(R.layout.dialog_custom_view,
- null);
- builder.setView(view);
- builder.setPositiveButton("登录",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- String username = ((EditText) view
- .findViewById(R.id.edit_username))
- .getText().toString();
- String password = ((EditText) view
- .findViewById(R.id.edit_password))
- .getText().toString();
- if ("001".equals(username)
- && "123".equals(password)) {
- Toast.makeText(DialogDemoActivity.this,
- "登录成功!", Toast.LENGTH_LONG).show();
- } else {
- Toast.makeText(DialogDemoActivity.this,
- "用户名或密码错误!", Toast.LENGTH_LONG).show();
- }
- return;
- }
- });
- builder.setNegativeButton("取消",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- return;
- }
- });
- break;
- default:
- break;
- }
- // builder.show();
- builder.create().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:orientation="vertical" >
- <Button
- android:id="@+id/text_dialog"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="click"
- android:text="简单文本对话框" />
- <Button
- android:id="@+id/list_dialog"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="click"
- android:text="列表对话框" />
- <Button
- android:id="@+id/single_choice"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="click"
- android:text="单选列表框" />
- <Button
- android:id="@+id/muti_choice"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="click"
- android:text="多选列表框" />
- <Button
- android:id="@+id/custom_item"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="click"
- android:text="自定义列表项对话框" />
- <Button
- android:id="@+id/custom_view"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="click"
- android:text="自定义View对话框" />
- </LinearLayout>
自定义View布局文件dialog_custom_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" >
- <EditText
- android:id="@+id/edit_username"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:inputType="text"
- android:hint="请输入用户名"
- android:ems="10" >
- </EditText>
- <EditText
- android:id="@+id/edit_password"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:inputType="textPassword"
- android:hint="请输入密码"
- android:ems="10" />
- </LinearLayout>