美文网首页android基础知识
使用 DialogFragment 实现弹窗1-基本用法

使用 DialogFragment 实现弹窗1-基本用法

作者: 111123123 | 来源:发表于2017-09-04 21:51 被阅读0次

    定义

    DiaogFragment 是一个用来显示对话框窗体的 Fragment,浮在他所在的 Activity 之上。它包含一个 Dialog 对象,根据Fragment的状态来控制Dialog的显示,并且应使用他的API来管理 Dialog,而不是直接调用 Dialog 。

    例:在手机配置变化导致 Activity 需要重新创建时,例如旋转屏幕,基于 DialogFragment 的对话框将会由 FragmentManager 自动重建,然而基于 Dialog 实现的对话框却没有这样的能力

    使用方法

    1、继承 DiaogFragment
    2、实现 onCreateView 或者 onCreateDialog 方法来指定布局
    3、就像普通Fragment一样在宿主调用显示

    生命周期

    DialogFragment 做了很多事情来尽量通过 Fragment 的生命周期来控制它自身,而不是采用 Dialog 的生命周期。注意,通常对话框是自治实体 -- 他们有自己的 window,接受输入事件,甚至自己决定什么时候消失(一般是后退事件)。

    DialogFragment 需要尽量让 Fragment 和 Dialog 保持一致的状态,因此他会监测 Dialog 的 dismiss 事件,当事件触发的时候,他会移除掉它自己的状态。这意味着,你需要使用 show(FragmentManager, String) 和 show(FragmentTransaction, String) 来给你的 UI 添加 DialogFragment,因为这两个方法会在 DialogFrament 移除自己和Dialog 的 dismiss 事件之间建立关联。

    示例1 - 一般 Dialog

    1、DialogFragment 定义

    public static class MyDialogFragment extends DialogFragment {
        int mNum;
    
        /**
         * Create a new instance of MyDialogFragment, providing "num"
         * as an argument.
         */
        static MyDialogFragment newInstance(int num) {
            MyDialogFragment f = new MyDialogFragment();
    
            // Supply num input as an argument.
            Bundle args = new Bundle();
            args.putInt("num", num);
            f.setArguments(args);
    
            return f;
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mNum = getArguments().getInt("num");
    
            // Pick a style based on the num.
            int style = DialogFragment.STYLE_NORMAL, theme = 0;
            switch ((mNum-1)%6) {
                case 1: style = DialogFragment.STYLE_NO_TITLE; break;
                case 2: style = DialogFragment.STYLE_NO_FRAME; break;
                case 3: style = DialogFragment.STYLE_NO_INPUT; break;
                case 4: style = DialogFragment.STYLE_NORMAL; break;
                case 5: style = DialogFragment.STYLE_NORMAL; break;
                case 6: style = DialogFragment.STYLE_NO_TITLE; break;
                case 7: style = DialogFragment.STYLE_NO_FRAME; break;
                case 8: style = DialogFragment.STYLE_NORMAL; break;
            }
            switch ((mNum-1)%6) {
                case 4: theme = android.R.style.Theme_Holo; break;
                case 5: theme = android.R.style.Theme_Holo_Light_Dialog; break;
                case 6: theme = android.R.style.Theme_Holo_Light; break;
                case 7: theme = android.R.style.Theme_Holo_Light_Panel; break;
                case 8: theme = android.R.style.Theme_Holo_Light; break;
            }
            setStyle(style, theme);
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.fragment_dialog, container, false);
            View tv = v.findViewById(R.id.text);
            ((TextView)tv).setText("Dialog #" + mNum + ": using style "
                    + getNameForNum(mNum));
    
            // Watch for button clicks.
            Button button = (Button)v.findViewById(R.id.show);
            button.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    // When button is clicked, call up to owning activity.
                    ((FragmentDialog)getActivity()).showDialog();
                }
            });
    
            return v;
        }
    }
    

    2、调用

    void showDialog() {
        mStackLevel++;
    
        // DialogFragment.show() will take care of adding the fragment
        // in a transaction.  We also want to remove any currently showing
        // dialog, so make our own transaction and take care of that here.
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        Fragment prev = getFragmentManager().findFragmentByTag("dialog");
        if (prev != null) {
            ft.remove(prev);
        }
        ft.addToBackStack(null);
    
        // Create and show the dialog.
        DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel);
        newFragment.show(ft, "dialog");
    }
    

    示例2 - Alert Dialog

    可以通过实现 onCreateDialog(Bundle),直接来实现自定义 Dialog

    public static class MyAlertDialogFragment extends DialogFragment {
    
        public static MyAlertDialogFragment newInstance(int title) {
            MyAlertDialogFragment frag = new MyAlertDialogFragment();
            Bundle args = new Bundle();
            args.putInt("title", title);
            frag.setArguments(args);
            return frag;
        }
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            int title = getArguments().getInt("title");
    
            return new AlertDialog.Builder(getActivity())
                    .setIcon(R.drawable.alert_dialog_icon)
                    .setTitle(title)
                    .setPositiveButton(R.string.alert_dialog_ok,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                ((FragmentAlertDialog)getActivity()).doPositiveClick();
                            }
                        }
                    )
                    .setNegativeButton(R.string.alert_dialog_cancel,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                ((FragmentAlertDialog)getActivity()).doNegativeClick();
                            }
                        }
                    )
                    .create();
        }
    }
    
    void showDialog() {
        DialogFragment newFragment = MyAlertDialogFragment.newInstance(
                R.string.alert_dialog_two_buttons_title);
        newFragment.show(getFragmentManager(), "dialog");
    }
    
    public void doPositiveClick() {
        // Do stuff here.
        Log.i("FragmentAlertDialog", "Positive click!");
    }
    
    public void doNegativeClick() {
        // Do stuff here.
        Log.i("FragmentAlertDialog", "Negative click!");
    }
    

    参考文档:

    https://developer.android.google.cn/reference/android/app/DialogFragment.html

    相关文章

      网友评论

        本文标题:使用 DialogFragment 实现弹窗1-基本用法

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