Activities提供了一种方便管理的创建、保存、回复的对话框机制,例如onCreateDialog(int),onPrepareDialog(int,Dialog),showDialog(int),dismissDialog(int)等方法,如果使用这些方法的话,Activity将通过getOwnerActivity()方法返回该Activity管理的对话框(dialog).
onCreateDialog(int):当你使用这个回调函数时,Android系统会有效的设置这个Activity为每个对话框的所有者,从而自动管理每个对话框的状态并挂靠到Activity上。这样,每个对话框继承这个Activity的特定属性。比如,当一个对话框打开时,菜单键显示为这个Activity定义的选项菜单,音量键修改Activity使用的音频流。
showDialog(int):当你想要显示一个对话框时,调用showDialog(intid)方法并传递一个唯一标识这个对话框的整数。当对话框第一次被请求时,Android从你的Activity中调用onCreateDialog(intid),你应该在这里初始化这个对话框Dialog。这个回调方法被传以和showDialog(intid)相同的ID。当你创建这个对话框后,在Activity的最后返回这个对象。
onPrepareDialog(int,Dialog):在对话框被显示之前,Android还调用了可选的回调函数onPrepareDialog(intid,Dialog).如果你想在每一次对话框被打开时改变它的任何属性,你可以定义这个方法。这个方法在每次打开对话框时被调用,而onCreateDialog(int)仅在对话框第一次打开时被调用。如果你不定义onPrepareDialog(),那么这个对话框将保持和上次打开时一样。这个方法也被传递以对话框的ID,和在onCreateDialog()中创建的对话框对象。
dismissDialog(int):当你准备关闭对话框时,你可以通过对这个对话框调用dismiss()来消除它。如果需要,你还可以从这个Activity中调用dismissDialog(intid)方法,这实际上将为你对这个对话框调用dismiss()方法。如果你想使用onCreateDialog(intid)方法来管理你对话框的状态(就如同在前面的章节讨论的那样),然后每次你的对话框消除的时候,这个对话框对象的状态将由该Activity保留。如果你决定不再需要这个对象或者清除该状态是重要的,那么你应该调用removeDialog(intid)。这将删除任何内部对象引用而且如果这个对话框正在显示,它将被消除。
进度条对话框
日期对话框
确认对话框
多选对话框
自定义对话框
自定义Fragment对话框
Toast弹出对话框
上面的各图代码如下:
activity_dialog1_layout.xml
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/dialog_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="进度条对话框" /> <Button android:id="@+id/date_dialog_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="日期对话框" /> <Button android:id="@+id/ok_dialog_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="确认对话框" /> <Button android:id="@+id/check_dialog_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="多选对话框" /> <Button android:id="@+id/my_dialog_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onMyDialogViewClick" android:text="自定义对话框" /> <Button android:id="@+id/my_alert_dialog_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onMyAlertDialogViewClick" android:text="自定义对话框AlertDialog" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onMyDialogFragmentViewClick" android:text="MyDialogFragment" /> <Button android:id="@+id/toast_dialog_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onToastDialogViewClick" android:text="Toast" /> </LinearLayout></span>
view_my_dialog_layout.xml
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_margin="20dp" android:background="@android:color/holo_blue_dark" > <TextView android:id="@+id/message" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:textSize="24sp" android:text="确定退出吗?" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_horizontal" android:layout_marginTop="20dp" > <Button android:id="@+id/ok_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="退出" /> <Button android:id="@+id/cancel_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消" /> </LinearLayout> </LinearLayout></span>
DialogActivity.java
<span style="font-size:18px;">package com.sc.android.ui.dialog; import android.annotation.SuppressLint; import android.app.Activity; import android.app.DatePickerDialog; import android.app.DatePickerDialog.OnDateSetListener; import android.app.ProgressDialog; import android.app.TimePickerDialog; import android.app.TimePickerDialog.OnTimeSetListener; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.DatePicker; import android.widget.TimePicker; import android.widget.Toast; import com.sc.android.R; import com.sc.android.util.Logs; @SuppressLint("HandlerLeak") public class DialogActivity extends Activity { private Button mProgressBtn, mDateBtn, mTimeBtn; private ProgressDialog mProgressDialog; private DatePickerDialog mDateDialog; private TimePickerDialog mTimeDialog; private Handler mHandler = new Handler() { public void handleMessage(android.os.Message msg) { mProgressDialog.dismiss(); Toast.makeText(DialogActivity.this, "加载完成", Toast.LENGTH_SHORT) .show(); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_dialog1_layout); mProgressBtn = (Button) findViewById(R.id.progressdialog_btn); mDateBtn = (Button) findViewById(R.id.datedialog_btn); mTimeBtn = (Button) findViewById(R.id.timedialog_btn); mProgressDialog = new ProgressDialog(this); mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); mProgressDialog.setTitle("进度条对话框"); mProgressDialog.setMessage("进度加载中......"); mProgressBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable() { @Override public void run() { for (int i = 0; i <= 100; i++) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } // mProgressDialog.incrementProgressBy(1);//增量参数 mProgressDialog.setProgress(i); Logs.v(">>>>>>>>>> " + i); } Logs.v(">>>sendEmptyMessage>>>>>> "); mHandler.sendEmptyMessage(0); } }).start(); mProgressDialog.show(); } }); mDateDialog = new DatePickerDialog(this, new OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { } }, 2015, 7, 1); mDateBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mDateDialog.show(); } }); mTimeDialog = new TimePickerDialog(this, new OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { } }, 14, 50, true); mTimeBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mTimeDialog.show(); } }); } } </span>
MyAlertDialog.java
<span style="font-size:18px;">package com.scxh.android1502.ui.dialog; import android.app.AlertDialog; import android.content.Context; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; import com.scxh.android1502.R; public class MyAlertDialog extends AlertDialog { private Button mOkBtn, mCancelBtn; private Context mContext; protected MyAlertDialog(Context context) { super(context); mContext = context; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.view_my_dialog_layout); mOkBtn = (Button) findViewById(R.id.ok_btn); mCancelBtn = (Button) findViewById(R.id.cancel_btn); mOkBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(mContext, "确定成功>>>", Toast.LENGTH_SHORT).show(); dismiss(); } }); mCancelBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(mContext, "取消成功>>>", Toast.LENGTH_SHORT).show(); dismiss(); } }); } } </span>
MyAlertDialogFragment.java
<span style="font-size:18px;">package com.scxh.android1502.ui.dialog; import android.app.AlertDialog; import android.app.Dialog; import android.app.DialogFragment; import android.content.DialogInterface; import android.os.Bundle; import android.widget.Toast; /** * 调用方法: * MyAlertDialogFragment dialogFragment = new MyAlertDialogFragment(); * dialogFragment.show(getFragmentManager(), "dialog"); * */ public class MyAlertDialogFragment extends DialogFragment { // 法一 @Override public Dialog onCreateDialog(Bundle savedInstanceState) { return new AlertDialog.Builder(getActivity()) .setIcon(android.R.drawable.ic_dialog_alert) .setTitle("提示框") .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { Toast.makeText(getActivity(), "确定", Toast.LENGTH_SHORT).show(); } }) .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { Toast.makeText(getActivity(), "取消", Toast.LENGTH_SHORT).show(); } }).create(); } }</span>