免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1737 | 回复: 0
打印 上一主题 下一主题

[Android] Android 对话框中的进度条 ProgressDialog [复制链接]

论坛徽章:
59
2015七夕节徽章
日期:2015-08-24 11:17:25ChinaUnix专家徽章
日期:2015-07-20 09:19:30每周论坛发贴之星
日期:2015-07-20 09:19:42ChinaUnix元老
日期:2015-07-20 11:04:38荣誉版主
日期:2015-07-20 11:05:19巳蛇
日期:2015-07-20 11:05:26CU十二周年纪念徽章
日期:2015-07-20 11:05:27IT运维版块每日发帖之星
日期:2015-07-20 11:05:34操作系统版块每日发帖之星
日期:2015-07-20 11:05:36程序设计版块每日发帖之星
日期:2015-07-20 11:05:40数据库技术版块每日发帖之星
日期:2015-07-20 11:05:432015年辞旧岁徽章
日期:2015-07-20 11:05:44
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-28 13:03 |只看该作者 |倒序浏览
显然要定义对话框进度条就要用ProgressDialog,首先我们需要创建ProgressDialog对象,当然这里同样使用了线程来控制进度条显示,另外可以使用以下方法来设置ProgressDialog。
  setProgressStyle:设置进度条风格,风格为圆形,旋转的。
  setTitlt:设置ProgressDialog 标题
  setMessage:设置ProgressDialog提示信息;
  setIcon:设置ProgressDialog标题图标;
  setIndeterminate:设置ProgressDialog 的进度条是否不明确;
  setCancelable:设置ProgressDialog 是否可以按返回键取消;
  setButton:设置ProgressDialog 的一个Button(需要监听Button事件);
  show:显示ProgressDialog。

  首先还是让我们看看运行效果吧





   下面先来看看布局文件

<LINEARLAYOUT xmlns:android="http://schemas.android.com/apk/res/android"  
    androidrientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TEXTVIEW  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<BUTTON
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="圆形进度条"/>

<BUTTON
android:id="@+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="长形进度条"/>


Activity01
Java代码  

  • package xiaohang.zhimeng;  
  •   
  • import android.app.Activity;  
  • import android.app.ProgressDialog;  
  • import android.content.DialogInterface;  
  • import android.os.Bundle;  
  • import android.view.View;  
  • import android.view.View.OnClickListener;  
  • import android.widget.Button;  
  •   
  • public class Activity01 extends Activity {  
  •   
  •     private Button xhButton01, xhButton02;  
  •   
  •     int xh_count = 0;  
  •     // 声明进度条对话框  
  •     ProgressDialog xh_pDialog;  
  •   
  •     @Override  
  •     public void onCreate(Bundle savedInstanceState) {  
  •         super.onCreate(savedInstanceState);  
  •         setContentView(R.layout.main);  
  •   
  •         // 得到按钮对象  
  •         xhButton01 = (Button) findViewById(R.id.Button01);  
  •         xhButton02 = (Button) findViewById(R.id.Button02);  
  •   
  •         // 设置xhButton01的事件监听  
  •         xhButton01.setOnClickListener(new OnClickListener() {  
  •             @Override  
  •             public void onClick(View v) {  
  •                 // 创建ProgressDialog对象  
  •                 xh_pDialog = new ProgressDialog(Activity01.this);  
  •   
  •                 // 设置进度条风格,风格为圆形,旋转的  
  •                 xh_pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);  
  •   
  •                 // 设置ProgressDialog 标题  
  •                 xh_pDialog.setTitle("提示";  
  •   
  •                 // 设置ProgressDialog提示信息  
  •                 xh_pDialog.setMessage("这是一个圆形进度条对话框";  
  •   
  •                 // 设置ProgressDialog标题图标  
  •                 xh_pDialog.setIcon(R.drawable.img1);  
  •   
  •                 // 设置ProgressDialog 的进度条是否不明确 false 就是不设置为不明确  
  •                 xh_pDialog.setIndeterminate(false);  
  •   
  •                 // 设置ProgressDialog 是否可以按退回键取消  
  •                 xh_pDialog.setCancelable(true);  
  •   
  •                 // 设置ProgressDialog 的一个Button  
  •                 xh_pDialog.setButton("确定", new Bt1DialogListener());  
  •   
  •                 // 让ProgressDialog显示  
  •                 xh_pDialog.show();  
  •             }  
  •   
  •         });  
  •   
  •         // 设置xhButton02的事件监听  
  •         xhButton02.setOnClickListener(new Button.OnClickListener() {  
  •   
  •             @Override  
  •             public void onClick(View v) {  
  •   
  •                 xh_count = 0;  
  •   
  •                 // 创建ProgressDialog对象  
  •                 xh_pDialog = new ProgressDialog(Activity01.this);  
  •   
  •                 // 设置进度条风格,风格为圆形,旋转的  
  •                 xh_pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  •   
  •                 // 设置ProgressDialog 标题  
  •                 xh_pDialog.setTitle("提示";  
  •   
  •                 // 设置ProgressDialog提示信息  
  •                 xh_pDialog.setMessage("这是一个长形进度条对话框";  
  •   
  •                 // 设置ProgressDialog标题图标  
  •                 xh_pDialog.setIcon(R.drawable.img2);  
  •   
  •                 // 设置ProgressDialog 的进度条是否不明确 false 就是不设置为不明确  
  •                 xh_pDialog.setIndeterminate(false);  
  •   
  •                 // 设置ProgressDialog 进度条进度  
  •                 xh_pDialog.setProgress(100);  
  •   
  •                 // 设置ProgressDialog 是否可以按退回键取消  
  •                 xh_pDialog.setCancelable(true);  
  •   
  •                 // 让ProgressDialog显示  
  •                 xh_pDialog.show();  
  •   
  •                 new Thread() {  
  •                     @Override  
  •                     public void run() {  
  •                         try {  
  •                             while (xh_count <= 100) {  
  •                                 // 由线程来控制进度  
  •                                 xh_pDialog.setProgress(xh_count++);  
  •                                 Thread.sleep(100);  
  •                             }  
  •                             xh_pDialog.cancel();  
  •                         } catch (Exception e) {  
  •                             xh_pDialog.cancel();  
  •                         }  
  •                     }  
  •                 }.start();  
  •   
  •             }  
  •   
  •         });  
  •     }  
  •   
  •     // xhButton01的监听器类  
  •     class Bt1DialogListener implements DialogInterface.OnClickListener {  
  •         @Override  
  •         public void onClick(DialogInterface dialog, int which) {  
  •             // 点击“确定”按钮取消对话框  
  •             dialog.cancel();  
  •         }  
  •     }  
  • }  


android 2.0  
源码 运行效果图片 见附件
CU博客本地下载见下面:49adb25a-e911-30f3-b302-324cf2c5a3aa.rar
http://blog.csdn.net/kaiyang45/article/details/6997131
http://byandby.iteye.com/blog/817214
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP