android:ProgressDialog实现

package com.example.progressbar;

import java.io.IOException;

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

    private Button btn;
    private ProgressDialog pg;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		btn=(Button)findViewById(R.id.b1);
		btn.setOnClickListener(new Button.OnClickListener()
		{
			public void onClick(View view)
			{
				final CharSequence Body="Please wait for a moment";
				final CharSequence Title="Progress Bar";
				pg=ProgressDialog.show(MainActivity.this, Title, Body);
				new Thread()
				{
					public void run()
					{
						try
						{
							sleep(3000);
						}
						catch(Exception e)
						{
							e.printStackTrace();
						}
						finally
						{
							pg.dismiss();
						}
					}
				}.start();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

posted @ 2013-01-21 16:30  java程序员-c  阅读(194)  评论(0编辑  收藏  举报