Android学习笔记(八)——显示运行进度对话框
2018-04-07 12:10
253 查看
显示运行进度对话框
我们经常有这种经历:运行某一应用程序时。须要等待一会,这时会显示一个进度(Please Wait)对话框,让用户知道操作正在进行。
我们继续在上一篇中的程序中加入代码~
1、在上一篇的activity_main.xml文件里加入一个Button,加入后的代码例如以下:
2、在MainActivity.java中加入一个onClick2()方法。加入的代码块例如以下:
3、运行。点击第二个button。效果例如以下:
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、运行。点击第二个button。效果例如以下:
5秒后,进度条自己主动消失,程序恢复原来的状态~
点击下载完整代码~
相关文章推荐
- Android学习笔记(八)——显示进度对话框
- 1.5 显示进度对话框---ProgressDialog
- Android学习笔记(九)——更复杂的进度对话框
- "当应用程序不是以 UserInteractive 模式运行时显示模式对话框或窗体是无效操作。请指定 ServiceNotification....."的解决方案
- Coreutils Viewer -- 显示linux命令运行进度的工具
- 在VC中调用WORD(显示,修改,存盘,运行宏)之三。调用word的各种对话框翻页等
- MFC多线程对话框显示进度和状态
- 运行时 不显示对话框 只显示 对话框上面的控件
- 多线程:C#.NET中使用BackgroundWorker在模态对话框中显示进度条
- Android学习笔记(九)——更复杂的进度对话框
- 决定是否显示运行对话框、关机功能
- 文件下载之使用AsyncTask并在进度对话框中显示下载进度
- EMF插件安装问题(在About 的Installation中能够看到,且在运行选项的插件列表中也能显示,但是在新建工程对话框中无EMF工程选项)
- 网页浏览控件WebView和显示进度的对话框ProgressDialog
- 当应用程序不是以 UserInteractive 模式运行时显示模式对话框或窗体是无效操作。
- MFC入门学习之控件(2)创建一个对话框窗口并在运行时显示
- Android程序设计:显示后台任务进度对话框
- 显示R程序的运行进度
- 当应用程序不是以UserInteractive 模式运行时显示模式对话框或窗体
- C#.NET中使用BackgroundWorker在模态对话框中显示进度条