1、当前界面弹出的一个小窗口,用于提示用户重要的信息,并获得焦点。
2、用户要与它进行互动,可以接受用户输入的信息,也可以反馈信息给用户,如下载进度,退出提示等。常用的对话框:
1、简单对话框
2、简单列表对话框3、单选按钮对话框
4、多选按钮对话框5、自定义列表对话框
6、自定义布局对话框
对话框包含四个区域:
1、图标区
2、标题区3、内容区
4、按钮区如何创建对话框:
1、创建AlertDialog.Builder的对象
2、使用Builder的setTitle()或者setCustomTitil()来设置标题。
3、使用Builder的setIcon()来设置图标4、使用Builder的相关方法来设置内容
5、使用Builder的setpositivebutton()和setnegativeButton()设置按钮,setNeutralButton()用于设置中立的按钮。6、使用Builder的create()方法创建一个AlertDialog的对象。
7、然后把Dialog对象通过show()方法显示出来。AlertDialog.Builder提供了六种指定对话框内容的方法
1、setMessage() 设置对话框内容为简单的文本格式。
2、setitems() 设置对话框的内容为简单的列表项。3、setSingleChoiceItems 设置对话框的内容为单选按钮的列表项
4、setMultiChoiceitems() 设置对话框内容为多选按钮列表项
5、setAdapter() 设置对话框内容为自定义的列表项6、setView() 设置对话框内容为自定义的View
package com.example.toast_demo01;
import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
@SuppressLint("ShowToast")
public class AlertDialogDemo extends Activity implements OnClickListener {
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private Button button6;
private Button button7;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alertaialog);
initEvent();
}
// findViewById集合方法
public void initEvent() {
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(this);
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(this);
button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(this);
button4 = (Button) findViewById(R.id.button4);
button4.setOnClickListener(this);
button5 = (Button) findViewById(R.id.button5);
button5.setOnClickListener(this);
button6 = (Button) findViewById(R.id.button6);
button6.setOnClickListener(this);
button7 = (Button) findViewById(R.id.button7);
button7.setOnClickListener(this);
}
// 监听事件
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
simpleDialog();
break;
case R.id.button2:
simpleListDialog();
break;
case R.id.button3:
simpleSingleChoiceDialog();
break;
case R.id.button4:
simpleMultiChoiceDialog();
break;
case R.id.button5:
simpleCustomAdapterDiaglog();
break;
case R.id.button6:
simpleCustomViewDialog();
break;
default:
break;
}
}
// 简单窗口
public void simpleDialog() {
// AlertDialog.Builder不写出全局:如果全局,会被反复赋值,这样容易产生错误。
AlertDialog.Builder builder = new AlertDialog.Builder(
AlertDialogDemo.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("对话框提示信息");
builder.setMessage("简单的对话框");
// 设置第一个按钮
builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertDialogDemo.this, "确认", 0).show();
}
});
// 设置中间按钮
builder.setNeutralButton("中立", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertDialogDemo.this, "中立", 0).show();
}
});
// 设置最后一个按钮
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertDialogDemo.this, "取消", 0).show();
}
});
// 显示对话框关键性代码,没写的话对话框不显示
builder.create().show();
}
// 选项数组
String items[] = { "三国", "水浒", "红楼", "西游" };
// 简单列表窗口
public void simpleListDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(
AlertDialogDemo.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("列表对话框");
// 对话框中内容为列表的写法
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertDialogDemo.this, items[which], 0).show();
}
});
builder.create().show();
}
// 单选按钮对话框
int singleIndex = 0;
public void simpleSingleChoiceDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(
AlertDialogDemo.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("单选按钮对话框");
builder.setSingleChoiceItems(items, 0,
new DialogInterface.OnClickListener() {
// 0为默认选择的按钮
@Override
public void onClick(DialogInterface dialog, int which) {
singleIndex = which;
}
});
builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertDialogDemo.this, items[singleIndex], 0)
.show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create().show();
}
// 定义多选选项的初始状态
boolean checkedItems[] = { false, false, false, false };
ArrayList<Integer> myChseList = new ArrayList<Integer>();
// 多选按钮对话框
public void simpleMultiChoiceDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(
AlertDialogDemo.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("多选按钮对话框");
builder.setMultiChoiceItems(items, checkedItems,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
// 改变选中项的状态
checkedItems[which] = isChecked;
if (isChecked) {
myChseList.add(which);
} else {
myChseList.remove(which);
}
}
});
builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String multi = "";
// Toast各个选中项
for (int i = 0; i < checkedItems.length; i++) {
if (checkedItems[i] == true) {
multi = multi + items[i];
checkedItems[i] = false;
}
}
Toast.makeText(AlertDialogDemo.this, multi, 0).show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create().show();
}
// 自定义列表对话框
public void simpleCustomAdapterDiaglog() {
AlertDialog.Builder builder = new AlertDialog.Builder(
AlertDialogDemo.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("自定义对话框");
builder.setAdapter(new ArrayAdapter<String>(AlertDialogDemo.this,
android.R.layout.simple_list_item_1, items),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertDialogDemo.this, items[which], 0)
.show();
}
});
builder.create().show();
}
// 自定义布局对话框
@SuppressWarnings("unused")
private void simpleCustomViewDialog() {
LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(
R.layout.dialog_login, null);
AlertDialog.Builder builder = new AlertDialog.Builder(
AlertDialogDemo.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("自定义布局对话框");
builder.setView(layout);
builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create().show();
}
}