• android ProgressDialog 对话框


      1 /**
      2  * Description:
      3  * <br/>site: <a href="http://www.crazyit.org">crazyit.org</a> 
      4  * <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
      5  * <br/>This program is protected by copyright laws.
      6  * <br/>Program Name:
      7  * <br/>Date:
      8  * @author  Yeeku.H.Lee kongyeeku@163.com
      9  * @version  1.0
     10  */
     11 public class ProgressDialogTest extends Activity
     12 {
     13     // 该程序模拟填充长度为100的数组
     14     private int[] data = new int[100];
     15     int hasData = 0;
     16     // 定义进度对话框的标识
     17     final int PROGRESS_DIALOG = 0x112;
     18     // 记录进度对话框的完成百分比
     19     int progressStatus = 0;
     20     ProgressDialog pd;
     21     // 定义一个负责更新的进度的Handler
     22     Handler handler;
     23 
     24     @Override
     25     public void onCreate(Bundle savedInstanceState)
     26     {
     27         super.onCreate(savedInstanceState);
     28         setContentView(R.layout.main);
     29         Button execBn = (Button) findViewById(R.id.exec);
     30         execBn.setOnClickListener(new OnClickListener()
     31         {
     32             public void onClick(View source)
     33             {
     34                 showDialog(PROGRESS_DIALOG);
     35             }
     36         });
     37         handler = new Handler()
     38         {
     39             @Override
     40             public void handleMessage(Message msg)
     41             {
     42                 // 表明消息是由该程序发送的。
     43                 if (msg.what == 0x111)
     44                 {
     45                     pd.setProgress(progressStatus);
     46                 }
     47             }
     48         };
     49     }
     50 
     51     @Override
     52     public Dialog onCreateDialog(int id, Bundle status)
     53     {
     54         System.out.println("------create------");
     55         switch (id)
     56         {
     57             case PROGRESS_DIALOG:
     58                 // 创建进度对话框
     59                 pd = new ProgressDialog(this);
     60                 pd.setMax(100);
     61                 // 设置对话框的标题
     62                 pd.setTitle("任务完成百分比");
     63                 // 设置对话框 显示的内容
     64                 pd.setMessage("耗时任务的完成百分比");
     65                 // 设置对话框不能用“取消”按钮关闭
     66                 pd.setCancelable(false);
     67                 // 设置对话框的进度条风格
     68                 pd.setProgressStyle(ProgressDialog.STYLE_SPINNER); //圆形进度条
     69 //                pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//水平进度条
     70                 // 设置对话框的进度条是否显示进度
     71                 pd.setIndeterminate(false);
     72                 break;
     73         }
     74         return pd;
     75     }
     76 
     77     // 该方法将在onCreateDialog方法调用之后被回调
     78     @Override
     79     public void onPrepareDialog(int id, Dialog dialog)
     80     {
     81         System.out.println("------prepare------");
     82         super.onPrepareDialog(id, dialog);
     83         switch (id)
     84         {
     85             case PROGRESS_DIALOG:
     86                 // 对话框进度清零
     87                 pd.incrementProgressBy(-pd.getProgress());
     88                 new Thread()
     89                 {
     90                     public void run()
     91                     {
     92                         while (progressStatus < 100)
     93                         {
     94                             // 获取耗时操作的完成百分比
     95                             progressStatus = doWork();
     96                             // 发送消息到Handler
     97                             Message m = new Message();
     98                             m.what = 0x111;
     99                             // 发送消息
    100                             handler.sendMessage(m);
    101                         }
    102 
    103                         // 如果任务已经完成
    104                         if (progressStatus >= 100)
    105                         {
    106                             // 关闭对话框
    107                             pd.dismiss();
    108                         }
    109                     }
    110                 }.start();
    111                 break;
    112         }
    113     }
    114 
    115     // 模拟一个耗时的操作。
    116     public int doWork()
    117     {
    118         // 为数组元素赋值
    119         data[hasData++] = (int) (Math.random() * 100);
    120         try
    121         {
    122             Thread.sleep(100);
    123         }
    124         catch (InterruptedException e)
    125         {
    126             e.printStackTrace();
    127         }
    128         return hasData;
    129     }
    130 }
  • 相关阅读:
    对 Service中sqlsession对象的优化
    mybatis 接口绑定 和 动态SQL
    添加指定的日志信息
    数据库优化
    MyBatis 实现新增
    MyBatis中log4j 和 参数 和 分页和别名 功能
    OpenGL_混合
    android应用开发揭秘第13章01示例,移动图像,opengl绘制星星问题解决!
    《关于个人承接项目的注意事项》读后感
    Android OpenGL ES教程 第二部分 构建多边形
  • 原文地址:https://www.cnblogs.com/songyao/p/4073072.html
Copyright © 2020-2023  润新知