android最简单的ProgressBar以及ProgressDialog使用

说到ProgressDialog和ProgressBar,相信大家不陌生吧,就是比如导入联系人的时候会出现一个进度条的控件,或者下载的时候的进度条。

首先说一下ProgressBar:

既然要更新UI,在android中,一般是不在主线程也就是Activity所在的那个线程来做些费时的操作的,因为这样会阻塞主线程,导致出现ANR错误。因此,一般是新开一个线程来做费时的操作,当然,做操作的时候又要通知前端的主线程来更新界面,这样能让用户知道在做操作啊,因此,就需要使用android中的线程通信Handler了。好了。下面是示例:

1,写好一个带progressbar的xml布局文件:

<?xml version="1.0" encoding="utf-8"?>    
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
         android:orientation="vertical"   
         android:layout_width="fill_parent"    
         android:layout_height="fill_parent">    
           
         <TextView   
             android:layout_width="fill_parent"    
             android:layout_height="wrap_content"   
             />    
         <ProgressBar   
             android:id="@+id/rectangleProgressBar"    
             style="?android:attr/progressBarStyleHorizontal" mce_style="?android:attr/progressBarStyleHorizontal"     
             android:layout_width="fill_parent"   
             android:layout_height="wrap_content"  
             android:visibility="gone"  
             />  
         <ProgressBar   
             android:id="@+id/circleProgressBar"    
             style="?android:attr/progressBarStyleLarge" mce_style="?android:attr/progressBarStyleLarge"  
             android:layout_width="wrap_content"   
             android:layout_height="wrap_content"  
             android:visibility="gone"   
             />  
            
         <Button android:id="@+id/button"   
                 android:text="Show ProgressBar"    
                 android:layout_width="wrap_content"   
                 android:layout_height="wrap_content"  
                 />  
     </LinearLayout>

2,Activity代码:

public class ProgressBarDemo extends Activity {  
      
    private ProgressBar rectangleProgressBar,circleProgressBar;  
    private Button mButton;  
      
    protected static final int STOP = 0x10000;  
    protected static final int NEXT = 0x10001;  
    private int iCount = 0;  
      
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        //findView By id  
        rectangleProgressBar = (ProgressBar)findViewById(R.id.rectangleProgressBar);  
        circleProgressBar = (ProgressBar)findViewById(R.id.circleProgressBar);  
        mButton = (Button)findViewById(R.id.button);  
          
        rectangleProgressBar.setIndeterminate(false);  
        circleProgressBar.setIndeterminate(false);  
          
        mButton.setOnClickListener(new Button.OnClickListener() {  
              
            public void onClick(View v) {  
                  
                rectangleProgressBar.setVisibility(View.VISIBLE);  
                circleProgressBar.setVisibility(View.VISIBLE);  
                  
                rectangleProgressBar.setMax(100);  
                rectangleProgressBar.setProgress(0);  
                circleProgressBar.setProgress(0);  
                  
                //创建一个线程,每秒步长为5增加,到100%时停止  
                Thread mThread = new Thread(new Runnable() {  
                      
                    public void run() {  
                          
                        for(int i=0 ; i < 20; i++){  
                            try{  
                                iCount = (i + 1) * 5;  
                                Thread.sleep(1000);  
                                if(i == 19){  
                                    Message msg = new Message();  
                                    msg.what = STOP;  
                                    mHandler.sendMessage(msg);  
                                    break;  
                                }else{  
                                    Message msg = new Message();  
                                    msg.what = NEXT;  
                                    mHandler.sendMessage(msg);  
                                }  
                            }catch (Exception e) {  
                                e.printStackTrace();  
                            }  
                        }  
                          
                    }  
                });  
                mThread.start();  
            }  
        });  
    }  
      
    //定义一个Handler  
    private Handler mHandler = new Handler(){  
        public void handleMessage(Message msg){  
            switch (msg.what) {  
            case STOP:  
                rectangleProgressBar.setVisibility(View.GONE);  
                circleProgressBar.setVisibility(View.GONE);  
                Thread.currentThread().interrupt();  
                break;  
            case NEXT:  
                if(!Thread.currentThread().isInterrupted()){  
                    rectangleProgressBar.setProgress(iCount);  
                    circleProgressBar.setProgress(iCount);  
                }  
                break;  
            }  
        }  
    };  
}

当然,这里涉及两种progressbar,一种是进度条的,一种是环形的,另外这里的线程停止方式,还可以有另外的方式,就是设置一个boolean变量来控制线程的停止。

再说一下ProgressDialog的用法;示例代码:

1,也是先看布局:

<?xml version="1.0" encoding="utf-8"?>  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:orientation="vertical"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        >  
    <TextView android:id="@+id/status"  
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content"   
        android:text="@string/hello"  
        />  
    <Button android:id="@+id/beginBtn"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="begin"  
        />  
    </LinearLayout>

2,再看Activity代码:

public class ProgressBarDemo extends Activity {  
      
    private TextView statusTextView;  
    private Button beginBtn;  
    private ProgressDialog progressDialog;  
      
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        statusTextView = (TextView)findViewById(R.id.status);  
        beginBtn = (Button)findViewById(R.id.beginBtn);  
        setListener();  
    }  
      
    /** 
     * 用Handler来更新UI 
     */  
    private Handler handler = new Handler(){  
  
        @Override  
        public void handleMessage(Message msg) {  
              
            //关闭ProgressDialog  
            progressDialog.dismiss();  
              
            //更新UI  
            statusTextView.setText("Completed!");  
        }};  
      
          
    /** 
     * 点击按钮事件listener 
     */  
    private void setListener(){  
        beginBtn.setOnClickListener(new View.OnClickListener() {  
              
            @Override  
            public void onClick(View v) {  
                  
                //显示ProgressDialog  
                progressDialog = ProgressDialog.show(ProgressBarDemo.this, "Loading...", "Please wait...", true, false);  
                  
                //新建线程  
                new Thread(){  
  
                    @Override  
                    public void run() {  
                        //需要花时间计算的方法  
                        Calculation.calculate(4);  
                          
                        //向handler发消息  
                        handler.sendEmptyMessage(0);  
                    }}.start();  
            }  
        });  
    }

class Calculation {  
      
    public static void calculate(int sleepSeconds){  
        try {  
            Thread.sleep(sleepSeconds * 1000);  
        } catch (Exception e) {  
            // TODO: handle exception  
        }  
    }  
  
}    
      
}

很简单吧,主要使用的android中线程之间的通信机制Handler。

相关推荐