myprogressbar.xml的内容如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/back_qian"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="正在切换状态...."
android:textSize="18sp"
android:textColor="@color/black" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="" />
<ProgressBar
android:id="@+id/progressBar1"
style="@style/ProgressBar_Mini"
android:layout_width="290dp"
android:layout_height="17dp" />
</LinearLayout>
MyProgressBar.java的内容如下:
package com.wl.util;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import com.wl.R;
public class MyProgressBar {
Dialog dialog;
Context context;
// 声明ProgressBar对象
private ProgressBar pro1;
/**
* 构造
*/
public MyProgressBar(Context context) {
// TODO Auto-generated constructor stub
this.context = context;
dialog = new Dialog(context, R.style.dialog);
dialog.setOnCancelListener(onCancelListener);
}
/**
* 初始化进度对话框
*/
public void initDialog() {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.myprogressbar, null);
dialog.setContentView(view);
pro1 = (ProgressBar) dialog.findViewById(R.id.progressBar1);
// 设置进度条是否自动旋转,即设置其不确定模式,false表示不自动旋转
pro1.setIndeterminate(false);
// 设置ProgressBar的最大值
pro1.setMax(100);
// 设置ProgressBar的当前值
pro1.setProgress(0);
dialog.show();
}
public void setProgress(int progressValue) {
pro1.setProgress(progressValue);
}
public void colseDialog() {
dialog.dismiss();
}
public boolean isShowing() {
if (dialog.isShowing()) {
return true;
} else {
return false;
}
}
OnCancelListener onCancelListener = new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
dialog.dismiss();
}
};
}
