标签:
AlertDialog
一个可以展示 标题,三个按钮,可选择项的列表或自定义布局 的对话框。DatePickerDialog
or TimePickerDialog
一个使用预先定义好的UI,允许用户选择一个日期或时间 的对话框android.app.DialogFragment
.。DialogFragment
:管理的: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();
}
}
AlertDialog
::// 1. 通过它的构造器实例化一个 AlertDialog.Builder
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// 2.将设置方法都集合在一起来设置对话框的属性
builder.setMessage(R.string.dialog_message)
.setTitle(R.string.dialog_title);
// 3. 从create()中获得
AlertDialogAlertDialog dialog = builder.create();
setPositiveButton()
和setNegativeButton()方法
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();
DialogInterface.OnClickListener
,该监听器定义了当用户点击该按钮时的操作。AlertDialog
.中。也就是说,不能有多个“positive”按钮。@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.pick_color)
.setItems(R.array.colors_array, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// which 变量包含了被选中项的索引值
}
});
return builder.create();
}
ListAdapter
,返回你的列表,经常使用一个Loader来异步加载内容。这些在Building Layouts with an Adapter 和 Loaders开发向导中详细的讲解了。setMultiChoiceItems()
或setSingleChoiceItems()
方法。ArrayList
:中:@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
mSelectedItems = new ArrayList(); //跟踪被选项的地方
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// 设置对话框标题
builder.setTitle(R.string.pick_toppings)
// 指定列表数组, 默认被选中项t (null for none),和选择项目时的监听器
.setMultiChoiceItems(R.array.toppings, null,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
if (isChecked) {
// 如果用户选中了项目,将它加入被选择项
mSelectedItems.add(which);
} else if (mSelectedItems.contains(which)) {
// 否则,如果项目已经在数组中,移除它
mSelectedItems.remove(Integer.valueOf(which));
}
}
})
// Set the action buttons
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// 用户点击OK,因此建议
mSelectedItems 结果存储在某处,或者从打开的对话框中返回它们。 // or return them to the component that opened the dialog
...
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
...
}
});
return builder.create();
}
setView()
来添加进去。<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="@drawable/header_logo"
android:layout_width="match_parent"
android:layout_height="64dp"
android:scaleType="center"
android:background="#FFFFBB33"
android:contentDescription="@string/app_name" />
<EditText
android:id="@+id/username"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="@string/username" />
<EditText
android:id="@+id/password"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="@string/password"/>
</LinearLayout>
LayoutInflater
并且调用inflate(),它的第一个参数是布局资源ID,第二个参数是布局的父view。之后你可以调用setView()来在该对话框中放置该布局。@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();
}
<activity>
配置元素中设置它的主题为Theme.Holo.Dialog
:<activity android:theme="@android:style/Theme.Holo.Dialog" >
DialogFragment
,定义了一个接口,通过它传递事件给持有者的activity:public class NoticeDialogFragmentextends DialogFragment {
/* 创建该对话框碎片引用的activity必须实现该接口
,为了收到事件回调 public interface NoticeDialogListener {
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);
}
// 使用该接口的实例来传递操作事件
NoticeDialogListener mListener;
// 重写Fragment.onAttach()方法来实例化 NoticeDialogListener
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// 验证持有者的activity 实现了回调接口
try {
// Instantiate the NoticeDialogListener so we can send events to the host
mListener = (NoticeDialogListener) activity;
} catch (ClassCastException e) {
// The activity doesn‘t implement the interface, throw exception
throw new ClassCastException(activity.toString()
+ " must implement NoticeDialogListener");
}
}
...
}
public class MainActivity extends FragmentActivity
implements NoticeDialogFragment.NoticeDialogListener{
...
public void showNoticeDialog() {
// 创建一个 dialog fragment 引用并且展示它
DialogFragment dialog = new NoticeDialogFragment();
dialog.show(getSupportFragmentManager(), "NoticeDialogFragment");
}
// dialog fragment 通过Fragment.onAttach()回调
返回一个参数给改activity ,它被用来调用如下方法,这些方法定义在 NoticeDialogFragment.NoticeDialogListener接口 @Override
public void onDialogPositiveClick(DialogFragment dialog) {
// User touched the dialog‘s positive button
...
}
@Override
public void onDialogNegativeClick(DialogFragment dialog) {
// User touched the dialog‘s negative button
...
}
}
public class NoticeDialogFragment extends DialogFragment {
...
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Build the dialog and set up the button click handlers
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) {
// Send the positive button event back to the host activity
mListener.onDialogPositiveClick(NoticeDialogFragment.this);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Send the negative button event back to the host activity
mListener.onDialogNegativeClick(NoticeDialogFragment.this);
}
});
return builder.create();
}
}
getFragmentManager()
从Fragment 中获得FragmentManager。例如:public void confirmFireMissiles() {
DialogFragment newFragment = new FireMissilesDialogFragment();
newFragment.show(getSupportFragmentManager(), "missiles");
}
findFragmentByTag()
.来获得。DialogFragment
类提供你这些易用性,因为它仍可以像一个可嵌入的片段一样。onCreateView()
中加载该布局。public class CustomDialogFragment extends DialogFragment {
/** 系统调用该方法来获得 DialogFragment‘s 布局, 不关心是否是作为一个对话框显示还是可嵌入的碎片
*/ @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout to use as dialog or embedded fragment
return inflater.inflate(R.layout.purchase_items, container, false);
}
/** 系统调用该方法,仅当创建对话框布局时 */
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// 你必须覆盖该方法的唯一原因就是当使用 onCreateView() 来更改任何对话框属性时。例如,对话框默认包含一个标题,但是你的自定义布局或许不需要它。因此你可以移除对话框标题,但是你必须调用父类来获得该对话框。
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
}
public void showDialog() {
FragmentManager fragmentManager = getSupportFragmentManager();
CustomDialogFragment newFragment = new CustomDialogFragment();
if (mIsLargeLayout) {
// The device is using a large layout, so show the fragment as a dialog
newFragment.show(fragmentManager, "dialog");
} else {
// The device is smaller, so show the fragment fullscreen
FragmentTransaction transaction = fragmentManager.beginTransaction();
// For a little polish, specify a transition animation
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
// To make it fullscreen, use the ‘content‘ root view as the container
// for the fragment, which is always the root view for the activity
transaction.add(android.R.id.content, newFragment)
.addToBackStack(null).commit();
}
}
<!-- Default boolean values -->
<resources>
<bool name="large_layout">false</bool>
</resources>
<!-- Large screen boolean values -->
<resources>
<bool name="large_layout">true</bool>
</resources>
boolean mIsLargeLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mIsLargeLayout = getResources().getBoolean(R.bool.large_layout);
}
<activity>配置元素中
应用Theme.Holo.DialogWhenLarge主题:<activity android:theme="@android:style/Theme.Holo.DialogWhenLarge" >
DialogFragment
.中调用dismiss()
来手工的消散你的对话框。DialogFragment
.中实现onDismiss()方法。onDismiss()
。然而,如果你调用Dialog.dismiss()
或DialogFragment.dismiss(),
system 系统调用onDismiss()但不会调用onCancel()。因此当用户点击了你对话框上的posivity按钮来从view上移除该对话框时,通常调用dismiss()
。标签:
原文地址:http://www.cnblogs.com/android-blogs/p/5703292.html