1. 范例说明
常看见Windows窗口程序里或Flash程序的“加载中”对话框,在Android里,则是通过ProgressDialog来运行,这个类封装在Android.app.ProgressDialog里。
该范例将设计一个按钮,在单击按钮之后开始线程的周期,在运行的过程中显示ProgressDialog,最后当线程运行完毕时,选择结束ProgressDialog对话窗口。

2. 运行结果




3. 编写代码
Button.OnClickListener myShowProgressBar = new Button.OnClickListener() {
public void onClick(View arg0) {
final CharSequence strDialogTitle = getString(R.string.str_dialog_title);
final CharSequence strDialogBody = getString(R.string.str_dialog_body);
final CharSequence strDone = getString(R.string.str_dialog_done);
// 显示Progress对话框
myDialog = ProgressDialog.show(MainActivity.this, strDialogTitle,
strDialogBody, false);
myDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mTextView1.setText(strDialogBody);
new Thread() {
public void run() {
try {
/* 在这里写上要后台执行的程序片段 */
/* 为了明显看见效果,以暂停3秒作为示范 */
sleep(3000);
} catch (Exception e) {
e.printStackTrace();
} finally { // 卸除所建立的myDialog对象。
myDialog.dismiss();
}
}
}.start(); /* 开始执行线程 */
}


4. 扩展学习与作业
1.继承关系:
ProgressDialog->AlertDialog->Dialog->Object
2.查看Reference
3.Android 对话框中的进度条ProgressDialog
http://www.eyeandroid.com/thread-9844-1-1.html
作业:
1.写一个有进度更新的ProgressDialog
2.思考:如何在线程执行完毕后更新TextView内容为“正确执行完毕!”
视频讲解: http://www.eyeandroid.com/thread-9847-1-1.html

更多相关文章

  1. Android之EventBus1.0 和EventBus3.0的使用详解
  2. Google Android如何分析和研究Log文件 ,如何看日志信息
  3. android面试(7)-handler机制
  4. 2011android面试题目及其答案大全
  5. iOS开发 OC与java相对应的3DES加解密
  6. 获取SD卡下Android/data/下文件
  7. 转载:Android获取其他包的Context和在任意位置获取应用程序Contex
  8. android 系统权限大全的简介与内容
  9. 在Visual Studio中使用C#开发iOS & Android应用程序

随机推荐

  1. 使用ComponentName启动Activity
  2. android 取设备公网ip ,内网ip
  3. Fragment里面嵌套ViewPager
  4. android辅助圆按钮
  5. Android第二次作业
  6. android studio gradle常用设置
  7. ==(Android)简易计算器==
  8. Android(安卓)-- Notification
  9. unity android获取电量和wifi信号强度
  10. android异步下载图片