进度条对话框ProgressDialog

进度条对话框ProgressDialog经常用于不能马上完成的操作,为了避免用户莫名其妙的等待,给用户一个提示。

  本例中我们演示了两种进度条:条形进度条和圆形进度条。

一、设计界面

  1、打开“res/layout/activity_main.xml”文件。

  从工具栏向activity拖出2个按钮Button


2、打开activity_main.xml文件。

  代码如下:

<span style="font-size:14px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/progress" />

    <Button
        android:id="@+id/circle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/circle" />

</LinearLayout></span>

二、长按事件 

  打开“src/com.genwoxue.progress/MainActivity.java”文件。

  然后输入以下代码:  

<span style="font-size:14px;">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 MainActivity extends Activity {
	//声明按钮
	private Button btnCircle = null;
	private Button btnProgress = null;
	//声明进度条对话框
	private ProgressDialog pdDialog = null;
	//进度计数
	int iCount = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//获取按钮对象
		btnCircle = (Button) findViewById(R.id.circle);
		btnProgress = (Button) findViewById(R.id.progress);
		//设置btnCircle的事件监听
		btnCircle.setOnClickListener(new OnClickListener() {
			
			@SuppressWarnings("deprecation")
			@Override
			public void onClick(View v) {
				iCount = 0;
				//创建ProgressDialog对象
				pdDialog = new ProgressDialog(MainActivity.this);
				//设置进度条风格,风格为圆形,旋转的
				pdDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
				//设置ProgressDialog标题
				pdDialog.setTitle("圆形进度条");
				//设置ProgressDialog提示信息
				pdDialog.setMessage("正在下载中……");
				//设置ProgressDialog标题图标
				pdDialog.setIcon(R.drawable.ic_launcher);
				//设置ProgressDialog进度条进度
				pdDialog.setProgress(100);
				//设置ProgressDialog的进度条是否不明确
				pdDialog.setIndeterminate(false);
				//设置ProgressDialog是否可以按退回按键取消
				pdDialog.setCancelable(true);
				//设置ProgressDialog的一个Button
				pdDialog.setButton("取消", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int i) {
						//点击“取消”按钮取消对话框
						dialog.cancel();
					}
				});
				//让ProgressDialog显示
				pdDialog.show();
				//创建线程实例
				new Thread(){
					public void run(){
						try{
							while(iCount<=100){
								//由线程来控制进度
								pdDialog.setProgress(iCount++);
								Thread.sleep(50);
							}
							pdDialog.cancel();
						}catch(InterruptedException e){
							pdDialog.cancel();
						}
					}
				}.start();
			}
		});
		//设置btnProgress的事件监听
		btnProgress.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				iCount = 0;
				//创建ProgressDialog对象
				pdDialog = new ProgressDialog(MainActivity.this);
				//设置进度条风格,风格为长形
				pdDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
				//设置ProgressDialog标题
				pdDialog.setTitle("条形进度条");
				//设置ProgressDialog提示信息
				pdDialog.setMessage("正在下载中……");
				//设置ProgressDialog标题图标
				pdDialog.setIcon(R.drawable.ic_launcher);
				//设置ProgressDialog进度条进度
				pdDialog.setProgress(100);
				//设置ProgressDialog的进度条是否不明确
				pdDialog.setIndeterminate(false);
				//设置ProgressDialog是否可以按退回按键取消
				pdDialog.setCancelable(true);
				//让ProgressDialog显示
				pdDialog.show();
				//创建线程实例
				new Thread(){
					public void run(){
						try{
							while(iCount<=100){
								//由线程来控制进度
								pdDialog.setProgress(iCount++);
								Thread.sleep(50);
							}
							pdDialog.cancel();
						}catch(InterruptedException e){
							pdDialog.cancel();
						}
					}
				}.start();
			}
		});
	}

}</span>

三、运行效果


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值