Android实战技巧:Dialog (转)
转:http://blog.csdn.net/hitlion2008/article/details/7567549#t0
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.
}
}
Android实战技巧:Dialog (转)的更多相关文章
- Android实战技巧:深入解析AsyncTask
AsyncTask的介绍及基本使用方法 关于AsyncTask的介绍和基本使用方法可以参考官方文档和Android实战技巧:多线程AsyncTask这里就不重复. AsyncTask引发的一个问题 上 ...
- Android实战技巧之十二:Android Studio导入第三方类库、jar包和so库
第三方类库源码 将一网友的XMPP代码从ADT转到AS时,发现其使用了第三方类库,源码放在了lib下,直接在AS中Import project,第三方类库并没有自动导入进来,看来需要自己动手了. 项目 ...
- Android实战技巧:ViewStub的应用
在开发应用程序的时候,经常会遇到这样的情况,会在运行时动态根据条件来决定显示哪个View或某个布局.那么最通常的想法就是把可能用到的View都写在上面,先把它们的可见性都设为View.GONE,然后在 ...
- Android实战技巧之六:PreferenceActivity使用详解
一.写作前面 当我们做应用的时候,需要用户配置一些信息,而这就是通常所说的应用设置. 对于Android系统来说,系统本身的设置带来的用户体验和习惯已经深入人心,在我们的应用中同样用到类似的设置页, ...
- Android实战技巧之十九:android studio导出jar包(Module)并获得手机信息
AS中并没有独立的Module 工程,可是能够在普通的Project中增加Module.所谓的Module就是我们通常所指的模块化的一个单元.并经常以jar包的形式存在.以下以一个获取手机信息的样例演 ...
- Android实战技巧: ListView之ContextMenu无法弹出
问题 Activity中使用了ListView作为布局.当每一列表项中含有默认能获取焦点的子View时有可能会对ListView的某些事件有影响: 1. OnItemClick 2. OnItemLo ...
- 【转】Android实战技巧之四十九:Usb通信之USB Host
零 USB背景知识 USB是一种数据通信方式,也是一种数据总线,而且是最复杂的总线之一. 硬件上,它是用插头连接.一边是公头(plug),一边是母头(receptacle).例如,PC上的插座就是母头 ...
- Android实战技巧:如何在ScrollView中嵌套ListView
前几天因为项目的需要,要在一个ListView中放入另一个ListView,也即在一个ListView的每个ListItem中放入另外一个ListView.但刚开始的时候,会发现放入的小ListVie ...
- Android实战技巧之三十八:Handler使用中可能引发的内存泄漏
问题描写叙述 曾几何时,我们用原来的办法使用Handler时会有以下一段温馨的提示: This Handler class should be static or leaks might occur ...
随机推荐
- 手撸红黑树-Red-Black Tree 入门
一.学习红黑树前的准备: 熟悉基础数据结构 了解二叉树概念 二.红黑树的规则和规则分析: 根节点是黑色的 所有叶子节点(Null)是黑色的,一般会认定节点下空节点全部为黑色 如果节点为红色,那么子节点 ...
- 结合Pool进程池进程,实现进程之间的通讯,稍微复杂的运用
#进程池中的Queue """ 如果要用Pool创建进程,就需要multiprocessing.Manager()中的Queue() 而不是multiprocessing ...
- 身份证验证的js
function isIdCardNo(num) { num = num.toUpperCase(); //身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能 ...
- 【报错】An error happened during template parsing (template: "class path resource [templates/hello1.html]")
页面显示: Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing t ...
- HNCPC2019部分题解
ProblemSet 签到题就不写了. C. Distinct Substrings 先对原串建出SAM,map存边. 由于这题相当于添加一个字符再删除这个字符,添加下一个字符,所以每次都暴力跳后缀链 ...
- JVM(9)之 年轻代收集器
开发十年,就只剩下这套架构体系了! >>> 继续上一篇博文所讲的,STW即GC时候的停顿时间,他会暂停我们程序中的所有线程.如果STW所用的时间长而且次数多的话,那么我们整个系统 ...
- 你创不出伟大的事业,因为……
你认为自己是对的,而别人都不了解.你忙著实现自己的梦想,却不管你的梦想对这世界有什么意义.你成天想著自己的问题,对别人的问题却提不起任何兴趣. 你活在自己的世界 你认为自己是对的,而别人都不了解.你忙 ...
- Redis线上环境做Keys匹配操作!你可以离职了!
转自:https://blog.csdn.net/bntx2jsqfehy7/article/details/84207884一.一个新闻 新闻内容如下:php工程师执行redis keys * 导致 ...
- 2019西北工业大学程序设计创新实践基地春季选拔赛 D(卢卡斯定理)
链接:https://ac.nowcoder.com/acm/contest/553/D来源:牛客网 Chino with Equation 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C ...
- Django学习笔记--数据库中的单表操作----增删改查
1.Django数据库中的增删改查 1.添加表和字段 # 创建的表的名字为app的名称拼接类名 class User(models.Model): # id字段 自增 是主键 id = models. ...