1 1.普通对话框:
2
3 Builder alert=new AlertDialog.Builder(MainActivity.this);
4 alert.setTitle("提示");
5 alert.setMessage("普通对话框");
6 alert.setPositiveButton("确定", null);
7 alert.show();
8
9 2.自定义对话框:
10
11 LayoutInflater layout=LayoutInflater.from(MainActivity.this);//设置布局文件的过滤是从MainActivity中进行的
12 View view=layout.inflate(R.layout.custmer, null);//建一个View 对象用来存放自定义的布局文件,这里的R.layout.custmer就是自定义的文件布局
13 Builder customer=new AlertDialog.Builder(MainActivity.this);
14 customer.setView(view);//通过此方法可以将自定义的布局加载到对话框中
15 customer.setTitle("提示");
16 customer.setMessage("自定义对话框");
17 customer.setPositiveButton("确定", null).create();
18 customer.show();
19
20 3.类似单选按钮形式的对话框:
21
22 Builder customer=new AlertDialog.Builder(MainActivity.this);
23 customer.setTitle("提示");
24 customer.setMessage("自定义对话框");
25 customer.setSingleChoiceItems(new String[]{"a","b","c"}, 0, new OnClickListener(){
26 public void onClick(DialogInterface arg0, int arg1) {
27 Toast.makeText(MainActivity.this, "你选择了"+arg1+"项", Toast.LENGTH_LONG).show();
28 }
29
30 });
31 customer.setPositiveButton("确定", null).create();
32 customer.show();
33
34
35 4.多选类型的对话框
36 Builder customer=new AlertDialog.Builder(MainActivity.this);
37 customer.setTitle("提示");
38 customer.setMessage("自定义对话框");
39 设置为多选对话框,前面的是选项,会面对应的是,是否处于选中状态
40 customer.setMultiChoiceItems(new String[]{"a","b","c","d"}, new boolean[]{true,false,false,false}, new OnMultiChoiceClickListener(){
41 public void onClick(DialogInterface arg0, int arg1, boolean arg2)
42 {
43 Toast.makeText(MainActivity.this, "你选择了"+arg1+"项", Toast.LENGTH_LONG).show();
44 }
45
46 });
47 customer.show();
48
49
50 5.进度对话框:
51 1)。ProgressDialog progress=ProgressDialog.show(MainActivity.this, "安装进度", "正在安装");
52 第二个参数是:提示的标题;第三个参数是:messge信息
53
54 2)。也可以这样写:
55 ProgressDialog dialog=new ProgressDialog(MainActivity.this);
56 dialog.setButton2("确定", new OnClickListener()){
57
58 @Override
59 public void onClick(DialogInterface arg0, int arg1) {
60 // TODO Auto-generated method stub
61
62 }
63
64 });
65 dialog.setTitle("安装进度");
66 dialog.setMessage("正在安装");
67 dialog.setIndeterminate(false);//设置进度为不明确类型的
68 dialog.setSecondaryProgress(progress);//设置进度值的二次进度
69 dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL)//设置为水平进队条
70 dialog.show();
71
72 6.时间选择对话框:
73 calendar=Calendar.getInstance();
74 year=calendar.get(Calendar.YEAR);
75 month=calendar.get(Calendar.MONTH);
76 day=calendar.get(Calendar.DAY_OF_MONTH);
77 minute=calendar.get(Calendar.MINUTE);
78 hour=calendar.get(Calendar.HOUR);
79 new TimePickerDialog(this, new OnTimeSetListener(){
80
81 @Override
82 public void onTimeSet(TimePicker arg0, int arg1, int arg2) {
83 calendar.set(year,month,day,hour,minute);
84 }}, hour, minute, false).show();
85
86
87 7.日期选择对话框:
88 new DatePickerDialog(this,new OnDateSetListener(){
89 public void onDateSet(DatePicker arg0, int arg1, int arg2,
90 int arg3) {
91 calendar.set(year,month,day);
92 }
93
94 },year,month,day).show();