ZDialogController.java 4.02 KB
Newer Older
霍志良 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
package com.ydl.audioim.widget;

import android.content.Context;
import android.content.DialogInterface;
import android.util.SparseArray;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;

import java.lang.ref.WeakReference;

/**
 * Created on 2018/1/18  11:32
 *
 * @author Z
 * @version : 1.0.0
 */

public class ZDialogController {
    private ZDialog mZDialog;
    private Window mWindow;
    private ZDialogViewHelper mZDialogViewHelper;

    public ZDialogController(ZDialog ZDialog, Window window) {
        mZDialog = ZDialog;
        mWindow = window;
    }

    public ZDialog getZDialog() {
        return mZDialog;
    }

    public Window getWindow() {
        return mWindow;
    }

    public void setZDialogViewHelper(ZDialogViewHelper ZDialogViewHelper) {
        mZDialogViewHelper = ZDialogViewHelper;
    }

    public void setText(int viewId, CharSequence charSequence) {
        mZDialogViewHelper.setText(viewId, charSequence);
    }

    public EditText getEditText(int viewId) {
        return mZDialogViewHelper.getEditText(viewId);
    }

    public View findView(int viewId) {
        return mZDialogViewHelper.findView(viewId);
    }

    public void setOnClickListener(int viewId, View.OnClickListener onClickListener) {
        mZDialogViewHelper.setOnClickListener(viewId, onClickListener);
    }

    /**
     * ZDialog 显示出来可能需要的一些参数
     */
    public static class ZDialogParams {
        public WeakReference<Context> context;
        public int themeResId;// dialog 的style
        public boolean cancelable;
        public View contentView;
        public int contentResId;
        public int defaultWidth = ViewGroup.LayoutParams.WRAP_CONTENT;
        public int defaultHeight = ViewGroup.LayoutParams.WRAP_CONTENT;
        public int direction = Gravity.CENTER;
        public int animation = 0;
        public SparseArray<CharSequence> textArray = new SparseArray<>();
        public SparseArray<View.OnClickListener> clickViewArray = new SparseArray<>();

        public DialogInterface.OnCancelListener onCancelListener;
        public DialogInterface.OnDismissListener onDismissListener;
        public DialogInterface.OnKeyListener onKeyListener;


        public ZDialogParams(Context context, int themeResId) {
            this.context = new WeakReference<>(context);
            this.themeResId = themeResId;
        }

        public void apply(ZDialogController controller) {
            //创建ZDialogViewHelper
            ZDialogViewHelper viewHelper = null;
            if (contentResId != 0) {
                viewHelper = new ZDialogViewHelper(context.get(), contentResId);
            }
            if (contentView != null) {
                viewHelper = new ZDialogViewHelper();
                viewHelper.setContentView(contentView);
            }
            if (viewHelper == null) {
                throw new IllegalArgumentException("please do setContentView() first");
            }

            //添加布局到dialog
            if (viewHelper.getContentView() != null) {
                controller.getZDialog().setContentView(viewHelper.getContentView());
            }

            controller.setZDialogViewHelper(viewHelper);

            for (int i = 0; i < textArray.size(); i++) {
                controller.setText(textArray.keyAt(i), textArray.valueAt(i));
            }

            for (int i = 0; i < clickViewArray.size(); i++) {
                controller.setOnClickListener(clickViewArray.keyAt(i), clickViewArray.valueAt(i));
            }


            Window window = controller.getWindow();
            window.setGravity(direction);
            if (animation != 0) {
                window.setWindowAnimations(animation);
            }
            WindowManager.LayoutParams attributes = window.getAttributes();
            attributes.width = defaultWidth;
            attributes.height = defaultHeight;
            window.setAttributes(attributes);


        }


    }
}