1、ProgressDialog简介
在上一篇文章中,我简单介绍了ProgressBar控件,该控件能在界面上显示进度条,但是却不能以弹出框的形式显示进度条,而ProgressDialog提供了这个功能。ProgressDialog以弹出框的形式显示了进度条。如下图布局:
2、xml布局代码和java代码
该进度条弹出框是使用ProgressDialog类设置的,故在xml布局文件中没有相关代码,但是该进度条弹出框是一般是点击了某个按钮才会显示,如下载按钮等。所以在xml文件中给一个按钮添加了点击事件。xml代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:onClick="showProgressDialog"
android:text="显示进度提示框" />
</RelativeLayout>
java代码如下
package com.example.progressdialog;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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;
}
/**
* 响应按钮单击事件,显示进度条对话框
* @param view
*/
public void showProgressDialog(View view){
// 方式一:
ProgressDialog pd1 = new ProgressDialog(this);
// 设置最大值
pd1.setMax(200);
// 设置当前值
pd1.setProgress(120);
// 设置标题
pd1.setTitle("下载进度");
// 设置信息
pd1.setMessage("正在下载。。。");
// 设置显示样式为进度长条,默认是圆盘进度条
pd1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd1.setIndeterminate(true);
pd1.show();
// 方式二:
ProgressDialog pd2 = ProgressDialog.show(this, "Download", "Download");
}
}