Android中ProgressDialog的简单示例

Android中ProgressDialog的使用:http://skycode.iteye.com/blog/346960

Android中ProgressDialog的简单示例:http://lveyo.iteye.com/blog/585771

AndroidProgressDialog的两种用法:http://www.pocketdigi.com/20110510/271.html

在android中,通常我们无法在单独的线程中更新UI,而要在主线程中,这也就是为什么我们要使用Handler了,当handler收到消息中,它会把它放入到队列中等待执行,通常来说这会很快被执行。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textview1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ShowProgressDialog" />

</LinearLayout>
package com.progress.dialog;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ProgressDialogActivity extends Activity {
    /** Called when the activity is first created. */
	private Button button1;
	private TextView textview1;
	private ProgressDialog progressDialog;
	
	private Handler handler = new Handler(){
    	@Override
    	public void handleMessage(android.os.Message msg) {
    		try {
    			if (progressDialog != null) {
					progressDialog.dismiss();
				}
    			textview1.setText("关闭等待框.");
    			
			} catch (Exception e) {
				// TODO: handle exception
				Log.v("ProgressDialogActivity", e.getMessage());
			}
    		
    	};
    };
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        button1 = (Button) findViewById(R.id.button1);
        textview1 = (TextView)findViewById(R.id.textview1);
        
        button1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// 创建并赋值给 progressDialog
				progressDialog = ProgressDialog.show(ProgressDialogActivity.this, "我的标题", "[3秒后关闭]Load......", true, false);
				textview1.setText("显示等待框.");
				//创建线程并启动
				new Thread(new Runnable(){
					@Override
					public void run() {
						try {
							Thread.sleep(3000);
							handler.sendEmptyMessage(0);
							
						} catch (Exception e) {
							e.printStackTrace();
						}
						
					}
				}).start();
				
			}
		});
    }
    
}

ProgressDialog,顾名思义,就是一个进度对话框,常用于显示载入进度、下载进度等。合理使用ProgressDialog能增加用户体验,让用户知道现在程序所处的状态。

下面是两种用法,第一种适合复杂环境,可以自定义风格,添加按钮等,而第二种只能简单的显示一个只有标题和信息的ProgressDialog

package com.pocketdigi.ProgressDialog;
 
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 Main extends Activity {
	/** Called when the activity is first created. */
	ProgressDialog pd1, pd2;
	Button b1, b2;
 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		b1 = (Button) findViewById(R.id.b1);
		b2 = (Button) findViewById(R.id.b2);
		b1.setOnClickListener(showPd1);
		b2.setOnClickListener(showPd2);
	}
	OnClickListener showPd1 = new OnClickListener() {
 
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			pd1 = new ProgressDialog(Main.this);
			pd1.setTitle("PD1标题");
			pd1.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 转圈风格
			// 进度条风格为ProgressDialog.STYLE_HORIZONTAL,使用setMax,setProgress,incrementProgressBy方法设置进度
			pd1.setMessage("PD1信息");
			pd1.setButton("关闭", new DialogInterface.OnClickListener() {
 
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					pd1.dismiss();
				}
 
			});
			//可以使用setButton2,setButton3来添加更多按钮
			pd1.setCancelable(false);//不可被返回键取消对话框
			pd1.show();
		}
 
	};
	OnClickListener showPd2 = new OnClickListener() {
 
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			pd2=ProgressDialog.show(Main.this,"PD2标题","PD2信息");
			//使用该方法将不能再设置按钮,适合显示简单的ProgressDialog
		}
 
	};
 
}

相关推荐