ProgressDialog的基本用法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/niohandsome/article/details/53354530


ProgressDialog为进度对话框。android手机自带的对话框显得比较单一,我们可以通过ProgressDialog来自己定义对话框中将要显示出什么东西。

首先看看progressDialog里面的方法

  setProgressStyle:设置进度条风格,风格为圆形,旋转的。
  setTitlt:设置标题
  setMessage:设置提示信息;
  setIcon:设置标题图标;
  setIndeterminate:设置ProgressDialog 的进度条是否不明确;这个属性对于ProgressDailog默认的转轮模式没有实际意义,默认下设置为true,它仅仅对带有ProgressBar的Dialog有作用。修改这个属性为false后可以实时更新进度条的进度。
  setCancelable:设置ProgressDialog 是否可以按返回键取消;

  CancelListner:当前Dialog强制取消之后将会被执行,通常用来清理未完成的任务。
  setButton:设置ProgressDialog 的一个Button(需要监听Button事件);
  show:显示ProgressDialog。

  cancel:删除progressdialog

  dismiss: 删除progressdialog 作用和cancel相同

  setProgress(intCounter);更新进度条,当然一般都需要Handler的结合来更新进度条

然后我们看看效果

 

 

  实现代码如下

Java代码  收藏代码
  1. "">package cn.android;  
  2. import android.app.Activity;  
  3. import android.app.ProgressDialog;  
  4. import android.content.DialogInterface;  
  5. import android.content.Intent;  
  6. import android.os.Bundle;  
  7. import android.os.Handler;  
  8. import android.os.Message;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.Button;  
  12. import android.widget.Toast;  
  13.   
  14. public class SecondActivity extends Activity implements Runnable{  
  15.       
  16.     private Button b_dialog,b_dialog1,button;//两个按钮  
  17.     private ProgressDialog pd,pd1;//进度条对话框  
  18.     private int count;  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.second);//关联对应的界面  
  22.          b_dialog = (Button)this.findViewById(R.id.button_dialog);      
  23.          b_dialog1 = (Button)this.findViewById(R.id.Button_dialog1);      
  24.         //处理事件发生时要做的事  
  25.          b_dialog.setOnClickListener(listener);  
  26.          b_dialog1.setOnClickListener(listener);         
  27.   
  28. "">    }  
  29.     //定义监听器对象  
  30.     private OnClickListener listener = new OnClickListener() {  
  31.         // 鼠标按下后  
  32.         public void onClick(View v) {  
  33.           // 得到当前被触发的事件的ID —— 类型是int  
  34.           int id = v.getId();  
  35.           if(id == R.id.button_dialog){  
  36. //按下确定键就会消失的进程对话框                 
  37. //             pd = new ProgressDialog(SecondActivity.this);// 创建ProgressDialog对象     
  38. //             pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置进度条风格,风格为圆形,旋转的    
  39. //             pd.setTitle("提示");// 设置ProgressDialog 标题     
  40. //             pd.setMessage("这是一个圆形进度条对话框");// 设置ProgressDialog提示信息         
  41. //             pd.setIcon(R.drawable.icon);// 设置ProgressDialog标题图标         
  42. //             // 设置ProgressDialog 的进度条是否不明确 false 就是不设置为不明确     
  43. //             pd.setIndeterminate(false);                   
  44. //             pd.setCancelable(true); // 设置ProgressDialog 是否可以按退回键取消                          
  45. //             pd.setButton("确定", new Bt1DialogListener()); // 设置ProgressDialog 的一个Button     
  46. //             pd.show(); // 让ProgressDialog显示      
  47.                   
  48.  //过1秒钟就会自己消失的进程对话框  
  49.             //弹出另外一种对话框  
  50.             pd = ProgressDialog.show(SecondActivity.this"自动关闭对话框""Working,,,,,,1秒"truefalse);  
  51.             Thread thread = new Thread(SecondActivity.this);//开启一个线程来延时               
  52.                 thread.start();//启动线程  
  53.             }else if(id == R.id.Button_dialog1){  
  54.                 pd1 = new ProgressDialog(SecondActivity.this);// 创建ProgressDialog对象     
  55.                 pd1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 设置进度条风格,风格为圆形,旋转的             
  56.                 pd1.setTitle("提示");// 设置ProgressDialog 标题     
  57.                 pd1.setMessage("这是一个条状进度条对话框");// 设置ProgressDialog提示信息         
  58.                 pd1.setIcon(R.drawable.secondback);// 设置ProgressDialog标题图标         
  59.                 // 设置ProgressDialog 的进度条是否不明确 false 就是不设置为不明确     
  60.                 pd1.setIndeterminate(false);                   
  61.                 pd1.setCancelable(true); // 设置ProgressDialog 是否可以按退回键取消              
  62.                 pd1.setProgress(100);// 设置ProgressDialog 进度条进度      
  63.                 pd1.show(); // 让ProgressDialog显示    
  64.                 count = 0;                                   
  65.                 new Thread() {       
  66.                          public void run() {     
  67.                             try {     
  68.                                while(count <= 100) {     
  69.                                // 由线程来控制进度     
  70.                                 pd1.setProgress(count++);     
  71.                                 Thread.sleep(100);     
  72.                              }     
  73.                                  pd1.cancel();     
  74.                              } catch (Exception e) {     
  75.                                  pd1.cancel();     
  76.                              }     
  77.                           }     
  78.                       }.start();     
  79.     }  
  80.         }  
  81.     };  
  82.     //run的是实现  
  83.     public void run() {  
  84.         try {  
  85.             Thread.sleep(1000);//睡1秒  
  86.         } catch (InterruptedException e) {  
  87.             e.printStackTrace();  
  88.         }  
  89.         handler.sendEmptyMessage(0);//传送消息  
  90.     }  
  91.     //定义处理消息的对象  
  92.     private Handler handler = new Handler(){  
  93.         //加入传消息来了就这么么办  
  94.         public void handleMessage(Message msg){  
  95.             pd.dismiss();//对话框消失  
  96.             Toast.makeText(SecondActivity.this"对话框就消失了"3).show();     
  97.         }  
  98.     };  
  99.   
  100.     // pon01的监听器类     
  101.     class Bt1DialogListener implements DialogInterface.OnClickListener {     
  102.         @Override    
  103.         public void onClick(DialogInterface dialog, int which) {     
  104.             // 点击“确定”按钮取消对话框     
  105.             dialog.cancel();     
  106.         }     
  107.     }     
  108.       
  109. }  
  110.  

ProgressDialog用法

05-04

我在ListView加载数前想显示ProgressDialog,但是窗体加载数据前,ProgressDialog一直未能出现,只有等到数据全部加完才显示出来,不知道为什么,请高人指教,谢谢!rnrnpackage gather.ui;rnrnimport java.util.LinkedList;rnimport java.util.List;rnrnimport com.chinahr.gather.data.CompanyDataProvider;rnimport com.chinahr.gather.data.HtmlHelper;rnimport com.chinahr.gather.entity.CompanyMessageEntity;rnimport com.chinahr.gather.ui.MyListView.OnRefreshListener;rnrnimport android.app.Activity;rnimport android.app.ProgressDialog;rnimport android.os.AsyncTask;rnimport android.os.Bundle;rnimport android.os.Handler;rnimport android.os.Message;rnimport android.view.LayoutInflater;rnimport android.view.View;rnimport android.view.ViewGroup;rnimport android.widget.ArrayAdapter;rnimport android.widget.BaseAdapter;rnimport android.widget.ImageView;rnimport android.widget.TextView;rnrnrnrnpublic class PaginationTestActivity extends Activity rnrn private LinkedList data;rn private BaseAdapter adapter;rn private final static String REQUEST_URL = "http://10.155.5.44:10001/GetNewCompanyMessage.aspx";rn private final int MSG_UPDATE = 1;rn private final int MSG_DISMISS = 0;rn private MyListView listView;rn public ProgressDialog progressDialog = null;rnrn public void onCreate(Bundle savedInstanceState) rn super.onCreate(savedInstanceState);rn setContentView(R.layout.main);rn rn progressDialog = new ProgressDialog(PaginationTestActivity.this);rn progressDialog.setMessage("正在加载");rn progressDialog.show();rn rn data = new LinkedList();rn rn try rn List listMsg = new CompanyDataProvider(REQUEST_URL).getCompanyInfos();rn for(CompanyMessageEntity message:listMsg)rn data.add(message);rn rn catch (Exception e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn rn rn rnrn listView = (MyListView) findViewById(R.id.listView);rn rn mhandler.post(mThread);rn rn listView.setonRefreshListener(new OnRefreshListener() rn public void onRefresh() rn new AsyncTask() rn protected Void doInBackground(Void... params) rn try rn Thread.sleep(1000);rn catch (Exception e) rn e.printStackTrace();rn rn // 这里获取最新的数据 rn return null;rn rnrn @Overridern protected void onPostExecute(Void result) rn adapter.notifyDataSetChanged();rn listView.onRefreshComplete();rn rnrn .execute(null);rn rn );rn rn rn rnrn private Runnable mThread = new Runnable()rnrn @Overridern public void run() rn int count = 0;rn try rn List listMsg = new CompanyDataProvider(REQUEST_URL).getCompanyInfos();rn for(CompanyMessageEntity message:listMsg)rn data.add(message);rn mhandler.sendEmptyMessage(MSG_UPDATE);rn count++;rn rn if(count == listMsg.size())rn mhandler.sendEmptyMessage(MSG_DISMISS);rn rn catch (Exception e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn rn rn rn rn rn rn ;rn rn private Handler mhandler = new Handler()rn @Overridern public void handleMessage(Message message)rn super.handleMessage(message);rn rn switch (message.what) rn case MSG_UPDATE:rn adapter = new DataAdapert();rn listView.setAdapter(adapter);rn break;rn case 0:rn progressDialog.dismiss();rn break;rn default: rn break;rn rn rn ;rnrn class DataAdapert extends BaseAdapterrn @Overridern public View getView(int position, View convertView, ViewGroup parent) rn convertView=LayoutInflater.from(getApplicationContext()).inflate(R.layout.item, null);rn TextView textView = (TextView) convertView.findViewById(R.id.textView_item);rn textView.setText(data.get(position).getMessageBody());rn rn ImageView imageView_item = (ImageView)findViewById(R.id.imageView_item);rn tryrn imageView_item.setImageBitmap(Utility.getBitmap(data.get(position).getPhotoImg()));rn catch(Exception ex)rn rn rn return convertView;rn rnrn public long getItemId(int position) rn return position;rn rnrn public Object getItem(int position) rn return data.get(position);rn rnrn public int getCount() rn return data.size();rn rn ;rn rnrn

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试