Dialog是所有对话框的基类,但Dialog并非继承自View,而是直接从Object构造出来的。Dialog调用是异步调用,所以showDialog()时不会阻碍UI线程。
1. Activity托管对话框:
Android提供了创建对话框的快捷方式,在Activity中可以通过如showDialog(int dialogId),dismissDialog(int dialogId),onCreateDialog(),onPrepareDialog(),removeDialog()等方法来创建和管理对话框。
onCreateDialog(int dialogId)和onPrepareDialog(int dialogId, Dialog dialog)是Dialog的2个回调函数,showDialog()触发这两个回调函数的调用。同所有的onCreate()一样,其只在Dialog第一次生成的时候才会被调用,而onPrepareDialog()在每次执行showDialog()都会被调用(不管Dialog生成了没有)。如果你想要更新对话框的内容,你只要在onPrepareDialog()中作相应的工作就可以了,该方法会在对话框显示之前进行调用。
dismissDialog()方法是用来关闭对话框的;removeDialog()方法用来将对话框从Activity的托管中移除(如果对已经移除的对话框重新进行调用showDialog ,则该对话框将进行重新创建)。
2. 常用Dialog
(1)AlertDialog
AlertDialog类是Dialog类的子类,它默认提供了3个按钮和一个文本消息,这些按钮可以按需要来使他们显示或隐藏。AlertDialog类中有一个内部静态类,名为“Builder”,Builder类提供了为对话框添加多选或单选列表,以及为这些列表添加事件处理的功能。另外,这个Builder类将AlertDialog对话框上的3个按钮按照他们的位置分别称呼为:PositiveButton, NeutralButton, NegativeButton。
(2)ProgressDialog
ProgressDialog.dialog = new ProgressDialog(context); 没有内部静态类,直接构造函数构造
3. 一个很特别的Dialog(由Activity转换而来) ,具体请参见参考doc中android.R.style部分
(1)AndroidManifest.xml中的activity的属性中增加:android :theme="@android :style/Theme.Dialog( Activity的对话框主题)。可使Activity变为Dialog(浮动窗口);
(2)AndroidManifest.xml中的activity的属性中增加:android:theme="@android:style/Theme.Translucent"(Activity半透明主题);
4. 示例代码
(1)自定义Dialog并获取Dialog中EditText的数据
- 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();
- break;
- case R.id.cancelButton:
- cancel();
- break;
- }
-
-
-
-
-
- }
-
- }
- 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>
(2)
- 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();
- }
- }
(3)托管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);
-
-
- 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 inflater = LayoutInflater.from(this);
- View textEntryView = inflater.inflate(R.layout.alert_dialog_text_entry, null);
- builder.setTitle(R.string.alert_dialog_text_entry);
-
-
- 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() {
-
- button1 = (Button) findViewById(R.id.button1);
- button2 = (Button) findViewById(R.id.button2);
- button3 = (Button) findViewById(R.id.button3);
- button4 = (Button) findViewById(R.id.button4);
-
-
- 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);
- }
- };
-
-
- 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"
- >
-
-
- <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>