翻译自:开发->API 指南->User Interface & Navigation->Dialogs
注意:
dialog是一个基类,但是我们应该尽可能避免直接使用dialog,而是应该使用其子类,比如AlertDialog,DatePickerDialog或者TimePickerDialog等。
使用DialogFragment去管理dialog,这会让你的app在用户点击返回键和旋转屏幕时,正确的控制dialog的生命周期。DialogFragment与传统的Fragment基本一致。
Creating a Dialog Fragment
使用DialogFragment创建一个基本的AlertDialog
public class FireMissilesDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_fire_missiles)
.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
如果你创建了以上的dialog,并且调用了show()方法,那你会看到如下所示的dialog。
Building an Alert Dialog
Alert Dialog包括三部分,标题,内容和按钮,如下图所示
创建一个Alert Dialog
// 1. Instantiate an AlertDialog.Builder with its constructor
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// 2. Chain together various setter methods to set the dialog characteristics
builder.setMessage(R.string.dialog_message)
.setTitle(R.string.dialog_title);
// 3. Get the AlertDialog from create()
AlertDialog dialog = builder.create();
添加按钮
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Add the buttons
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Set other dialog properties
...
// Create the AlertDialog
AlertDialog dialog = builder.create();
dialog里也可以添加list,具体的可以查看官方文档。
Creating a Custom Layout
If you want a custom layout in a dialog, create a layout and add it to an AlertDialog by calling setView() on your AlertDialog.Builder object.
如果想使用自定义布局,可以在AlertDialog.Builder中调用setView()方法,将自定义布局添加到AlertDialog中。
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog_signin, null))
// Add action buttons
.setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// sign in the user ...
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
LoginDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
小提示:如果你想创建一个自定义dialog,你可以显示一个Activity去代替dialog,只需要在
manifest中设置activity的主题为Theme.Holo.Dialog即可。如下所示 <activity android:theme="@android:style/Theme.Holo.Dialog" >
搞定,这样activity就会显示成 一个dialog,而不是全屏显示啦。