本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。

原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/


当要进行耗时的操作的时候,往往会看见“请稍候”字样的对话框。例如,用户正在登入服务器,此时并不允许用户使用这个软件,或者应用程序把结果返回给用户之前,要进行某些耗时的计算。在这些情况下,显示一个“进度条”对话框,能友好地让用户等待,同时也能阻止用户进行某些不必要的操作。

1. 创建一个工程:Dialog。

2. main.xml中的代码。

[java] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6. <Button
  7. android:id="@+id/btn_dialog2"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:onClick="onClick2"
  11. android:text="Clicktodisplayaprogressdialog"/>
  12. </LinearLayout>
3. DialogActivity.java中的代码。 [java] view plain copy
  1. publicclassDialogActivityextendsActivity{
  2. ProgressDialogprogressDialog;
  3. /**Calledwhentheactivityisfirstcreated.*/
  4. @Override
  5. publicvoidonCreate(BundlesavedInstanceState){
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.main);
  8. }
  9. publicvoidonClick2(Viewv){
  10. //---showthedialog---
  11. finalProgressDialogdialog=ProgressDialog.show(this,
  12. "Doingsomething","Pleasewait...",true);
  13. newThread(newRunnable(){
  14. publicvoidrun(){
  15. try{
  16. //---simulatedoingsomethinglengthy---
  17. Thread.sleep(5000);
  18. //---dismissthedialog---
  19. dialog.dismiss();
  20. }catch(InterruptedExceptione){
  21. e.printStackTrace();
  22. }
  23. }
  24. }).start();
  25. }
  26. }

4. 按F11调试,点击按钮,弹出“进度条”对话框。


基本上,想要创建一个“进度条”对话框,只需要创建一个ProgressDialog类的实例,然后调用show()方法:

[java] view plain copy
  1. //---showthedialog---
  2. finalProgressDialogdialog=ProgressDialog.show(this,
  3. "Doingsomething","Pleasewait...",true);
因为它是一个“模态”的对话框,所以它就会把其他UI组件给遮盖住,直到它被解除。如果想要在后台执行一个“长期运行”的任务,可以创建一个线程。run()方法里面的代码将会在一个独立的线程里面执行。下面的代码使用sleep()方法,模拟了一个需要5秒执行的后台任务: [java] view plain copy
  1. newThread(newRunnable(){
  2. publicvoidrun(){
  3. try{
  4. //---simulatedoingsomethinglengthy---
  5. Thread.sleep(5000);
  6. //---dismissthedialog---
  7. dialog.dismiss();
  8. }catch(InterruptedExceptione){
  9. e.printStackTrace();
  10. }
  11. }
  12. }).start();
5秒钟之后,执行dismiss()方法,对话框就被解除了。

更多相关文章

  1. [置顶] Android通过tcpdump抓包
  2. 【Android】之【对话框(Dialog)大全】
  3. Android初级教程三个Dialog对话框小案例
  4. Android获取可执行应用的列表并执行相关应用
  5. Android的showDialog()、onPrepareDialog()和onCreateDialog()的
  6. TelephonyManager类:Android手机及Sim卡状态的获取
  7. Android用户输入自动提示控件AutoCompleteTextView使用方法
  8. android的两种启动service方式及混合方式
  9. Android中文API (60) ―― DatePicker.OnDateChangedListener

随机推荐

  1. Android开发便签6:数据保存之File存储
  2. androidpn 作为Android推送方案存在的问
  3. MVC浅析(实际上应该是MVP,有时间再更新该博
  4. Android(安卓)Studio ADB响应失败解决方
  5. android通讯录数据库表解析和添加,访问通
  6. 用HTML+JS实现Android闹钟功能,附带Alarm
  7. Android(安卓)Notification基础使用(兼容
  8. android ImageView实现上面圆角下面直角(
  9. Android ListView 分分钟实现Item单选、
  10. Android(安卓)TelephonyManager类的使用