Android开发笔记之:Dialog的使用详解

所属分类: 软件编程 / Android 阅读数: 133
收藏 0 赞 0 分享
Dialog是任何系统都必须有的一个控件,作为辅助窗口,用于显示一些消息,或请求用户采取一引起操作等。
在Android中也不例外,基本使用可能参看文档。
使用时的注意事项
1. BACK键能取消掉对话框(dismiss),但是却不会触发其onOkey和onCancel回调接口,所以如果你的对话框会改某些状态,一定要注意还有第三种方式取消对话框。
2. 尽量少用模态对话框(Model dialog),如果Dialog.setCancellable(false),就变成了一个模态对话框,除了程序内部把其Dismiss,否则按什么键都无法将其取消。这是极差的用户体验,对话框本身就是一种干扰,再无法取消会把用户搞疯的。所以除非特别有必要,也即当执行某个操作时不希望被打破,才可以使用模态对话框。
3. 尽量少用对话框,它对用户是一种干扰,除非需要用户做操作,或者做出选择。通常的一般性的通知用Toast或者Notification就足够了。
4. 不要使用对话框风格的Activity,也即把Activity变成一个对话框。因为这样是自已定义的布局,与系统Dialog的风格可能会不一致。最严重的是当系统风格发生变化,Dialog的子类会变化,但Activity式的对话框就不会变化。可以在ICS中找一找Activity对话框,你会发现其OK是在左边,而ICS中系统Dialog的OK都是在右边的。
5. 尽量保证Dialog对象活在Activity的生命周期之内,也即至多是在onCreate()和onDestroy()之间。
6. 要想到和测试到Activity在其Dialog.dismiss()之前死掉的情况。因为Activity必须依附于某个正在显示的Activity实例,当显示和取消的时候其Activity实例必须存在,否则就会有"IllegalArgumentException: View not attached to window manager"。
复制代码 代码如下:

05-15 02:45:26.320: E/AndroidRuntime(1161): java.lang.IllegalArgumentException: View not attached to window manager
05-15 02:45:26.320: E/AndroidRuntime(1161):     at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:355)
05-15 02:45:26.320: E/AndroidRuntime(1161):     at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:200)
05-15 02:45:26.320: E/AndroidRuntime(1161):     at android.view.Window$LocalWindowManager.removeView(Window.java:432)
05-15 02:45:26.320: E/AndroidRuntime(1161):     at android.app.Dialog.dismissDialog(Dialog.java:278)
05-15 02:45:26.320: E/AndroidRuntime(1161):     at android.app.Dialog.access$000(Dialog.java:71)
05-15 02:45:26.320: E/AndroidRuntime(1161):     at android.app.Dialog$1.run(Dialog.java:111)
05-15 02:45:26.320: E/AndroidRuntime(1161):     at android.app.Dialog.dismiss(Dialog.java:268)
05-15 02:45:26.320: E/AndroidRuntime(1161):     at com.hilton.effectiveandroid.app.DialogDemo$1.handleMessage(DialogDemo.java:26)
05-15 02:45:26.320: E/AndroidRuntime(1161):     at android.os.Handler.dispatchMessage(Handler.java:99)

7. Dialog.show()必须在主线程里调用,但Dialog.dismiss()却可以在任何线程中调用。
三种使用方式比较
1. 直接创建一个局部的Dialog对象
优点是变量是局部的容易理解和维护。缺点是Dialog对象难以控制,容易引发RuntimeException。
2. 把Dialog对象变成Activity的域
优点是Dialog对象可以重复利用,且Activity可以控制以保证Dialog不会在Activity生命周期外显示。是推荐的使用方式。
3. 用Activity的方法onCreateDialog(), showDialog()和dismissDialog()
优点是Frameworks会帮忙照看Dialog,在大多数情况下这是推荐的做法。但是对于Activity提前死掉的情况,此方法必有RuntimeException,且无法回避。
实例
复制代码 代码如下:

public class DialogDemo extends Activity {
    private static final int DISMISS_DIALOG = 1;

    private ProgressDialog mBetterDialog;
    private Handler mMainHandler = new Handler() {
 @Override
 public void handleMessage(Message msg) {
     switch (msg.what) {
     case DISMISS_DIALOG:
  Dialog dialog = (Dialog) msg.obj;
  dialog.dismiss();
  break;
     default:
  break;
     }
 }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.dialog_demo);

 final Button sucking = (Button) findViewById(R.id.sucking);
 sucking.setOnClickListener(new View.OnClickListener() {
     public void onClick(View v) {
  final Activity activity = DialogDemo.this;
  final ProgressDialog dialog = new ProgressDialog(activity);
  dialog.setTitle("Worst dialogging");
  dialog.setMessage("This is the worst dialogging scheme, NEVER use it. This dialog is easy to " +
   "run out of its attached activity, yielding WindowManager#BadTokenException if the activity is gone when dismissing");
  dialog.setIndeterminate(true);
  dialog.setCancelable(true);
  // You MUST do the show in main thread anyway
  dialog.show();
  new Thread(new Runnable() {
      public void run() {
   SystemClock.sleep(10000);
   /*
    * IllegalArgumentException: View not attached to window manager
    * If the activity showing the dialog was killed before dismiss() out of rotation or locale changed,
    * the dialog will gone with activity, but when dismiss() yields "IllegalArgumentException: View not attached to
    * window manager".
    * Checking isShowing() won't help.
    * Checking activity.isFinishing() won't help, either.
    * Dismiss it in main thread also won't give any help.
    */
   // THIS WON't WORK
//   if (dialog.isShowing()) {
//       dialog.dismiss();
//   }
//   if (!activity.isFinishing()) {
//       dialog.dismiss();
//   }
   Message msg = Message.obtain();
   msg.what = DISMISS_DIALOG;
   msg.obj = dialog;
   mMainHandler.sendMessage(msg);
      }
  }).start();
     }
 });

 final Button better = (Button) findViewById(R.id.better);
 better.setOnClickListener(new View.OnClickListener() {
     public void onClick(View v) {
  mBetterDialog = new ProgressDialog(DialogDemo.this);
  mBetterDialog.setTitle("Better dialogging");
  mBetterDialog.setMessage("This dialogging can be used. The dialog object is a field of its activity, so activity can" +
    " control it to make sure dialog only lives within activity lifecircle");
  mBetterDialog.setIndeterminate(true);
  mBetterDialog.setCancelable(true);
  // You MUST do the show in main thread anyway
  mBetterDialog.show();
  new Thread(new Runnable() {
      public void run() {
   SystemClock.sleep(10000);
   /*
    * This is much better, mBetterDialog is a field of its activity, so activity can take care of it in order
    * to make sure dialog only live within activity's life circle, to avoid any unexpected exceptions.
    */
   // THIS really works
       if (mBetterDialog != null && mBetterDialog.isShowing()) {
           mBetterDialog.dismiss();
       }
      }
  }).start();
     }
 });

 final Button optional = (Button) findViewById(R.id.optional);
 optional.setOnClickListener(new View.OnClickListener() {
     @SuppressWarnings("deprecation")
     public void onClick(View v) {
  showDialog(0);
  new Thread(new Runnable() {
      public void run() {
   SystemClock.sleep(10000);
   /*
    * This way works best for most of time, except if activity died before dismissing, exception must be
    * thrown: "IllegalArgumentException: View not attached to window manager".
    * Although activity takes care of its belonging dialog, there is no way to operate it manually any more.
    * First you do not have reference to dialog object and second, any manual operation only interferences
    * and breaks state maintained by frameworks.
    */
   dismissDialog(0);
      }
  }).start();
     }
 });
    }
    @Override
    protected Dialog onCreateDialog(int id) {
 ProgressDialog d = new ProgressDialog(this);
 d.setTitle("Optional dialogging");
 d.setMessage("This dialogging scheme works best for most times, the dialogs are all taken care of by activitys and frameworks" +
   ". Except for activity being killed during dialog showing");
 d.setIndeterminate(true);
 d.setCancelable(true);
 return d;
    }
    @Override
    protected void onDestroy() {
 super.onDestroy();
 // Activity is dying, all its belonging dialogs should be dismissed, of course.
 if (mBetterDialog != null && mBetterDialog.isShowing()) {
     mBetterDialog.dismiss();
     mBetterDialog = null;
 }
 // For dialogs showed via showDialog(int), no way to stop it in onDestroy()
// dismissDialog(0); // cause "IllegalArgumentException: no dialog with id 0 was ever shown via Activity#showDialog"
       // This is because Activity has to manage its dialog during onPause() and onResume() to restore
                   // dialogs' state. So if you manually dismiss it in onDestroy(), it will cause JE.

// removeDialog(0);// cause "IllegalArgumentException: no dialog with id 0 was ever shown via Activity#showDialog", when
   // dismissing in thread.
               // This is because Activity has to manage its dialog during onPause() and onResume() to restore
                     // dialogs' state. So if you manually dismiss it in onDestroy(), it will cause JE.
    }
}

更多精彩内容其他人还在看

使用ViewPager实现android软件使用向导功能实现步骤

现在的大部分android软件,都是使用说明,就是第一次使用该软件时,会出现向导,可以左右滑动,然后就进入应用的主界面了,下面我们就实现这个功能
收藏 0 赞 0 分享

android在异步任务中关闭Cursor的代码方法

android在异步任务中如何关闭Cursor?在我们开发应用的时候,很多时候会遇到这种问题,下面我们就看看代码如何实现
收藏 0 赞 0 分享

Android自定义桌面功能代码实现

android自定义桌面其实很简单,看一个例子就明白了
收藏 0 赞 0 分享

android将图片转换存到数据库再从数据库读取转换成图片实现代码

有时候我们想把图片存入到数据库中,尽管这不是一种明智的选择,但有时候还是不得以会用到,下面说说将图片转换成byte[]数组存入到数据库中去,并从数据库中取出来转换成图像显示出来
收藏 0 赞 0 分享

TextView显示系统时间(时钟功能带秒针变化

用System.currentTimeMillis()可以获取系统当前的时间,我们可以开启一个线程,然后通过handler发消息,来实时的更新TextView上显示的系统时间,可以做一个时钟的功能
收藏 0 赞 0 分享

Android用ListView显示SDCard文件列表的小例子

本文简单实现了用ListView显示SDCard文件列表,目录的回退等功能暂不讨论,获取文件列表,files即为所选择目录下的所有文件列表
收藏 0 赞 0 分享

Android拦截外拨电话程序示例

这篇文章主要介绍了Android拦截外拨电话的示例,大家参考使用吧
收藏 0 赞 0 分享

通过Html网页调用本地安卓(android)app程序代码

如何使用html网页和本地app进行传递数据呢?经过研究,发现还是有方法的,总结了一下,大致有一下几种方式
收藏 0 赞 0 分享

android Textview文字监控(Textview使用方法)

以手机号充值为例,当用户输入最后一位数时候,进行汇率的变换,本文就实现类似这样的功能
收藏 0 赞 0 分享

Android ListView长按弹出菜单二种实现方式示例

这篇文章主要介绍了Android ListView长按弹出菜单的方法,大家参考实现
收藏 0 赞 0 分享
查看更多