ProgressBar和ProgressDialog(三)

来源:互联网 发布:淘宝我的心愿单在哪 编辑:程序博客网 时间:2024/06/10 00:53
四、ProgressDialog简介
ProgressDialog的本质就是在一对话框中显示ProgressDialog,并另外显示一些关于进度的等文本信息。虽然ProgressDialog继承于AlertDialog,但是并没取消操作按钮,用户只能通过back键来取消和返回。如果想要取消和返回按钮请参照下文关于ProgressDialog的使用实例。
       ProgressDialog支持ProgressDialog.STYLE_HORIZONTALProgressDialog.STYLE_SPINNER,这两种风格。但是当ProgressDialog.STYLE_HORIZONTALindeterminatetrue时,进度的文本信息无法更新,这个似乎是Google的bug.
另外我们可以通过ProgressDialog的静态的show()系列函数快速的创建一个ProgressDialog,但是此时风格只能是STYLE_SPINNER
五、ProgressDialog使用实例
      以下是一个ProgressDialog的使用实例,ProgressDialog其实很简单,但是似乎它的功能也太简单。
程序主界面,截图1

 点击"normal",截图2

 
点击"Custom",将显示一个我自己实现的ProgressBar对话框,截图3

 
点击“ineterminate”,截图4

 
点击"SPINNER",截图5

 
点击“Quick”,截图6

 
代码
ProgressDialogSampleActivity.java文件
package com.teleca.robin;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ProgressDialogSampleActivity extends Activity {
/**
 * menu list item array
 */
protected ArrayList<String> mArrayListItemsdata = new ArrayList<String>();
private ListView mListView = null;

/**
 * menu list adapter
 */
private MyArrayAdapter mAdapter = null;
public static final int PROGRESS_DIALOG_HORIZONTAL_1 = 1000;
public static final int PROGRESS_DIALOG_CUSTOM = 1001;
public static final int PROGRESS_DIALOG_INDETERMINATE_1 = 1002;
public static final int PROGRESS_DIALOG_SPINNER_1 = 1003;
public static final int PROGRESS_DIALOG_QUICK = PROGRESS_DIALOG_SPINNER_1 + 1;
final static String tag = "robin";
/**
 * progress bar sample menu text
 */
private String[] mStrings = { "normal", "Custom", "indeterminate",
"SPINNER", "Quick" };

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
mListView = (ListView) findViewById(R.id.ListView01);
for (int i = 0; i < mStrings.length; i++) {
mArrayListItemsdata.add(mStrings[i]);
}

mAdapter = new MyArrayAdapter(this, R.layout.row_item,
mArrayListItemsdata);
mListView.setAdapter(mAdapter);
/**
 * 
 * Menu selection handler for displaying proper progress bar example.
 */
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
onListItemClick(mListView, position, id);
}
});
}

/**
 * Show progress bar example as per user selection on list
 */
protected void onListItemClick(ListView l, int position, long id) {

switch (position) {
case 0:
showDialog(PROGRESS_DIALOG_HORIZONTAL_1);
break;
case 1:
showDialog(PROGRESS_DIALOG_CUSTOM);
break;
case 2:
showDialog(PROGRESS_DIALOG_INDETERMINATE_1);
break;
case 3:
showDialog(PROGRESS_DIALOG_SPINNER_1);
break;
case 4:
showDialog(PROGRESS_DIALOG_QUICK);
break;
}
}
@Override
protected Dialog onCreateDialog(int id) {

Context context = this;
LayoutInflater factory = LayoutInflater.from(this);
switch (id) {

case PROGRESS_DIALOG_HORIZONTAL_1: {
final ProgressDialog progressDialog = new ProgressDialog(context);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("downloading");
if (id == PROGRESS_DIALOG_INDETERMINATE_1)
progressDialog.setIndeterminate(true);
final Toast toast = Toast.makeText(context,
"downloading is canceled", Toast.LENGTH_LONG);
DialogInterface.OnDismissListener dismissListener = new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub
if (progressDialog.getProgress() < progressDialog.getMax()) {
blCanced = true;
toast.show();
}
}
};
progressDialog.setOnDismissListener(dismissListener);
return progressDialog;
}
                //显示一个我自己实现了ProgressBar的对话框
case PROGRESS_DIALOG_CUSTOM: {

AlertDialog.Builder progressSample1 = new AlertDialog.Builder(
context);

View progressBody = factory.inflate(R.layout.my_progress_dialog,
null);
final TextView bodyTitle = (TextView) progressBody
.findViewById(R.id.progress11bodytext);

final TextView numberText = (TextView) progressBody
.findViewById(R.id.progress11_number);
final ProgressBar progressBar = (ProgressBar) progressBody
.findViewById(R.id.ProgressBar11);
final Button btnCancel = (Button) progressBody
.findViewById(R.id.cancel);
progressBar.setMax(100);
bodyTitle.setText("downloding");
numberText.setText("(" + progressBar.getProgress() + "/"
+ progressBar.getMax() + ")");
progressSample1.setView(progressBody);
progressSample1.setMessage("Please Wating...");
final Toast toast = Toast.makeText(context, bodyTitle.getText()
+ " is canceled", Toast.LENGTH_LONG);
DialogInterface.OnDismissListener dismissListener = new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub
if (progressBar.getProgress() < progressBar.getMax()) {
blCanced = true;
toast.show();
}
}
};
final AlertDialog dialog = progressSample1.create();
OnClickListener cancelOnClickListener = new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
};
btnCancel.setOnClickListener(cancelOnClickListener);
dialog.setOnDismissListener(dismissListener);
return dialog;
}
case PROGRESS_DIALOG_INDETERMINATE_1
/**
 * Indeterminate的ProgressDialog的进度看不到变化,应该是Google的BUG.
 */
{
final ProgressDialog progressDialog = new ProgressDialog(context);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setIndeterminate(true);
progressDialog.setMax(100);
progressDialog.setMessage("downloading");
final Toast toast = Toast.makeText(context,
"downloading is canceled", Toast.LENGTH_LONG);
DialogInterface.OnDismissListener dismissListener = new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub
if (progressDialog.getProgress() < progressDialog.getMax()) {
blCanced = true;
toast.show();
}
}
};
progressDialog.setOnDismissListener(dismissListener);
return progressDialog;
}
case PROGRESS_DIALOG_SPINNER_1:

{
ProgressDialog progressDialog = new ProgressDialog(context);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMessage("connecting");
mProgressStatus = 0;
final Toast toast = Toast.makeText(context,
"connecting is canceled", Toast.LENGTH_LONG);
DialogInterface.OnDismissListener dismissListener = new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub
if (mProgressStatus < 100) {
blCanced = true;
toast.show();
}
}
};
progressDialog.setOnDismissListener(dismissListener);
return progressDialog;
}
case PROGRESS_DIALOG_QUICK: {
final Toast toast = Toast.makeText(context, "loading is canceled",
Toast.LENGTH_LONG);
DialogInterface.OnCancelListener cancelListener = new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
if (mProgressStatus < 100) {
blCanced = true;
toast.show();
}
}
};
ProgressDialog progressDialog = ProgressDialog.show(context,
"Connecting to the Internet", "Please waiting", true,
true, cancelListener);

return progressDialog;
}

}
return null;
}

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
super.onPrepareDialog(id, dialog);
final Context context = this;
Log.i(tag, dialog + "id:" + id);
switch (id) {
case PROGRESS_DIALOG_HORIZONTAL_1: {
final ProgressDialog progressDialog = (ProgressDialog) dialog;
// initializing progress when user re-enter
progressDialog.setProgress(0);
mProgressStatus = 0;
blCanced = false;
final int progressBarMax = progressDialog.getMax();
final Toast toast = Toast.makeText(context,
"downloading is finished", Toast.LENGTH_LONG);
// Start lengthy operation in a background thread
new Thread(new Runnable() {
public void run() {
while (mProgressStatus < progressBarMax) {
doWork();
Log.i(tag, "do working" + mProgressStatus);
// Update the progress bar
handler.post(new Runnable() {
public void run() {
progressDialog.setProgress(mProgressStatus);
if (mProgressStatus >= progressBarMax) {
toast.show();
}
}
});
if (blCanced)
break;
}
}
}).start();
progressDialog.show();
}
break;
//显示一个我自己实现了ProgressBar的对话框
case PROGRESS_DIALOG_CUSTOM: {

final ProgressBar progressBar = (ProgressBar) dialog
.findViewById(R.id.ProgressBar11);
// initializing progress when user re-enter
progressBar.setProgress(0);
mProgressStatus=0;
blCanced = false;
final int progressBarMax = progressBar.getMax();
final TextView numberText = (TextView) dialog
.findViewById(R.id.progress11_number);
final Button btnCancel = (Button) dialog.findViewById(R.id.cancel);
final Toast toast = Toast.makeText(context, "finish",
Toast.LENGTH_LONG);
new Thread(new Runnable() {
public void run() {
while (mProgressStatus < progressBarMax) {
mProgressStatus = doWork();
// Update the progress bar
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(mProgressStatus);
numberText.setText("("
+ progressBar.getProgress() + "/"
+ progressBarMax + ")");
if (mProgressStatus >= progressBarMax) {
btnCancel.setText("back");
toast.show();
}
}
});
if (blCanced)
break;
}
}
}).start();
dialog.show();
}
break;
case PROGRESS_DIALOG_INDETERMINATE_1: {
final ProgressDialog progressDialog = (ProgressDialog) dialog;
// initializing progress when user re-enter
mProgressStatus = 0;
progressDialog.setProgress(mProgressStatus);
blCanced = false;
final int progressBarMax = progressDialog.getMax();
final Toast toast = Toast.makeText(context,
"downloading is finished", Toast.LENGTH_LONG);
// Start lengthy operation in a background thread
new Thread(new Runnable() {
public void run() {
while (mProgressStatus < progressBarMax) {
doWork();
Log.i(tag, "do working" + mProgressStatus);
// Update the progress bar
handler.post(new Runnable() {
public void run() {
Log.i(tag, progressDialog.getProgress()
+ "mProgressStatu:" + mProgressStatus);
progressDialog.setProgress(mProgressStatus);
Log.i(tag, "hi:" + progressDialog.getProgress());
if (mProgressStatus >= progressBarMax) {
toast.show();
progressDialog.dismiss();
}
}
});
if (blCanced)
break;
}
}
}).start();
progressDialog.show();
}
break;
case PROGRESS_DIALOG_SPINNER_1: {
final Toast toast = Toast.makeText(context,
"connected", Toast.LENGTH_LONG);
blCanced = false;
mProgressStatus=0;
final Dialog myDialog=dialog;
new Thread(new Runnable() {
public void run() {
while (mProgressStatus < 100) {
doWork();
if (blCanced)
break;
}
if (!blCanced)
{
toast.show();
handler.post(new Runnable() {
public void run() {
myDialog.dismiss();
}
});
}
}
}).start();
dialog.show();
}
break;
case PROGRESS_DIALOG_QUICK:
blCanced = false;
mProgressStatus=0;
final Dialog myDialog=dialog;
new Thread() {
public void run() {
while (mProgressStatus < 100) {
doWork();
if (blCanced)
break;
}
if (!blCanced)
handler.post(new Runnable() {
public void run() {
myDialog.dismiss();
}
});
}
}.start();
dialog.show();
break;
}
}

boolean blCanced = false;
final Handler handler = new Handler();
private int mProgressStatus = 0;

int doWork() {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mProgressStatus = mProgressStatus + 1;
return mProgressStatus;
}
}
原创粉丝点击