南通網(wǎng)站關鍵詞優(yōu)化發(fā)稿吧
Dialog是所有對話框的基類,但Dialog并非繼承自View,而是直接從Object構(gòu)造出來的。Dialog調(diào)用是異步調(diào)用,所以showDialog()時不會阻礙UI線程。
1. Activity托管對話框:
????? Android提供了創(chuàng)建對話框的快捷方式,在Activity中可以通過如showDialog(int dialogId),dismissDialog(int dialogId),onCreateDialog(),onPrepareDialog(),removeDialog()等方法來創(chuàng)建和管理對話框。
????? onCreateDialog(int dialogId)和onPrepareDialog(int dialogId, Dialog dialog)是Dialog的2個回調(diào)函數(shù),showDialog()觸發(fā)這兩個回調(diào)函數(shù)的調(diào)用。 同所有的onCreate()一樣,其只在Dialog第一次生成的時候才會被調(diào)用,而onPrepareDialog()在每次執(zhí)行showDialog()都會被調(diào)用(不管Dialog生成了沒有)。如果你想要更新對話框的內(nèi)容,你只要在onPrepareDialog()中作相應的工作就可以了,該方法會在對話框顯示之前進行調(diào)用。
????? dismissDialog()方法是用來關閉對話框的;removeDialog()方法用來將對話框從Activity的托管中移除 (如果對已經(jīng)移除的對話框重新進行調(diào)用showDialog ,則該對話框?qū)⑦M行重新創(chuàng)建)。
2. 常用Dialog
(1)AlertDialog
AlertDialog類是Dialog類的子類,它默認提供了3個按鈕和一個文本消息,這些按鈕可以按需要來使他們顯示或隱藏。AlertDialog類中有一個內(nèi)部靜態(tài)類,名為“Builder”,Builder類提供了為對話框添加多選或單選列表,以及為這些列表添加事件處理的功能。另外,這個Builder類將AlertDialog對話框上的3個按鈕按照他們的位置分別稱呼為:PositiveButton, NeutralButton, NegativeButton。
(2)ProgressDialog
ProgressDialog.dialog = new ProgressDialog(context); 沒有內(nèi)部靜態(tài)類,直接構(gòu)造函數(shù)構(gòu)造
3. 一個很特別的Dialog(由Activity轉(zhuǎn)換而來) ,具體請參見參考doc中android.R.style部分
(1)AndroidManifest.xml中的activity的屬性中增加:android :theme="@android :style/Theme.Dialog( Activity的對話框主題)。可使Activity變?yōu)镈ialog(浮動窗口);
(2)AndroidManifest.xml中的activity的屬性中增加:android:theme="@android:style/Theme.Translucent"(Activity半透明主題);
4. 示例代碼
(1)自定義Dialog并獲取Dialog中EditText的數(shù)據(jù)
mydialog.xml
托管Dialog
alert_dialog_text_entry.xml
去除對話框Dialog白色邊框
使用樣式文件,在values 目錄下新建styles.xml文件,編寫如下代碼:
調(diào)用時,使用AlerDialog的接口類,Dialog 接口編寫如下代碼:
下面我們查看一下Dialog的源碼文件,里面的構(gòu)造函數(shù)為如下:
這里面我們可以看出,Android 使用了默認的構(gòu)造函數(shù)為Dialog 設置樣式,如果沒有為其設置樣式,即默認加載事先編寫好的樣式文件,Dialog 一共由多個9.png的圖片構(gòu)成,大部分都是帶有邊框的9.png圖片,所以就是為什么我們上邊的樣式文件要將其背景去除掉。
前后效果對比
未設置前:
設置后:
模式對話框Dialog背景的透明度&黑暗度設置方法
設置透明度:
alpha在0.0f到1.0f之間。1.0完全不透明,0.0f完全透明
設置黑暗度:
Android 之 ProgressDialog
http://blog.csdn.net/dadahacker/archive/2011/02/25/6208368.aspx
Android 對話框(Dialog)大全 建立你自己的對話框
http://www.cnblogs.com/salam/archive/2010/11/15/1877512.html
1. Activity托管對話框:
????? Android提供了創(chuàng)建對話框的快捷方式,在Activity中可以通過如showDialog(int dialogId),dismissDialog(int dialogId),onCreateDialog(),onPrepareDialog(),removeDialog()等方法來創(chuàng)建和管理對話框。
????? onCreateDialog(int dialogId)和onPrepareDialog(int dialogId, Dialog dialog)是Dialog的2個回調(diào)函數(shù),showDialog()觸發(fā)這兩個回調(diào)函數(shù)的調(diào)用。 同所有的onCreate()一樣,其只在Dialog第一次生成的時候才會被調(diào)用,而onPrepareDialog()在每次執(zhí)行showDialog()都會被調(diào)用(不管Dialog生成了沒有)。如果你想要更新對話框的內(nèi)容,你只要在onPrepareDialog()中作相應的工作就可以了,該方法會在對話框顯示之前進行調(diào)用。
????? dismissDialog()方法是用來關閉對話框的;removeDialog()方法用來將對話框從Activity的托管中移除 (如果對已經(jīng)移除的對話框重新進行調(diào)用showDialog ,則該對話框?qū)⑦M行重新創(chuàng)建)。
2. 常用Dialog
(1)AlertDialog
AlertDialog類是Dialog類的子類,它默認提供了3個按鈕和一個文本消息,這些按鈕可以按需要來使他們顯示或隱藏。AlertDialog類中有一個內(nèi)部靜態(tài)類,名為“Builder”,Builder類提供了為對話框添加多選或單選列表,以及為這些列表添加事件處理的功能。另外,這個Builder類將AlertDialog對話框上的3個按鈕按照他們的位置分別稱呼為:PositiveButton, NeutralButton, NegativeButton。
(2)ProgressDialog
ProgressDialog.dialog = new ProgressDialog(context); 沒有內(nèi)部靜態(tài)類,直接構(gòu)造函數(shù)構(gòu)造
3. 一個很特別的Dialog(由Activity轉(zhuǎn)換而來) ,具體請參見參考doc中android.R.style部分
(1)AndroidManifest.xml中的activity的屬性中增加:android :theme="@android :style/Theme.Dialog( Activity的對話框主題)。可使Activity變?yōu)镈ialog(浮動窗口);
(2)AndroidManifest.xml中的activity的屬性中增加:android:theme="@android:style/Theme.Translucent"(Activity半透明主題);
4. 示例代碼
(1)自定義Dialog并獲取Dialog中EditText的數(shù)據(jù)
- public?class?MyDialog?extends?Dialog?implements?Button.OnClickListener?{??
- ????private?Button?okButton,?cancelButton;??
- ????private?EditText?nameEditText;??
- ????private?MyDialogListener?listener;??
- ??????
- ????public?MyDialog(Context?context,?MyDialogListener?listener)?{??
- ????????super(context);??
- ????????this.listener?=?listener;??
- ????}??
- ????protected?void?onCreate(Bundle?savedInstanceState)?{??
- ????????setContentView(R.layout.mydialog);??
- ??????????
- ????????okButton?=?(Button)?findViewById(R.id.okButton);??
- ????????cancelButton?=?(Button)?findViewById(R.id.cancelButton);??
- ????????okButton.setOnClickListener(this);??
- ????????cancelButton.setOnClickListener(this);??
- ??????????
- ????????nameEditText?=?(EditText)?findViewById(R.id.nameEditText);??
- ????}??
- ????public?void?onClick(View?v)?{??
- ????????switch?(v.getId())?{??
- ????????case?R.id.okButton:??
- ?????????????listener.onOkClick(nameEditText.getText().toString());??
- ?????????????dismiss();?//?關閉Dialog??
- ?????????????break;??
- ????????case?R.id.cancelButton:??
- ?????????????cancel();?//?取消Dialog,?"取消"的含義是指不再需要執(zhí)行對話框上的任何功能和動作,?取消對話框會自動調(diào)用dismiss()方法??
- ?????????????break;??
- ????????}??
- ????????/*?
- ?????????*?當用戶點擊手機設備上的“返回”按鈕時,屏幕上的對話框?qū)蝗∠?#xff0c;?
- ?????????*?如果你想讓你的對話框不在這種情況下被取消掉的話,你可以如下設置你的對話框:setCancelable(false);?
- ?????????*?對話框的取消和關閉事件可以通過OnCancelListener和OnDismissListener兩個監(jiān)聽器來被監(jiān)聽處理。?
- ?????????*/??
- ????}??
- ??????
- }??
- public?class?MainActivity?extends?Activity?implements?MyDialogListener?{??
- ????public?void?onCreate(Bundle?savedInstanceState)?{??
- ????????super.onCreate(savedInstanceState);??
- ????????MyDialog?dialog?=?new?MyDialog(this,?this);??
- ????????dialog.show();??
- ????}??
- ????public?void?onCancelClick()?{??
- ????}??
- ????public?void?onOkClick(String?name)?{??
- ????????Toast.makeText(this,?name,?Toast.LENGTH_LONG).show();??
- ????}??
- }??
- ??
- interface?MyDialogListener?{??
- ????public?void?onOkClick(String?name);??
- ????public?void?onCancelClick();??
- }??
mydialog.xml
- <?xml?version="1.0"?encoding="UTF-8"?>??
- <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
- ????android:layout_width="fill_parent"??
- ????android:layout_height="wrap_content"??
- ????android:orientation="vertical">??
- ????<TextView??
- ????????android:id="@+id/nameMessage"??
- ????????android:layout_width="fill_parent"??
- ????????android:layout_height="wrap_content"??
- ????????android:text="Enter?Name:"></TextView>??
- ????????<EditText??
- ????????????android:id="@+id/nameEditText"??
- ????????????android:layout_width="fill_parent"??
- ????????????android:layout_height="wrap_content"??
- ????????????android:textSize="18sp"></EditText>??
- ????????<LinearLayout??
- ????????????android:id="@+id/buttonLayout"??
- ????????????android:layout_width="fill_parent"??
- ????????????android:layout_height="wrap_content"??
- ????????????android:layout_gravity="center_horizontal">??
- ????????????<Button??
- ????????????????android:id="@+id/okButton"??
- ????????????????android:layout_width="wrap_content"??
- ????????????????android:layout_height="wrap_content"??
- ????????????????android:text="OK">??
- ????????????</Button>??
- ????????????<Button??
- ????????????????android:id="@+id/cancelButton"??
- ????????????????android:layout_width="wrap_content"??
- ????????????????android:layout_height="wrap_content"??
- ????????????????android:text="Cancel">??
- ????????????</Button>??
- ?????</LinearLayout>??
- </LinearLayout>??
- public?class?MainActivity2?extends?Activity?{??
- ????public?void?onCreate(Bundle?savedInstanceState)?{??
- ????????super.onCreate(savedInstanceState);??
- ??????????
- ????????AlertDialog?dialog?=?new?AlertDialog.Builder(this).create();??
- ????????dialog.setMessage("Do?you?play?cricket?");??
- ????????dialog.setButton("Yes",?new?DialogInterface.OnClickListener()?{??
- ????????????public?void?onClick(DialogInterface?dialog,?int?which)?{??
- ????????????????switch?(which)?{??
- ????????????????case?AlertDialog.BUTTON1:??
- ????????????????????break;??
- ????????????????case?AlertDialog.BUTTON2:??
- ????????????????????break;??
- ????????????????}??
- ????????????}??
- ????????});??
- ????????dialog.setButton2("No",?new?DialogInterface.OnClickListener()?{??
- ????????????public?void?onClick(DialogInterface?dialog,?int?which)?{??
- ????????????}??
- ????????});??
- ????????dialog.show();??
- ????}??
- }??
托管Dialog
- public?class?MainActivity?extends?Activity?{??
- ????private?Button?button1,?button2,?button3,?button4;??
- ????private?Button.OnClickListener?listener1,?listener2,?listener3,?listener4;??
- ????private?final?int?DIALOG1?=?1,?DIALOG2?=?2,?DIALOG3?=?3,?DIALOG4?=?4;??
- ??????
- ????@Override??
- ????public?void?onCreate(Bundle?savedInstanceState)?{??
- ????????super.onCreate(savedInstanceState);??
- ????????setContentView(R.layout.main);??
- ????????prepareListener();??
- ????}??
- ??????
- ????protected?Dialog?onCreateDialog(int?id)?{??
- ????????switch?(id)?{??
- ????????case?DIALOG1:??
- ????????????return?buildDialog(this,?DIALOG1);??
- ????????case?DIALOG2:??
- ????????????return?buildDialog(this,?DIALOG2);??
- ????????case?DIALOG3:??
- ????????????return?buildDialog(this,?DIALOG3);??
- ????????case?DIALOG4:??
- ????????????return?buildDialog(this,?DIALOG4);??
- ????????}??
- ????????return?null;??
- ????}??
- ??????
- ????protected?void?onPrepareDialog(int?id,?Dialog?dialog)?{??
- ????????super.onPrepareDialog(id,?dialog);??
- ????}??
- ??????
- ????private?Dialog?buildDialog(Context?context,?int?seq)?{??
- ????????AlertDialog.Builder?builder?=?new?AlertDialog.Builder(context);??
- ????????builder.setIcon(R.drawable.alert_dialog_icon);??
- ????????if?(DIALOG1?==?seq)?{??
- ????????????builder.setTitle(R.string.alert_dialog_two_buttons_title);??
- ????????????builder.setPositiveButton(R.string.alert_dialog_ok,?new?DialogInterface.OnClickListener()?{??
- ????????????????public?void?onClick(DialogInterface?dialog,?int?which)?{??
- ????????????????????setTitle(R.string.alert_dialog_ok);??
- ????????????????}??
- ????????????});??
- ????????????builder.setNegativeButton(R.string.alert_dialog_cancel,?new?DialogInterface.OnClickListener()?{??
- ????????????????public?void?onClick(DialogInterface?dialog,?int?which)?{??
- ????????????????????setTitle(R.string.alert_dialog_cancel);??
- ????????????????}??
- ????????????});??
- ????????}??
- ????????if?(DIALOG2?==?seq)?{??
- ????????????builder.setTitle(R.string.alert_dialog_two_buttons_msg);??
- ????????????builder.setMessage(R.string.alert_dialog_two_buttons2_msg);??
- ??????????????
- ????????????//?AlertDialog最多只能有3個Button??
- ????????????builder.setPositiveButton(R.string.alert_dialog_ok,?new?DialogInterface.OnClickListener()?{??
- ????????????????public?void?onClick(DialogInterface?dialog,?int?which)?{??
- ????????????????????setTitle(R.string.alert_dialog_ok);??
- ????????????????}??
- ????????????});??
- ????????????builder.setNeutralButton(R.string.alert_dialog_something,?new?DialogInterface.OnClickListener()?{??
- ????????????????public?void?onClick(DialogInterface?dialog,?int?which)?{??
- ????????????????????setTitle(R.string.alert_dialog_something);??
- ????????????????}??
- ????????????});??
- ????????????builder.setNegativeButton(R.string.alert_dialog_cancel,?new?DialogInterface.OnClickListener()?{??
- ????????????????public?void?onClick(DialogInterface?dialog,?int?which)?{??
- ????????????????????setTitle(R.string.alert_dialog_cancel);??
- ????????????????}??
- ????????????});??
- ????????}??
- ????????if?(DIALOG3?==?seq)?{??
- ????????????//?LayoutInflater的inflate方法可以將一個XML布局變成一個View實例??
- ????????????LayoutInflater?inflater?=?LayoutInflater.from(this);??
- ????????????View?textEntryView?=?inflater.inflate(R.layout.alert_dialog_text_entry,?null);??
- ????????????builder.setTitle(R.string.alert_dialog_text_entry);??
- ??????????????
- ????????????//?setView()真可以說是Dialog的一個精髓??
- ????????????builder.setView(textEntryView);??
- ????????????builder.setPositiveButton(R.string.alert_dialog_ok,?new?DialogInterface.OnClickListener()?{??
- ????????????????public?void?onClick(DialogInterface?dialog,?int?which)?{??
- ????????????????????setTitle(R.string.alert_dialog_ok);??
- ????????????????}??
- ????????????});??
- ????????????builder.setNegativeButton(R.string.alert_dialog_cancel,?new?DialogInterface.OnClickListener()?{??
- ????????????????public?void?onClick(DialogInterface?dialog,?int?which)?{??
- ????????????????????setTitle(R.string.alert_dialog_cancel);??
- ????????????????}??
- ????????????});??
- ????????}??
- ????????if?(DIALOG4?==?seq)?{??
- ????????????ProgressDialog?dialog?=?new?ProgressDialog(context);??
- ????????????dialog.setTitle("Downding?the?songs...");??
- ????????????dialog.setMessage("Please?Waiting...");??
- ????????????return?dialog;??
- ????????}??
- ????????return?builder.create();??
- ????}??
- ??
- ????private?boolean?prepareListener()?{??
- ????????//?init?button??
- ????????button1?=?(Button)?findViewById(R.id.button1);??
- ????????button2?=?(Button)?findViewById(R.id.button2);??
- ????????button3?=?(Button)?findViewById(R.id.button3);??
- ????????button4?=?(Button)?findViewById(R.id.button4);??
- ??????????
- ????????//?init?listener??
- ????????listener1?=?new?Button.OnClickListener()?{??
- ????????????public?void?onClick(View?v)?{??
- ????????????????showDialog(DIALOG1);??
- ????????????}??
- ????????};??
- ????????listener2?=?new?Button.OnClickListener()?{??
- ????????????public?void?onClick(View?v)?{??
- ????????????????showDialog(DIALOG2);??
- ????????????}??
- ????????};??
- ????????listener3?=?new?Button.OnClickListener()?{??
- ????????????public?void?onClick(View?v)?{??
- ????????????????showDialog(DIALOG3);??
- ????????????}??
- ????????};??
- ????????listener4?=?new?Button.OnClickListener()?{??
- ????????????public?void?onClick(View?v)?{??
- ????????????????showDialog(DIALOG4);??
- ????????????}??
- ????????};??
- ??????????
- ????????//?bind?listener??
- ????????button1.setOnClickListener(listener1);??
- ????????button2.setOnClickListener(listener2);??
- ????????button3.setOnClickListener(listener3);??
- ????????button4.setOnClickListener(listener4);??
- ????????return?true;??
- ????}??
- }??
alert_dialog_text_entry.xml
- <?xml?version="1.0"?encoding="utf-8"?>??
- <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
- ????android:orientation="vertical"??
- ????android:layout_width="fill_parent"??
- ????android:layout_height="fill_parent"??
- ????>??
- ????<!--?android:textAppearance:設置字體,?系統(tǒng)自帶的字體?-->??
- ????<!--?android:capitalize:設置大小寫;none首字母小寫;words首字母大寫?-->??
- ????<TextView?android:id="@+id/username_view"??
- ????????android:layout_width="wrap_content"??
- ????????android:layout_height="wrap_content"??
- ????????android:layout_marginLeft="20dip"??
- ????????android:layout_marginRight="20dip"??
- ????????android:text="用戶名"??
- ????????android:textAppearance="?android:attr/textAppearanceMedium"??
- ????/>??
- ????<EditText?android:id="@+id/username_edit"??
- ????????android:layout_width="fill_parent"??
- ????????android:layout_height="wrap_content"??
- ????????android:layout_marginLeft="20dip"??
- ????????android:layout_marginRight="20dip"??
- ????????android:capitalize="words"??
- ????????android:textAppearance="?android:attr/textAppearanceMedium"??
- ????/>??
- ????<TextView?android:id="@+id/password_view"??
- ????????android:layout_width="wrap_content"??
- ????????android:layout_height="wrap_content"??
- ????????android:layout_marginLeft="20dip"??
- ????????android:layout_marginRight="20dip"??
- ????????android:text="密碼"??
- ????????android:textAppearance="?android:attr/textAppearanceMedium"??
- ????/>??
- ????<EditText?android:id="@+id/password_edit"??
- ????????android:layout_width="fill_parent"??
- ????????android:layout_height="wrap_content"??
- ????????android:layout_marginLeft="20dip"??
- ????????android:layout_marginRight="20dip"??
- ????????android:capitalize="none"??
- ????????android:password="true"??
- ????????android:textAppearance="?android:attr/textAppearanceMedium"??
- ????/>??
- </LinearLayout>??
去除對話框Dialog白色邊框
使用樣式文件,在values 目錄下新建styles.xml文件,編寫如下代碼:
- <resources>??
- ????<style?name="dialog"?parent="@android:style/Theme.Dialog">??
- ?????????<item?name="android:windowFrame">@null</item>??
- ????????<item?name="android:windowIsFloating">true</item>??
- ????????<item?name="android:windowIsTranslucent">false</item>??
- ????????<item?name="android:windowNoTitle">true</item>??
- ????????<item?name="android:background">@android:color/black</item>??
- ????????<item?name="android:windowBackground">@null</item>??
- ????????<item?name="android:backgroundDimEnabled">false</item>??
- ????</style>??
- </resources>??
調(diào)用時,使用AlerDialog的接口類,Dialog 接口編寫如下代碼:
- Dialog?dialog?=?new?Dialog(SetActivity.this,?R.style.dialog);??
- ???????dialog.setContentView(R.layout.test);??
- ???????dialog.show();??
下面我們查看一下Dialog的源碼文件,里面的構(gòu)造函數(shù)為如下:
- public?Dialog(Context?context,?int?theme)?{??
- ????????mContext?=?new?ContextThemeWrapper(??
- ????????????context,?theme?==?0???com.android.internal.R.style.Theme_Dialog?:?theme);??
- ????????mWindowManager?=?(WindowManager)context.getSystemService("window");??
- ????????Window?w?=?PolicyManager.makeNewWindow(mContext);??
- ????????mWindow?=?w;??
- ????????w.setCallback(this);??
- ????????w.setWindowManager(mWindowManager,?null,?null);??
- ????????w.setGravity(Gravity.CENTER);??
- ????????mUiThread?=?Thread.currentThread();??
- ????????mDismissCancelHandler?=?new?DismissCancelHandler(this);??
- ????}??
這里面我們可以看出,Android 使用了默認的構(gòu)造函數(shù)為Dialog 設置樣式,如果沒有為其設置樣式,即默認加載事先編寫好的樣式文件,Dialog 一共由多個9.png的圖片構(gòu)成,大部分都是帶有邊框的9.png圖片,所以就是為什么我們上邊的樣式文件要將其背景去除掉。
前后效果對比
未設置前:

設置后:

模式對話框Dialog背景的透明度&黑暗度設置方法
設置透明度:
- WindowManager.LayoutParams?lp=dialog.getWindow().getAttributes();????
- lp.alpha=1.0f;????
- dialog.getWindow().setAttributes(lp);????
alpha在0.0f到1.0f之間。1.0完全不透明,0.0f完全透明
設置黑暗度:
- dialog.setContentView(R.layout.dialog);????
- WindowManager.LayoutParams?lp=dialog.getWindow().getAttributes();????
- lp.dimAmount=1.0f;????
- dialog.getWindow().setAttributes(lp);????
- dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);??
Android 之 ProgressDialog
http://blog.csdn.net/dadahacker/archive/2011/02/25/6208368.aspx
Android 對話框(Dialog)大全 建立你自己的對話框
http://www.cnblogs.com/salam/archive/2010/11/15/1877512.html