DialogFragment

DialogFragment

从Android 3.0 (API level 11)开始引入,如果想在低于该版本的系统上使用,需用android.support.v4.app.DialogFragment来代替android.app.DialogFragment

0.Dialog的布局结构:

1:Title部分,该部分不是必须的(可以显示,也可以隐藏)
2:内容区域,该部分用来显示你定义的message,list,或者你的自定义布局
3:按钮区域,显示确定取消按钮的(其实中间还有个按钮,不过一般不用)。

1.建立一个普通的Dialog

只需要重写DialogFragment的onCreateDialog方法,返回一个Dialog对象即可
例子:

       @NonNull
       @Override
       public Dialog onCreateDialog(Bundle savedInstanceState) {
           AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
           builder.setTitle("这是个title");
           builder.setMessage("如你所见,这是个message");
           builder.setPositiveButton("确定",null);
           builder.setNegativeButton("取消",null);
          return builder.create();
       }   

2.显示List样式的Dialog

     @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("这是个title");
        builder.setItems(new String[]{"blue","yellow","green"},null);
        return builder.create();
    }

注意:dialog无法同时显示message跟list

你也可以使用builder的setAdapter方法来使用ListAdapter动态显示list

3.显示多选(checkbox)或单选(radiobutton)list的Dialog

可分别调用AlertDialog.Builder的 setMultiChoiceItems()方法,或setSingleChoiceItems()方法
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    mSelectedItems = new ArrayList();  // Where we track the selected items
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Set the dialog title
    builder.setTitle(R.string.pick_toppings)
    // Specify the list array, the items to be selected by default (null for none),
    // and the listener through which to receive callbacks when items are selected
           .setMultiChoiceItems(R.array.toppings, null,
                      new DialogInterface.OnMultiChoiceClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which,
                       boolean isChecked) {
                   if (isChecked) {
                       // If the user checked the item, add it to the selected items
                       mSelectedItems.add(which);
                   } else if (mSelectedItems.contains(which)) {
                       // Else, if the item is already in the array, remove it 
                       mSelectedItems.remove(Integer.valueOf(which));
                   }
               }
           })
    // Set the action buttons
           .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   // User clicked OK, so save the mSelectedItems results somewhere
                   // 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();
}

4.使用自定义布局

 使用AlertDialog.Buidler的setView()方法来设置自定义布局。默认情况下,自定义布局会填满Dialog的窗口,当然,也可以继续设置title和button
 先上效果图:

  layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="64dp"
        android:gravity="center"
        android:textStyle="bold"
        android:textSize="26dp"
        android:background="#FFFFBB33"
        android:text="Android App"
     />
    <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="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="password"/>
</LinearLayout>
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setView(R.layout.login_dialog);
        builder.setNegativeButton("cancle",null);
        builder.setPositiveButton("Sign in",null);
        return builder.create();
    }

5.显示dialog

public void confirmFireMissiles() {
    DialogFragment newFragment = new FireMissilesDialogFragment();
    newFragment.show(getSupportFragmentManager(), "missiles");
}
posted @ 2015-08-19 14:11  隐没  阅读(757)  评论(0编辑  收藏  举报