[转载]ProgressDialog用法_android开发_新浪博客

[转载]ProgressDialog用法_android开发_新浪博客.

ProgressDialog用法  

1.新建Android项目 ProgressDialog    在包andorid.huada下建文件ProgressDialogActivity.java

2.ProgressDialogActivity.java代码如下



 package android.huada;



import android.app.Activity;

import android.app.ProgressDialog;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

public class ProgressDialogActivity extends Activity {

    

 

 ProgressDialog progressDialog;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        

        progress("下载数据中", "请稍等......");

  

      

      

    }

    

    public void progress(String title,String message){

     progressDialog = new ProgressDialog(this);

     updateThread thread= new updateThread();

     progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

     progressDialog.setMessage(message);

     progressDialog.setTitle(title);

     progressDialog.setProgress(0);

     progressDialog.setMax(100);

     progressDialog.show();

     thread.start();

        }

    

    Handler handler = new Handler(){

  @Override

  public void handleMessage(Message msg) {

   // TODO Auto-generated method stub

   if(msg.what>=100){

    progressDialog.cancel();

       }

    progressDialog.setProgress(msg.what);

   super.handleMessage(msg);

  }

     

    };

    

    class updateThread extends Thread{

     public void run(){

      handler.sendEmptyMessage(0);

      try {

    updateThread.sleep(1000);

    handler.sendEmptyMessage(10);

       updateThread.sleep(1000);

       handler.sendEmptyMessage(20);

       updateThread.sleep(1000);

       handler.sendEmptyMessage(30);

       updateThread.sleep(1000);

       handler.sendEmptyMessage(40);

       updateThread.sleep(1000);

       handler.sendEmptyMessage(50);

       updateThread.sleep(1000);

       handler.sendEmptyMessage(60);

       updateThread.sleep(1000);

       handler.sendEmptyMessage(70);

       updateThread.sleep(1000);

       handler.sendEmptyMessage(80);

       updateThread.sleep(1000);

       handler.sendEmptyMessage(100);

       updateThread.sleep(1000);

       

   } catch (InterruptedException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

   }

      

     }

    }; 

}

3.配置文件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"

    />

</LinearLayout>
赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏