1.1 普通对话框
1.2 列表对话框
1.3 单选对话框
1.4 多选对话框
1.5 时间对话框
1.6 日期对话框
\
1.7 进度条对话框
1.8 自定义对话框
2.1 activity_main.xml文件代码
<span style="font-family:Microsoft YaHei;font-size:14px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
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=".MainActivity" >
<Button
android:id="@+id/bt_dialog1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:onClick="setOnClick"
android:text="普通对话框" />
<Button
android:id="@+id/bt_dialog2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:onClick="setOnClick2"
android:text="列表对话框" />
<Button
android:id="@+id/bt_dialog3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:onClick="setOnClick3"
android:text="单选对话框" />
<Button
android:id="@+id/bt_dialog4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:onClick="setOnClick4"
android:text="多选对话框" />
<Button
android:id="@+id/bt_dialog5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:onClick="setOnClick5"
android:text="时间对话框" />
<Button
android:id="@+id/bt_dialog6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:onClick="setOnClick6"
android:text="日期对话框" />
<Button
android:id="@+id/bt_dialog7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:onClick="setOnClick7"
android:text="进度条对话框" />
<Button
android:id="@+id/bt_dialog8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:onClick="setOnClick8"
android:text="自定义对话框" />
</LinearLayout></span>
<------------------------------------分割线---------------------------------------->
2.2 Activity代码:
<span style="font-family:Microsoft YaHei;font-size:14px;">package com.example.android_test7;
import java.util.Calendar;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText name;
private EditText pwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// 普通对话框的方法
public void setOnClick(View view) {
// 配置AlertDialog的Builder构造器构造一个对话框构造器
AlertDialog.Builder build = new Builder(this);
// 设置对话框的标题
build.setTitle("更新");
// 设置对话框的图标
build.setIcon(android.R.drawable.ic_popup_sync);
// 设置对话框的内容
build.setMessage("发现最新版本是否需要更新?");
// 设置对话框的正级按钮
build.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "正在更新中", 0).show();
}
});
build.setNeutralButton("中性的", null);
// 对话框的负极按钮
build.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "取消更新", 0).show();
}
});
// 关闭'按钮点界面的自动取消'功能
build.setCancelable(false);
// 显示对话框
build.show();
}
// 1.2 列表对话框
public void setOnClick2(View v) {
// new Builder构造器
AlertDialog.Builder build = new Builder(this);
// 设置标题
build.setTitle("选择语言").setIcon(
android.R.drawable.ic_notification_overlay);
// 多选框的内容数组
final String s[] = { "java", "c/c++", ".Net" };
// 设置多选的内容
build.setItems(s, new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this, "你选中的是" + s[arg1], 0).show();
}
});
// 关闭'按钮点界面的自动取消'功能
build.setCancelable(false);
// 显示对话框
build.show();
}
// 1.3 单选对话框
public void setOnClick3(View v) {
AlertDialog.Builder build = new Builder(this);
build.setTitle("选择语言").setIcon(
android.R.drawable.ic_search_category_default);
final String s[] = { "java", "c/c++", ".Net" };
// 设置多选内容
build.setSingleChoiceItems(s, 0, new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this, "你选中了" + s[arg1], 0).show();
}
});
// 关闭'按钮点界面的自动取消'功能
build.setCancelable(false);
build.show();
}
// 1.4 多选对话框
public void setOnClick4(View v) {
AlertDialog.Builder build = new Builder(this);
build.setTitle("多选语言").setIcon(
android.R.drawable.ic_search_category_default);
final String s[] = { "java", "c/c++", ".Net" };
final boolean isnot[] = { false, false, false };
// 设置多选内容
build.setMultiChoiceItems(s, isnot, new OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
isnot[which] = isChecked;
}
});
build.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String str = "";
for (int i = 0; i < isnot.length; i++) {
if (isnot[i] == true) {
str += s[i] + ",";
}
}
if (!str.equals("")) {
str = str.substring(0, str.length() - 1);
Toast.makeText(MainActivity.this, "你选中了" + str, 0).show();
}
}
});
build.setNegativeButton("取消", null);
// 关闭'按钮点界面的自动取消'功能
build.setCancelable(false);
build.show();
}
// 1.5 时间对话框
public void setOnClick5(View v) {
// 取当前时间
Calendar c = Calendar.getInstance();
final int hh = c.get(c.HOUR_OF_DAY);
final int mm = c.get(c.MINUTE);
TimePickerDialog pdialog = new TimePickerDialog(this,
new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
Toast.makeText(MainActivity.this,
"设置的时间为" + hourOfDay + "时" + minute + "分", 0)
.show();
}
}, hh, mm, true);
pdialog.setTitle("选择时间");
pdialog.setIcon(android.R.drawable.ic_lock_idle_alarm);
pdialog.show();
}
// 日期对话框
public void setOnClick6(View v) {
// 拿到当前系统的时间
Calendar c = Calendar.getInstance();
// 分别设置年月日3个变量
final int year = c.get(c.YEAR);
final int month = c.get(c.MONTH);
final int day = c.get(c.DAY_OF_MONTH);
// 创建日期对话框的dialog
DatePickerDialog dialog = new DatePickerDialog(this,
new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month,
int day) {
// 提示出来看下是否拿到值
String text = year + "-" + month + "-" + day;
Toast.makeText(MainActivity.this, text, 0).show();
}
}, year, month, day);// 默认设置为当前的系统时间
// 对话框的标题
dialog.setTitle("选择日期");
// 对话框的图标
dialog.setIcon(android.R.drawable.ic_lock_idle_alarm);
// 关闭'按钮点界面的自动取消'功能
dialog.setCancelable(false);
// 显示对话框
dialog.show();
}
// 进度条对话框
public void setOnClick7(View v) {
final ProgressDialog dialog = new ProgressDialog(this);
// 正在加载
dialog.setTitle("正在加载");
dialog.setIcon(android.R.drawable.ic_menu_more);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 主题
dialog.setMax(100);// 最大值
// 关闭'按钮点界面的自动取消'功能
dialog.setCancelable(false);
dialog.show();
// 进度条效果
Thread th = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
dialog.setProgress(i);
try {
// 休眠
Thread.sleep(80);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 关闭当前对话框
dialog.dismiss();
}
});
th.start();
}
// 自定义对话框
public void setOnClick8(View v) {
// 创建对话框
AlertDialog.Builder builde = new AlertDialog.Builder(this);
// 设置标题
builde.setTitle("登录");
// 设置图标
builde.setIcon(android.R.drawable.arrow_down_float);
// LayoutInflater.from获取到自定义的login.xml的视图,第二参数用于追加另一个布局使用
v = LayoutInflater.from(MainActivity.this)
.inflate(R.layout.login, null);
// 拿到name和pwd的值
name = (EditText) v.findViewById(R.id.et_name);
pwd = (EditText) v.findViewById(R.id.et_pwd);
// 设置view
builde.setView(v);
// 登录按钮监听事件
builde.setPositiveButton("登录", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String username = name.getText() + "";
String userpwd = pwd.getText() + "";
if (username != "" || userpwd != "") {
Toast.makeText(MainActivity.this,
"用户名为" + username + "密码为" + userpwd, 0).show();
} else {
Toast.makeText(MainActivity.this, "请输入正确的账户和密码", 0).show();
}
}
});
builde.setNeutralButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
});
// 显示对话框
builde.show();
// 关闭'按钮点界面的自动取消'功能
builde.setCancelable(false);
}
}
</span>
2.3 自定义对话框xml文件代码
<span style="font-family:Microsoft YaHei;font-size:14px;"><?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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:" />
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:" />
<EditText
android:id="@+id/et_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" />
</LinearLayout>
</span>
简单的各种对话框的案例供参考