美文网首页
Dialogs (对话框)

Dialogs (对话框)

作者: 王世军Steven | 来源:发表于2016-11-25 10:41 被阅读68次

    Dialogs

    A dialog is a small window that prompts the user to make a decision or enter additonal infomation .Adialog does not fill the screen and is normally used for modal events that require users to take an action before they can proceed.

    Dialog

    The Dialog class is the base class for dialogs, but you should avoid instantiating Dialog directly. Instead , use one of the following subclasses.

    1, AlertDialog

    A dialog that can show title , up to three buttons,a list of selectable items,or a custom layout.

    2, DatePickerDialog and TimePickerDialog

    A dialog with a pre-defined UI that allows the user to select a date or time.

    以上3个类定义了Dialog的样式和结构,但是最好使用一个个DialogFragment来包裹Dialog对象.
    DialogFragment 类提供了所有Dialog操作需要要的接口.可以创建和管理Dialog样式.使用
    DialogFragment 管理对话框确保他可以正确处理生命周期事件.
    DialogFragment 可以将Dialog作为一个组件嵌入到更大的组件中.就像普通的Fragment那样.
    

    Creating a Dialog Fragment

    You can accomplish a wide variety dialog designs - including custom layouts and those described in the Dialogs design guide - by extending DialogFragment and creating AlertDialog in the onCreateDialog() callback method.

    你可以完成各式各样的对话框设计,包括自定义布局和通过DialogFragment的onCreateDialog()回调函数中来创建.

    A basic AlertDialog that's managed within a DialogFragment

    public static class FireMissilesDialogFragment extends android.support.v4.app.DialogFragment{
            @Override
            public Dialog onCreateDialog(Bundle savedInstanceState) {
                // Use the Builder class for convenient dialog construction
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setMessage("测试对话框")
                        .setPositiveButton("Fire",new DialogInterface.OnClickListener(){
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Log.d("---","===========fire");
                            }
                        })
                        .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Log.d("---","=========== cancel");
                            }
                        });
                // Create the AlertDialog object and return it .
                return builder.create();
            }
        }
    

    直接创建类的对象调用 show() 方法.

    new FireMissilesDialogFragment().show(getSupportFragmentManager(),"111");
    

    Building an Alert Dialog

    组成

    1. Title          : 该部分是可选的,只有当内容区域有内容时才使用Title
    2. Content area   : 可以展示一个消息,列表,或者其他自定义布局 .
    3. Action buttons : 最多3个按钮.(确定,取消,其他)
    
    AlertDialog.Builder 创建对话框.
    // 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 charachteristics
    builder.setMessage("Message")
           .setTitle("Title");
           
    // 3. Get the AlertDialog from create()
    AlertDialog dialog = builder.create();
    
    Adding buttons
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setPositiveButton("Fire",new DialogInterface.OnClickListener(){
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Log.d("---","===========fire");
                            }
                        })
                        .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Log.d("---","=========== cancel");
                            }
                        });
                // Set other Dialog properties
                // Create the AlertDialog object and return it .
                return builder.create();
    
    Adding a list

    A traditional single-choice list(普通列表)

    // 使用 setItems()
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("列表")
                        .setItems(R.array.language, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                // The 'which' argument contains the index position
                                // of selected item .
                            }
                        });
                return builder.create();
    

    A persistent single-choice list(radio buttons)

    方法 : setSingleChoiceItems()

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("Radio")
                        .setSingleChoiceItems(R.array.language, 0, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Log.d("TAG","选择了 : " + which);
                            }
                        });
                return builder.create();
    

    A persistent multiple-choice list(checkboxes)

    方法 : setMultiChoiceItems()

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                return builder.setTitle("CheckBoxes")
                        .setMultiChoiceItems(R.array.language,null, new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                                Log.d("TAG","Index : " + which);
                                Log.d("TAG","OP    : " + isChecked);
                            }
                        }).create();
    
    Create a Custom Layout

    创建自定义布局的Dialog.

    1. 创建自定义布局.
    2. 通过AlertDialog.Builder 的 setView()添加到Dialog中.
    

    res/layout/dialog_signin.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent">
        <ImageView
            android:src="@drawable/logo"
            android:scaleType="centerInside"
            android:background="#FFFFBB33"
            android:contentDescription="LOGO"
            android:layout_width="match_parent"
            android:layout_height="64dp"/>
        <EditText
            android:id="@+id/et_username"
            android:inputType="textWebEmailAddress"
            android:layout_marginTop="16dp"
            android:layout_marginStart="4dp"
            android:layout_marginEnd="4dp"
            android:layout_marginBottom="4dp"
            android:hint="username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <EditText
            android:id="@+id/et_password"
            android:inputType="textPassword"
            android:layout_marginTop="16dp"
            android:layout_marginStart="4dp"
            android:layout_marginEnd="4dp"
            android:layout_marginBottom="4dp"
            android:hint="password"
            android:fontFamily="sans-serif"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    
    </LinearLayout>
    

    Tip : 设置 "textPassword" 会改变 fontFamily 为 monospace,
    可以通过设置 "sans-serif" 来保持两个框一致.

        public static class CustomLayoutDialog extends DialogFragment{
            @NonNull
            @Override
            public Dialog onCreateDialog(Bundle savedInstanceState) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    
                // Get the layout inflater
                LayoutInflater inflater = getActivity().getLayoutInflater();
    
                // Inflate and set 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));
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
    
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
    
                    }
                });
                return builder.create();
            }
        }
    

    Tip : 可以使用Activity来实现自定义Dialog的功能.
    只需要将Activity主题设置如下 :

    <activity android:theme="@android:style/Theme.Holo.Dialog">
    
    Passing Events Back to the Dialog's Host
       public static class NoticeDialogFragment extends DialogFragment{
            /* The activity that creates an instance of this dialog fragment
             * must implement this interface inorder to receive event callbacks.
             * Each method passes the DialogFragment in case the host needs to query it.
             */
            public interface NoticeDialogListener {
                public void onDialogPositiveClick(DialogFragment dialog);
                public void onDialogNegativeClick(DialogFragment dialog);
            }
            // Use this instance of the interface to deliver action events
            NoticeDialogListener mListener;
            // Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
    
    
            @Override
            public void onAttach(Activity activity) {
                super.onAttach(activity);
                // Verify that the host activity implements the callback interface
                try{
                    mListener = (NoticeDialogListener) activity;
                }catch (ClassCastException e){
                    throw new ClassCastException(activity.toString()+"must implement NoticeDialogListener");
                }
            }
        }
    

    Showing a Dialog

    1. 创建一个DialogFragment实例对象 
    2. 调用 show() 
    3. FragmentManager获取方式 :  
        3.1 getSupportFragmentManager() from the fragmentActivity.
        3.2 getFragmentManager() from a Fragment .
    

    Showing a Dialog Fullscreen or as an Embedded Fragment

    DialogFragment 可以同时使用一下两种情况:

    1. 显示为Dialog.
    2. 显示为Fragment.
    

    注意 : 在这种情况下不能使用AlertDialog.Builder 或者其他的Dialog对象创建

    需要在onCreateView() 中加载布局文件.

    一下代码实现了在小屏幕上显示Fragment在大屏幕上显示Dialog

     public static class CustomEmDialogFragment extends DialogFragment {
            /**
            * The system calls this to get the DialogFragment's layout,regardless
            * of whether it's being displayed as a dialog or an embedded fragment.
            * */
            @Nullable
            @Override
            public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
                // Inflate the layout to use as dialog or embedded fragment.
                return inflater.inflate(R.layout.dialog_signin,container,false);
            }
            /** The system calls this only when creating the layout in a dialog */
            @NonNull
            @Override
            public Dialog onCreateDialog(Bundle savedInstanceState) {
                // The only reason you might override this method when using onCreateView() is
                // to modify any dialog characteristics. For example , the dialog includes a
                // title by default , but you custom layout might not need it. So here you can
                // remove the dialog title , but you must call the superclass to get the Dialog.
                Dialog dialog = super.onCreateDialog(savedInstanceState);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                return dialog;
            }
        }
        private boolean mIsLargeLayout = false;
        public void showDialog(){
            FragmentManager fragmentManager = getSupportFragmentManager();
            CustomLayoutDialog newFragment = new CustomLayoutDialog();
            if (mIsLargeLayout){
                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);
                transaction.add(android.R.id.content,newFragment)
                        .addToBackStack(null).commit();
            }
        }
    

    res/values/bools.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <bool name="large_layout">false</bool>
    </resources>
    

    res/values-large/bools.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <bool name="large_layout">true</bool>
    </resources>
    

    Dismissing a Dialog

    1. 每次onCancel() 被调用 都会调用 onDismiss().
    2. Dialog.dismiss() 和 DialogFragment.dismiss() 会调用onDismiss .
       不会调用onCancel().
    3. 可以在onDismiss() 中进行必要操作.
    

    相关文章

      网友评论

          本文标题:Dialogs (对话框)

          本文链接:https://www.haomeiwen.com/subject/dliipttx.html