Android学习笔记(九)——更复杂的进度对话框

显示操作进度的对话框


1、使用上一篇创建的同一项目,在activity_main.xml文件中添加一个Button:

    

2、在MainActivity.jaAAAAAA加具体的进度条实现代码:

首先添加onClick3()方法:

public void onClick3(View v) {
		showDialog(1);// id为1,在调用回调方法onCreateDialog()时,将id传进去,使其选择case 1情况。
		progressDialog.setProgress(0);// 从0开始

		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				for (int i = 1; i <= 10; i++) {
					try {
						Thread.sleep(1000);
						progressDialog.incrementProgressBy(100 / 10);// 步进为10
					} catch (InterruptedException e) {
						// TODO: handle exception
						e.printStackTrace();
					}
				}
				progressDialog.dismiss();// 销毁对话框
			}
		}).start();
	}
再在onCreateDialog()回调方法中添加id=1时的代码(即实现显示操作进度对话框的代码):

case 1:
			progressDialog = new ProgressDialog(this);
			progressDialog.setIcon(R.drawable.ic_launcher);
			progressDialog.setTitle("Downloading files...");
			progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 设置进度条样式
			progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",// 设置OK按钮
					new DialogInterface.AAAlickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							Toast.makeText(getBaseContext(), "OK clicked!",
									Toast.LENGTH_SHORT).show();
						}
					});
			progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",// 设置取消按钮
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							Toast.makeText(getBaseContext(), "Cancel clicked!",
									Toast.LENGTH_SHORT).show();
						}
					});
			return progressDialog;
AAAAAAA运行一下,效果如下:

下载完整代码~

http://www.bkjia.com/Androidjc/767365.htmlwww.bkjia.comtruehttp://www.bkjia.com/Androidjc/767365.htmlTechArticle显示操作进度的对话框 1、使用上一篇创建的同一项目,在activity_main.xml文件中添加一个Button: 2、在MainActivity.java文件中添加具体的进度条...

本文源自: AG平台官网

上一篇:研发Android Framework所需的基础工具和C++基础知识

下一篇:没有了