1 @Override
2 public void onClick(View v) {
3
4
5 switch (v.getId()) {
6 case R.id.d1:
7
8 AlertDialog.Builder builder=new AlertDialog.Builder(this);
9 builder.setTitle("这是标题...");
10 builder.setMessage("这就是一个确认取消对话框...");
11 builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
12
13 @Override
14 public void onClick(DialogInterface dialog, int which) {
15 Toast.makeText(MainActivity.this, "你点击了确认按钮...", 0).show();
16 }
17 });
18 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
19
20 @Override
21 public void onClick(DialogInterface dialog, int which) {
22 Toast.makeText(MainActivity.this, "你点击了取消按钮...", 0).show();
23 }
24 });
25 builder.show();
26 break;
27 case R.id.d2:
28 AlertDialog.Builder builder1=new AlertDialog.Builder(this);
29 builder1.setTitle("这是标题...");
30 final String[] strings=new String[]{"小红","小明"};
31 builder1.setSingleChoiceItems(strings, -1, new DialogInterface.OnClickListener() {
32
33 @Override
34 public void onClick(DialogInterface dialog, int which) {
35 Toast.makeText(MainActivity.this, "你选择了:"+strings[which], 0).show();
36
37 }
38 });
39
40 builder1.show();
41 break;
42 case R.id.d3:
43 AlertDialog.Builder builder11=new AlertDialog.Builder(this);
44 builder11.setTitle("这是标题...");
45 final String[] strings1=new String[]{"小红","小明"};
46 boolean[] b=new boolean[]{false,true};
47 builder11.setMultiChoiceItems(strings1, b, new DialogInterface.OnMultiChoiceClickListener() {
48
49 @Override
50 public void onClick(DialogInterface dialog, int which, boolean isChecked) {
51
52 Toast.makeText(MainActivity.this, "你选择了:"+strings1[which]+" : "+isChecked, 0).show();
53 }
54 });
55 builder11.show();
56 break;
57
58 //进度对话框
59 case R.id.d4:
60
61 // ProgressDialog pDialog=new ProgressDialog(this);
62 // pDialog.setTitle("这是一个进度框...");
63 // pDialog.setMax(100);
64 // pDialog.show();
65
66 //这是比较简陋的方法,下面的比较全面合理
67 final ProgressDialog proDialog = android.app.ProgressDialog.show(MainActivity.this, "测试", "2秒后自动消失!");
68 Thread thread = new Thread()
69 {
70 public void run()
71 {
72 try{
73 sleep(2000);
74 } catch (InterruptedException e) {
75 // TODO 自动生成的 catch 块
76 e.printStackTrace();
77 }
78 proDialog.dismiss();//万万不可少这句,否则会程序会卡死。
79 }
80 };
81 thread.start();
82 break;
83 case R.id.d5:
84
85
86 xh_count = 0;
87
88 // 创建ProgressDialog对象
89 final ProgressDialog xh_pDialog = new ProgressDialog(this);
90
91 // 设置进度条风格,风格为矩形
92 xh_pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
93
94 // 设置ProgressDialog 标题
95 xh_pDialog.setTitle("提示");
96
97 // 设置ProgressDialog提示信息
98 xh_pDialog.setMessage("这是一个长形进度条对话框");
99
100 // 设置ProgressDialog标题图标
101 // xh_pDialog.setIcon(R.drawable.img2);
102
103 // 设置ProgressDialog 的进度条是否不明确 false 就是不设置为不明确
104 xh_pDialog.setIndeterminate(false);
105
106 // 设置ProgressDialog 进度条进度
107 xh_pDialog.setProgress(200);
108 xh_pDialog.setMax(200);
109
110 //下面两种都可以解决触摸其他地方消失的问题
111 xh_pDialog.setCancelable(false); // 设置ProgressDialog 是否可以按退回键取消
112 // xh_pDialog.setCanceledOnTouchOutside(false);//这种不会阻止返回键
113
114
115 // 设置ProgressDialog 的一个Button 可以用来取消对话框
116 xh_pDialog.setButton("取消", new DialogInterface.OnClickListener() {
117
118 @Override
119 public void onClick(DialogInterface dialog, int which) {
120 // 点击“确定”按钮取消对话框
121 // dialog.cancel();
122 xh_pDialog.dismiss();
123 }
124 });
125 // 让ProgressDialog显示
126 xh_pDialog.show();
127
128 new Thread() {
129 @Override
130 public void run() {
131 try {
132 while (xh_count <= 200) {
133 // 由线程来控制进度
134 xh_pDialog.setProgress(xh_count++);
135 Thread.sleep(100);
136 }
137 xh_pDialog.dismiss();
138 } catch (Exception e) {
139 xh_pDialog.cancel();
140 }
141 }
142 }.start();
143
144
145 break;
146
147
148 }
149 }
安卓的几种alert对话框
最新推荐文章于 2023-06-18 09:45:00 发布