博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Android学习专题】控件组件篇:Dialog汇总
阅读量:4958 次
发布时间:2019-06-12

本文共 18271 字,大约阅读时间需要 60 分钟。

【Android学习专题】控件组件篇:Dialog汇总

   SkySeraph Feb 22nd 2012  SZTCL

Email:    QQ:452728574

-------------------------------------------------------------------------------------------------------------------------------------------------------------

一、界面效果

运行界面

-------------------------------------------------------------------------------------------------------------------------------------------------------------

部分效果

自定义登录对话框

圆形(转圈)进度条

长形进度条

多选按钮对话框

单选按钮对话框

带多个按钮的提示对话框

带确定取消按钮的提示对话框

-------------------------------------------------------------------------------------------------------------------------------------------------------------

二、知识点

1 AlertDialog.Builder属性
* setTitle: 为对话框设置标题 ;
* setIcon : 为对话框设置图标;
* setMessage: 为对话框设置内容;
* setView :  给对话框设置自定义样式 ;
* setItems: 设置对话框要显示的一个list,一般用于显示几个命令时;
* setMultiChoiceItems:用来设置对话框显示一系列的复选框;
* setNeutralButton : 响应中立行为的点击;
* setPositiveButton : 响应Yes/Ok的点击 ;
* setNegativeButton :响应No/Cancel的点击 ;
* create : 创建对话框 ;
* show : 显示对话框;
2 ProgressDialog属性
*setProgressStyle:   设置进度条风格,风格为圆形,旋转的;
*setTitlt:        设置ProgressDialog 标题;
*setMessage:        设置ProgressDialog提示信息;
*setIcon:       设置ProgressDialog标题图标;
*setIndeterminate:    设置ProgressDialog 的进度条是否不明确;
*setCancelable:         设置ProgressDialog 是否可以按返回键取消;
*setButton:              设置ProgressDialog 的一个Button(需要监听Button事件);
*show:                     显示ProgressDialog。

-------------------------------------------------------------------------------------------------------------------------------------------------------------

三、源码

1 布局文件:dialog_demo.xml

View Code
1 
2
6 7
16
17 18
23 24
30 31
37 38
44 45
51 52
58 59
65 66
72 73
79 80
86 87
93 94
95 96

2 java代码:dialog_demo.java

View Code
1 public class dialog_demo extends Activity   2 {
3 private static final int MAX_PROGRESS = 100; //进度条最大数 4 private ProgressDialog mProgressDialog = null; //进度条 5 final String[] m_Items = {"Frist","Second","Third"}; 6 int mSingleChoiceID = -1; //记录单选中的ID 7 ArrayList
MultiChoiceID = new ArrayList
();//记录多选选中的id号 8 9 // 10 @Override 11 protected void onCreate(Bundle savedInstanceState) 12 {
13 // TODO Auto-generated method stub 14 super.onCreate(savedInstanceState); 15 setContentView(R.layout.dialog_demo); 16 findViews(); 17 } 18 19 // 20 private void findViews() 21 { 22 // // 23 /* 【简单提示对话框】 */ 24 Button btn1 = (Button) findViewById(R.id.dialg_demo_btn01); 25 btn1.setOnClickListener(new OnClickListener() 26 {
27 public void onClick(View v) 28 {
29 // TODO Auto-generated method stub 30 new AlertDialog.Builder(dialog_demo.this).setTitle("简单提示对话框").setMessage("这是提示信息") 31 .show(); 32 return; 33 } 34 }); 35 // // 36 /* 【带确定取消按钮的提示对话框】 */ 37 Button btn2 = (Button) findViewById(R.id.dialg_demo_btn02); 38 btn2.setOnClickListener(new OnClickListener() 39 {
40 public void onClick(View v) 41 {
42 // TODO Auto-generated method stub 43 AlertDialog.Builder dialog02 = new AlertDialog.Builder(dialog_demo.this); 44 dialog02.setTitle("带确定取消按钮的提示对话框"); 45 dialog02.setIcon(R.drawable.qq); 46 dialog02.setMessage("这是提示内容"); 47 dialog02.setPositiveButton("确定", new DialogInterface.OnClickListener() 48 {
49 public void onClick(DialogInterface dialoginterface, int which) 50 {
51 Toast.makeText(dialog_demo.this, "你选择了确定", Toast.LENGTH_LONG).show(); 52 } 53 }); 54 dialog02.setNegativeButton("取消", new DialogInterface.OnClickListener() 55 {
56 public void onClick(DialogInterface dialoginterface, int which) 57 {
58 Toast.makeText(dialog_demo.this, "你选择了取消", Toast.LENGTH_LONG).show(); 59 } 60 }); 61 dialog02.create().show(); 62 return; 63 } 64 }); 65 // // 66 /* 【带多个按钮的提示对话框】 */ 67 Button btn3 = (Button) findViewById(R.id.dialg_demo_btn03); 68 btn3.setOnClickListener(new OnClickListener() 69 {
70 public void onClick(View v) 71 {
72 // TODO Auto-generated method stub 73 AlertDialog.Builder dialog03 = new AlertDialog.Builder(dialog_demo.this); 74 dialog03.setIcon(R.drawable.img1); 75 dialog03.setTitle("带多个按钮的提示对话框"); 76 dialog03.setMessage("你最喜欢的球类运动是什么呢?"); 77 dialog03.setPositiveButton("篮球", new DialogInterface.OnClickListener() 78 {
79 public void onClick(DialogInterface dialoginterface, int which) 80 {
81 showDialog("篮球很不错"); 82 } 83 }); 84 dialog03.setNeutralButton("乒乓球", new DialogInterface.OnClickListener() 85 {
86 public void onClick(DialogInterface dialoginterface, int which) 87 {
88 showDialog("乒乓球很不错"); 89 } 90 }); 91 dialog03.setNegativeButton("足球", new DialogInterface.OnClickListener() 92 {
93 public void onClick(DialogInterface dialoginterface, int which) 94 {
95 showDialog("足球很不错"); 96 } 97 }); 98 dialog03.create().show(); 99 return; 100 } 101 }); 102 // // 103 /*【单选按钮对话框】*/ 104 Button btn4 = (Button) findViewById(R.id.dialg_demo_btn04); 105 btn4.setOnClickListener(new OnClickListener() 106 {
107 public void onClick(View v) 108 {
109 // TODO Auto-generated method stub 110 mSingleChoiceID = -1; 111 AlertDialog.Builder dialog04 = new AlertDialog.Builder(dialog_demo.this); 112 dialog04.setTitle("单选按妞"); 113 dialog04.setSingleChoiceItems(m_Items, 0, new DialogInterface.OnClickListener() 114 {
115 public void onClick(DialogInterface dialog, int whichButton) 116 {
117 mSingleChoiceID = whichButton; 118 showDialog("你选择的id为" + whichButton + " , " + m_Items[whichButton]); 119 } 120 }); 121 dialog04.setPositiveButton("确定", new DialogInterface.OnClickListener() 122 {
123 public void onClick(DialogInterface dialog, int whichButton) 124 {
125 if (mSingleChoiceID > 0) 126 {
127 showDialog("你选择的是" + mSingleChoiceID); 128 } 129 } 130 }); 131 dialog04.setNegativeButton("取消", new DialogInterface.OnClickListener() 132 {
133 public void onClick(DialogInterface dialog, int whichButton) 134 {
135 136 } 137 }); 138 dialog04.create().show(); 139 return; 140 } 141 }); 142 // // 143 /*【多选按钮对话框】*/ 144 Button btn5 = (Button) findViewById(R.id.dialg_demo_btn05); 145 btn5.setOnClickListener(new OnClickListener() 146 {
147 public void onClick(View v) 148 {
149 // TODO Auto-generated method stub 150 MultiChoiceID.clear(); 151 AlertDialog.Builder dialog05 = new AlertDialog.Builder(dialog_demo.this); 152 dialog05.setTitle("多选按钮"); 153 dialog05.setMultiChoiceItems(m_Items, new boolean[] 154 { false, false, false}, 155 new DialogInterface.OnMultiChoiceClickListener() 156 {
157 public void onClick(DialogInterface dialog, int whichButton, 158 boolean isChecked) 159 {
160 if (isChecked) 161 {
162 MultiChoiceID.add(whichButton); 163 showDialog("你选择的id为" + whichButton + " , " 164 + m_Items[whichButton]); 165 } else 166 {
167 MultiChoiceID.remove(whichButton); 168 } 169 170 } 171 }); 172 dialog05.create().show(); 173 return; 174 } 175 }); 176 // // 177 /*【列表框对话框】*/ 178 Button btn6 = (Button) findViewById(R.id.dialg_demo_btn06); 179 btn6.setOnClickListener(new OnClickListener() 180 {
181 public void onClick(View v) 182 {
183 // TODO Auto-generated method stub 184 AlertDialog.Builder dialog06 = new AlertDialog.Builder(dialog_demo.this); 185 dialog06.setTitle("列表框"); 186 dialog06.setItems(m_Items, new DialogInterface.OnClickListener() 187 {
188 public void onClick(DialogInterface dialog, int which) 189 {
190 // 点击后弹出窗口选择了第几项 191 showDialog("你选择的id为" + which + " , " + m_Items[which]); 192 } 193 }); 194 dialog06.create().show(); 195 return; 196 } 197 }); 198 // // 199 /*【自定义登录对话框】*/ 200 Button btn7 = (Button) findViewById(R.id.dialg_demo_btn07); 201 btn7.setOnClickListener(new OnClickListener() 202 {
203 public void onClick(View v) 204 {
205 // TODO Auto-generated method stub 206 LayoutInflater factory = LayoutInflater.from(dialog_demo.this); 207 final View view = factory.inflate(R.layout.dialog_demo_login, null);// 获得自定义对话框 208 209 AlertDialog.Builder dialog07 = new AlertDialog.Builder(dialog_demo.this); 210 dialog07.setIcon(R.drawable.qq); 211 dialog07.setTitle("自定义登录对话框"); 212 dialog07.setView(view); 213 dialog07.setPositiveButton("确定", new DialogInterface.OnClickListener() 214 {
215 public void onClick(DialogInterface dialog, int whichButton) 216 {
217 218 EditText userName = (EditText) view 219 .findViewById(R.id.dialog_demo_loginETUserName); 220 EditText password = (EditText) view 221 .findViewById(R.id.dialog_demo_loginETPassWord); 222 showDialog("姓名 :" + userName.getText().toString() + "密码:" 223 + password.getText().toString()); 224 } 225 }); 226 dialog07.setNegativeButton("取消", new DialogInterface.OnClickListener() 227 {
228 public void onClick(DialogInterface dialog, int whichButton) 229 {
230 //Toast.makeText(dialog_demo.this, "你选择了取消", Toast.LENGTH_LONG).show(); 231 showDialog("你选择了取消"); 232 } 233 }); 234 dialog07.create().show(); 235 return; 236 } 237 }); 238 // // 239 Button btn8 = (Button) findViewById(R.id.dialg_demo_btn08); 240 btn8.setOnClickListener(new OnClickListener() 241 {
242 public void onClick(View v) 243 {
244 // TODO Auto-generated method stub 245 mProgressDialog = new ProgressDialog(dialog_demo.this);//创建ProgressDialog对象 246 mProgressDialog.setIcon(R.drawable.qq);// 设置ProgressDialog标题 图标 247 mProgressDialog.setTitle("进度条窗口");// 设置ProgressDialog标题 248 mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置进度条风格,风格为长形 249 mProgressDialog.setMax(MAX_PROGRESS);// 设置ProgressDialo进度条进度 250 mProgressDialog.setButton("确定", new DialogInterface.OnClickListener() 251 {
252 public void onClick(DialogInterface dialog, int whichButton) 253 {
254 // 这里添加点击后的逻辑 255 } 256 }); 257 mProgressDialog.setButton2("取消", new DialogInterface.OnClickListener() 258 {
259 public void onClick(DialogInterface dialog, int whichButton) 260 {
261 // 这里添加点击后的逻辑 262 } 263 }); 264 mProgressDialog.show(); 265 new Thread() 266 {
267 @Override 268 public void run() 269 {
270 int Progress = 0; 271 while (Progress < MAX_PROGRESS) 272 {
273 try 274 { 275 mProgressDialog.setProgress(Progress++); 276 //mProgressDialog.incrementProgressBy(1); 277 Thread.sleep(100); 278 } catch (Exception e) 279 {
280 // TODO Auto-generated catch block 281 mProgressDialog.cancel(); 282 //e.printStackTrace(); 283 } 284 } 285 }; 286 }.start(); 287 return; 288 } 289 }); 290 // // 291 /*【圆形(转圈)进度条】*/ 292 Button btn9 = (Button) findViewById(R.id.dialg_demo_btn09); 293 btn9.setOnClickListener(new OnClickListener() 294 {
295 public void onClick(View v) 296 {
297 // TODO Auto-generated method stub 298 mProgressDialog = new ProgressDialog(dialog_demo.this);//创建ProgressDialog对象 299 mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); //设置进度条风格,风格为圆形,旋转的 300 mProgressDialog.setTitle("读取ing...");// 设置ProgressDialog标题 301 mProgressDialog.setMessage("正在读取中请稍候...");// 设置ProgressDialog提示信息 302 mProgressDialog.setIndeterminate(true);//设置ProgressDialog 的进度条不明确 303 mProgressDialog.setCancelable(true);// 设置ProgressDialog 可以按退回键取消 304 mProgressDialog.setButton("确定", new DialogInterface.OnClickListener() 305 {
306 public void onClick(DialogInterface dialog, int whichButton) 307 {
308 // 这里添加点击后的逻辑 309 } 310 }); 311 mProgressDialog.show();// 让ProgressDialog显示 312 return; 313 } 314 }); 315 // // 316 /*【带补充对话框】*/ 317 Button btn10 = (Button) findViewById(R.id.dialg_demo_btn10); 318 btn10.setOnClickListener(new OnClickListener() 319 {
320 public void onClick(View v) 321 {
322 // TODO Auto-generated method stub 323 return; 324 } 325 }); 326 // // 327 } 328 329 // 330 /*显示子函数*/ 331 private void showDialog(String str) 332 {
333 new AlertDialog.Builder(dialog_demo.this).setMessage(str).show(); 334 // Toast.makeText(dialog_demo.this, str, Toast.LENGTH_LONG).show(); 335 } 336 // 337 }

3 自定义登录对话框:dialog_demo_login.xml

View Code
1 
2
7 8
14
15 16
22
23 24
30
31 32
38
39 40

-------------------------------------------------------------------------------------------------------------------------------------------------------------

四、Refs

【Android】对话框 AlertDialog :http://blog.csdn.net/feng88724/article/details/6171450

Android UI学习 - 对话框 (AlertDialog & ProgressDialog) http://android.blog.51cto.com/268543/333769 

Android软件开发之盘点所有Dialog对话框大合集(一) :http://blog.csdn.net/xys289187120/article/details/6601613

Android 对话框(Dialog)大全 建立你自己的对话框 :http://www.cnblogs.com/salam/archive/2010/11/15/1877512.html

-------------------------------------------------------------------------------------------------------------------------------------------------------------

 

 

 

转载于:https://www.cnblogs.com/skyseraph/archive/2012/02/22/2362989.html

你可能感兴趣的文章
vuex中的dispatch和commit
查看>>
mybatis实战教程二:多对一关联查询(一对多)
查看>>
NodeMCU文档中文翻译 3 构建固件
查看>>
前端学习☞jquery
查看>>
10分钟搞懂树状数组
查看>>
Spring Cloud与微服务构建:微服务简介
查看>>
HTTP缓存和CDN缓存
查看>>
HDU-1171 Big Event in HDU(生成函数/背包dp)
查看>>
Babel 是干什么的
查看>>
cocos2dx-3.0(8)------Label、LabelTTF、LabelAtlas、LabelBMFont使用之法
查看>>
Mysql数据库乱码总结
查看>>
BZOJ.3160.万径人踪灭(FFT Manacher)
查看>>
CODE[VS] 1842 递归第一次
查看>>
20180418小测
查看>>
Spring Cloud是怎么运行的?
查看>>
12 联结表
查看>>
数字三角形
查看>>
NGUI 减少drawcall规则
查看>>
xss攻击
查看>>
开发环境中快速部署Oracle Essbase(Rapid deployment of oracle essbase in development envrioments)...
查看>>