Android学习笔记(八)——显示进度对话框
2014-05-06 20:44
197 查看
显示进度对话框
我们常常有这样的经历:执行某一应用程序时,需要等待一会,这时会显示一个进度(Please Wait)对话框,让用户知道操作正在进行。
我们继续在上一篇中的程序中添加代码~
1、在上一篇的activity_main.xml文件中添加一个Button,添加后的代码如下:
<LinearLayout 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:orientation="vertical"
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/btn_dialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Click to display a dialog" />
<Button
android:id="@+id/btn_dialog2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClick2"
android:text="Click to display a progress dialog" />
</LinearLayout>
2、在MainActivity.java中添加一个onClick2()方法,添加的代码块如下:
public void onClick2(View v) {
// ---show the dialog---
final ProgressDialog dialog = ProgressDialog.show(this,
"Doing something", "Please wait...", true);//创建一个进度对话框
new Thread(new Runnable() {//使用Runnable代码块创建了一个Thread线程
@Override
public void run() {//run()方法中的代码将在一个单独的线程中执行
// TODO Auto-generated method stub
try {
// ---simulate doing something lengthy---
Thread.sleep(5000);//模拟一个耗时5秒的操作
// ---dismiss the dialog---
dialog.dismiss();//5秒钟后,调用dismiss方法关闭进度对话框
} catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}).start();
}
3、运行,点击第二个按钮,效果如下:
5秒后,进度条自动消失,程序恢复原来的状态~
点击下载完整代码~
我们常常有这样的经历:执行某一应用程序时,需要等待一会,这时会显示一个进度(Please Wait)对话框,让用户知道操作正在进行。
我们继续在上一篇中的程序中添加代码~
1、在上一篇的activity_main.xml文件中添加一个Button,添加后的代码如下:
<LinearLayout 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:orientation="vertical"
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/btn_dialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Click to display a dialog" />
<Button
android:id="@+id/btn_dialog2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClick2"
android:text="Click to display a progress dialog" />
</LinearLayout>
2、在MainActivity.java中添加一个onClick2()方法,添加的代码块如下:
public void onClick2(View v) {
// ---show the dialog---
final ProgressDialog dialog = ProgressDialog.show(this,
"Doing something", "Please wait...", true);//创建一个进度对话框
new Thread(new Runnable() {//使用Runnable代码块创建了一个Thread线程
@Override
public void run() {//run()方法中的代码将在一个单独的线程中执行
// TODO Auto-generated method stub
try {
// ---simulate doing something lengthy---
Thread.sleep(5000);//模拟一个耗时5秒的操作
// ---dismiss the dialog---
dialog.dismiss();//5秒钟后,调用dismiss方法关闭进度对话框
} catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}).start();
}
3、运行,点击第二个按钮,效果如下:
5秒后,进度条自动消失,程序恢复原来的状态~
点击下载完整代码~
相关文章推荐
- Android学习笔记(八)——显示运行进度对话框
- Android学习笔记(九)——更复杂的进度对话框
- Android网络加载时显示进度条对话框
- 项目总结(三)——复制文件时显示进度对话框
- 【Android 开发教程】显示进度对话框
- Android进步之路一:显示“正在加载,请稍候.......”进度对话框示例代码以及具体细节讲解
- 文件下载之使用AsyncTask并在进度对话框中显示下载进度
- IFileOperation实现复制文件时显示进度对话框
- Android开发之显示进度对话框
- 利用VTK显示文件读取进度模态对话框
- 网页浏览控件WebView和显示进度的对话框ProgressDialog
- C# 打印,不显示打印进度对话框
- 多线程:C#.NET中使用BackgroundWorker在模态对话框中显示进度条
- Android学习笔记(九)——更复杂的进度对话框
- IFileOperation实现复制文件时显示进度对话框
- Android学习笔记(七)——显示对话框窗口
- C#.NET中使用BackgroundWorker在模态对话框中显示进度条
- 多线程:C#.NET中使用BackgroundWorker在模态对话框中显示进度条
- 在WebView中加载HTML页面时显示进度对话框的方法
- Android -- 对话框 Dialog: 确定取消,单选多选,进度显示, 常用控件示例:单选多选框,下拉列表,进度条,自动完成文本框, 菜单显示