progressDialog之圆形和长条形
progressDialog有圆形和长条形俩种类型,上面可以显示text文本,可以显示Button,通过Button控制让这个progressDialog消失。
长条形可以设置一个进行控制其显示时间,圆形不行,只能通过Button来控制其消失。
圆形progressDialog简单示例:
main.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"
    android:text="@string/hello"
    />
<Button
    android:id="@+id/button01"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="点我显示进度条"></Button>
  
</LinearLayout>

java端代码如下:,见proTest工程
//有一个按钮,点击这个按钮,显示一个圆形对话框进度条,点击进度条上面的按钮后,进度条消失
package com.pro;

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 ProTest1 extends Activity implements OnClickListener {//由于要对Button监听,以实现是否要让progressDialog消失
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button b = (Button)findViewById(R.id.button01);
        b.setOnClickListener(this);
    }
     
@Override
   public void onClick(View v) {
 //设置一个圆形对话框进度条,点击进度条上面的按钮后,进度条消失
   ProgressDialog pd = new ProgressDialog(this);
   pd.setButton("确定", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int i) {
     dialog.cancel();
    }
   });
   pd.show();
     }
    
}

长形progressDialog:
main.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"
    android:text="@string/hello"
    />
    <Button
    android:id="@+id/button01"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="点我显示进度条"></Button>
</LinearLayout>

java代码如下:
//设置一个长条形对话框进度条,设置每次显示进度,最大值,显示时间,直至进度条消失
package com.dialog;

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 proTest1 extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button b = (Button)findViewById(R.id.button01);
        b.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
     // TODO Auto-generated method stub
     ProgressDialog pd = new ProgressDialog(this);
     pd.setMax(9);//进度条显示的最大值依赖于max
     pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
     pd.show();
     //在此启动一个线程
     Task t =new Task(pd);
     t.start();
    }
private class Task extends Thread{
   //通过右键Run来添加run方法
//为了避免死循环,定义一个控制变量,使得程序不会死掉
   boolean run =true;
   ProgressDialog pd;
   Task(ProgressDialog p){
    pd =p;
   }
@Override
public void run() {
 while(run){
 try {
  
  pd.setProgress(pd.getProgress()+1);
  if(pd.getProgress()>=9){
   pd.cancel();
   run=false;
  }
  Thread.sleep(500);
 } catch (InterruptedException e) {
  // TODO Auto-generated catch block
  run=false;//产生异常的时候,让他停止
 }


 }
}
}

   
}

posted on 2010-04-30 12:41  snowdrop  阅读(8002)  评论(0编辑  收藏  举报