posts - 93,  comments - 2,  trackbacks - 0
showDialog()调用createDialog()和onPrepareDialog(),其中createDialog()调用onCreateDialog()。
例子如下所示
  1package com.zhihe.xqsh.test;
  2
  3import android.app.Activity;
  4import android.app.Dialog;
  5import android.app.ProgressDialog;
  6import android.os.Bundle;
  7import android.os.Handler;
  8import android.os.Message;
  9import android.util.Log;
 10import android.view.View;
 11import android.view.View.OnClickListener;
 12import android.widget.Button;
 13import android.widget.TextView;
 14
 15import com.zhihe.xqsh.activity.R;
 16
 17public class Sample2_19_Activity extends Activity {
 18
 19    final int PROGRESS_DIALOG = 0;
 20    final int INCREASE = 0;
 21    final int MAX_COUNTER = 100;
 22    ProgressDialog pd;
 23    Handler hd;
 24    private TextView tv_network;
 25    private Button bt_dialog;
 26   
 27    @Override
 28    public void onCreate(Bundle savedInstanceState)
 29    {
 30        super.onCreate(savedInstanceState);
 31        setContentView(R.layout.network);
 32       
 33        tv_network = (TextView) this.findViewById(R.id.tv_network);
 34        bt_dialog = (Button)this.findViewById(R.id.bt_dialog);
 35        bt_dialog.setOnClickListener(
 36            new OnClickListener(){
 37                @Override  
 38                public void onClick(View v){
 39                    showDialog(PROGRESS_DIALOG);                   
 40                }

 41            }

 42        ); 
 43        hd = new Handler(){
 44            @Override
 45            public void handleMessage(Message msg)  //必须重写此方法,用于接收数据
 46            {
 47                super.handleMessage(msg);               
 48                switch(msg.what)
 49                {
 50                   case INCREASE:
 51                     pd.incrementProgressBy(1);//进度每次加1
 52                        if(pd.getProgress() >= MAX_COUNTER){
 53                            pd.dismiss();                                
 54                        }
                      
 55                   break;
 56                }

 57            }

 58        }
;
 59    }

 60   
 61    @Override
 62    public Dialog onCreateDialog(int id){
 63        switch(id){
 64              case  PROGRESS_DIALOG:
 65                  pd = new ProgressDialog(this);
 66                  pd.setMax(MAX_COUNTER);//设置最大值
 67                  pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
 68                  pd.setTitle("hello");//设置标题
 69                  pd.setCancelable(false);//设置进度对话框不能用回退按钮关闭    
 70                  Log.d("kkk""kkk");
 71              break;
 72        }
       
 73        return pd;
 74    }
  
 75   
 76    //每次弹出对话框时被回调以动态更新对话框内容的方法
 77    @Override
 78    public void onPrepareDialog(int id, Dialog dialog){
 79        super.onPrepareDialog(id, dialog);
 80               switch(id){
 81               case  PROGRESS_DIALOG:
 82                   pd.incrementProgressBy(-pd.getProgress());//对话框进度清零
 83                   new Thread(){
 84                       public void run(){
 85                           while(true){
 86                               hd.sendEmptyMessage(INCREASE);
 87                               if(pd.getProgress() >= MAX_COUNTER){
 88                                   break;
 89                               }

 90                               try{
 91                                   Thread.sleep(40);
 92                               }
catch(Exception e){
 93                                   e.printStackTrace();
 94                               }

 95                           }

 96                       }
                
 97                   }
.start();
 98                   break;
 99        }

100    }

101}

102

posted on 2015-02-03 08:55 Terry Zou 阅读(288) 评论(0)  编辑  收藏 所属分类: Android

只有注册用户登录后才能发表评论。


网站导航:
 
<2015年2月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
1234567

常用链接

留言簿(2)

随笔分类

随笔档案

文章分类

文章档案

相册

收藏夹

Java

搜索

  •  

最新随笔

最新评论

阅读排行榜

评论排行榜