Dialogs (对话框)

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() 中进行必要操作.
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,245评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,749评论 3 391
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,960评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,575评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,668评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,670评论 1 294
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,664评论 3 415
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,422评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,864评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,178评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,340评论 1 344
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,015评论 5 340
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,646评论 3 323
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,265评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,494评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,261评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,206评论 2 352

推荐阅读更多精彩内容

  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,421评论 2 45
  • 打开百度,输入“厦门大学”,你会看到这样一段文字: 厦门大学(Amoy University),简称厦大,是中华人...
    尘与千寻阅读 144评论 0 0
  • 麻烦你先武装自己,再谈保护他人 文/胖头鱼 01 每个人都是有英雄梦的, 就像我,也有的。 我是学社会保障的,在学...
    Susie胖头鱼阅读 255评论 1 1
  • 第四章——程澈 “逸尘,逸尘,你快开门,快叫我先看看你的朝服,走的那天忘记看了。对了,咱俩喝酒的事儿你可千万...
    发绾君心阅读 349评论 0 0
  • 昨天晚上洗了澡提着刚买的麻辣拌往宿舍走,准备享受一番。听到后面有个男的大声的说,我c,那个女长得比我还黑,她tm怎...
    听歌的疯子阅读 198评论 0 0