Android開發(fā)筆記之:Dialog的使用詳解
更新時(shí)間:2013年05月21日 11:25:05 作者:
本篇文章是對Android中Dialog的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
Dialog是任何系統(tǒng)都必須有的一個(gè)控件,作為輔助窗口,用于顯示一些消息,或請求用戶采取一引起操作等。
在Android中也不例外,基本使用可能參看文檔。
使用時(shí)的注意事項(xiàng)
1. BACK鍵能取消掉對話框(dismiss),但是卻不會觸發(fā)其onOkey和onCancel回調(diào)接口,所以如果你的對話框會改某些狀態(tài),一定要注意還有第三種方式取消對話框。
2. 盡量少用模態(tài)對話框(Model dialog),如果Dialog.setCancellable(false),就變成了一個(gè)模態(tài)對話框,除了程序內(nèi)部把其Dismiss,否則按什么鍵都無法將其取消。這是極差的用戶體驗(yàn),對話框本身就是一種干擾,再無法取消會把用戶搞瘋的。所以除非特別有必要,也即當(dāng)執(zhí)行某個(gè)操作時(shí)不希望被打破,才可以使用模態(tài)對話框。
3. 盡量少用對話框,它對用戶是一種干擾,除非需要用戶做操作,或者做出選擇。通常的一般性的通知用Toast或者Notification就足夠了。
4. 不要使用對話框風(fēng)格的Activity,也即把Activity變成一個(gè)對話框。因?yàn)檫@樣是自已定義的布局,與系統(tǒng)Dialog的風(fēng)格可能會不一致。最嚴(yán)重的是當(dāng)系統(tǒng)風(fēng)格發(fā)生變化,Dialog的子類會變化,但Activity式的對話框就不會變化??梢栽贗CS中找一找Activity對話框,你會發(fā)現(xiàn)其OK是在左邊,而ICS中系統(tǒng)Dialog的OK都是在右邊的。
5. 盡量保證Dialog對象活在Activity的生命周期之內(nèi),也即至多是在onCreate()和onDestroy()之間。
6. 要想到和測試到Activity在其Dialog.dismiss()之前死掉的情況。因?yàn)锳ctivity必須依附于某個(gè)正在顯示的Activity實(shí)例,當(dāng)顯示和取消的時(shí)候其Activity實(shí)例必須存在,否則就會有"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()必須在主線程里調(diào)用,但Dialog.dismiss()卻可以在任何線程中調(diào)用。
三種使用方式比較
1. 直接創(chuàng)建一個(gè)局部的Dialog對象
優(yōu)點(diǎn)是變量是局部的容易理解和維護(hù)。缺點(diǎn)是Dialog對象難以控制,容易引發(fā)RuntimeException。
2. 把Dialog對象變成Activity的域
優(yōu)點(diǎn)是Dialog對象可以重復(fù)利用,且Activity可以控制以保證Dialog不會在Activity生命周期外顯示。是推薦的使用方式。
3. 用Activity的方法onCreateDialog(), showDialog()和dismissDialog()
優(yōu)點(diǎn)是Frameworks會幫忙照看Dialog,在大多數(shù)情況下這是推薦的做法。但是對于Activity提前死掉的情況,此方法必有RuntimeException,且無法回避。
實(shí)例
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中也不例外,基本使用可能參看文檔。
使用時(shí)的注意事項(xiàng)
1. BACK鍵能取消掉對話框(dismiss),但是卻不會觸發(fā)其onOkey和onCancel回調(diào)接口,所以如果你的對話框會改某些狀態(tài),一定要注意還有第三種方式取消對話框。
2. 盡量少用模態(tài)對話框(Model dialog),如果Dialog.setCancellable(false),就變成了一個(gè)模態(tài)對話框,除了程序內(nèi)部把其Dismiss,否則按什么鍵都無法將其取消。這是極差的用戶體驗(yàn),對話框本身就是一種干擾,再無法取消會把用戶搞瘋的。所以除非特別有必要,也即當(dāng)執(zhí)行某個(gè)操作時(shí)不希望被打破,才可以使用模態(tài)對話框。
3. 盡量少用對話框,它對用戶是一種干擾,除非需要用戶做操作,或者做出選擇。通常的一般性的通知用Toast或者Notification就足夠了。
4. 不要使用對話框風(fēng)格的Activity,也即把Activity變成一個(gè)對話框。因?yàn)檫@樣是自已定義的布局,與系統(tǒng)Dialog的風(fēng)格可能會不一致。最嚴(yán)重的是當(dāng)系統(tǒng)風(fēng)格發(fā)生變化,Dialog的子類會變化,但Activity式的對話框就不會變化??梢栽贗CS中找一找Activity對話框,你會發(fā)現(xiàn)其OK是在左邊,而ICS中系統(tǒng)Dialog的OK都是在右邊的。
5. 盡量保證Dialog對象活在Activity的生命周期之內(nèi),也即至多是在onCreate()和onDestroy()之間。
6. 要想到和測試到Activity在其Dialog.dismiss()之前死掉的情況。因?yàn)锳ctivity必須依附于某個(gè)正在顯示的Activity實(shí)例,當(dāng)顯示和取消的時(shí)候其Activity實(shí)例必須存在,否則就會有"IllegalArgumentException: View not attached to window manager"。
復(fù)制代碼 代碼如下:
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()必須在主線程里調(diào)用,但Dialog.dismiss()卻可以在任何線程中調(diào)用。
三種使用方式比較
1. 直接創(chuàng)建一個(gè)局部的Dialog對象
優(yōu)點(diǎn)是變量是局部的容易理解和維護(hù)。缺點(diǎn)是Dialog對象難以控制,容易引發(fā)RuntimeException。
2. 把Dialog對象變成Activity的域
優(yōu)點(diǎn)是Dialog對象可以重復(fù)利用,且Activity可以控制以保證Dialog不會在Activity生命周期外顯示。是推薦的使用方式。
3. 用Activity的方法onCreateDialog(), showDialog()和dismissDialog()
優(yōu)點(diǎn)是Frameworks會幫忙照看Dialog,在大多數(shù)情況下這是推薦的做法。但是對于Activity提前死掉的情況,此方法必有RuntimeException,且無法回避。
實(shí)例
復(fù)制代碼 代碼如下:
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.
}
}
相關(guān)文章
Android DownloadMananger管理器實(shí)現(xiàn)下載圖片功能
Android DownloadMananger類似于下載隊(duì)列,管理所有當(dāng)前正在下載或者等待下載的項(xiàng)目,他可以維持HTTP鏈接,并且在隊(duì)列中的下載項(xiàng)目一旦失敗,還能自動重新下載2023-01-01Android Jetpack組件DataBinding詳解
這篇文章主要介紹了Android Jetpack組件DataBinding,DataBinding有很多優(yōu)勢,其中最明顯是代碼更加簡潔,可讀性會更高。部分和UI控件有關(guān)的代碼可以在布局文件當(dāng)中完成,本文給大家詳細(xì)講解,需要的朋友可以參考下2022-10-10Android仿微信圖片上傳帶加號且超過最大數(shù)隱藏功能
這篇文章給大家分享android仿照微信空間上傳圖片,顯示圖片數(shù)量以及超過最大,上傳按鈕隱藏功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2018-03-03Android 中通過ViewDragHelper實(shí)現(xiàn)ListView的Item的側(cè)拉劃出效果
這篇文章主要介紹了 Android 中通過ViewDragHelper實(shí)現(xiàn)ListView的Item的側(cè)拉劃出效果,需要的朋友可以參考下2017-08-08Android實(shí)現(xiàn)點(diǎn)匯聚成字的動態(tài)效果詳解
在引入?fl_chart?繪制圖表的時(shí)候,看到插件有下面這樣的動效,隨機(jī)散亂的圓點(diǎn)最后組合成了?Flutter?的?Logo,挺酷炫的。本篇我們來探討類似的效果怎么實(shí)現(xiàn)2022-07-07