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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| public class MyAlertDialog extends Dialog { private final MyAlertController mAlert;
public MyAlertDialog(@NonNull Context context, int themeResId) { super(context, themeResId); mAlert = new MyAlertController(this, getWindow()); }
public static class Builder { public final MyAlertController.AlertParams P; public Builder(Context context) { this(context, R.style.dialog); } public Builder(Context context, int themeResId) { P = new MyAlertController.AlertParams(context, themeResId); } public MyAlertDialog create() { final MyAlertDialog dialog = new MyAlertDialog(P.mContext, P.mThemResId); P.apply(dialog.mAlert); dialog.setCancelable(P.mCancelable); if (P.mCancelable) { dialog.setCanceledOnTouchOutside(true); } dialog.setOnCancelListener(P.mOnCancelListener); dialog.setOnDismissListener(P.mOnDismissListener); if (P.mOnKeyListener != null) { dialog.setOnKeyListener(P.mOnKeyListener); } return dialog; } public MyAlertDialog show() { final MyAlertDialog dialog = create(); dialog.show(); return dialog; } /** * 设置布局 * * @param view v * @return b */ public Builder setContentView(View view) { P.mView = view; P.mViewLayoutResId = 0; return this; } public Builder setContentView(int resId) { P.mView = null; P.mViewLayoutResId = resId; return this; } public Builder setCancelable(boolean cancelable) { P.mCancelable = cancelable; return this; } public Builder setOnCancelListener(OnCancelListener onCancelListener) { P.mOnCancelListener = onCancelListener; return this; } public Builder setOnDismissListener(OnDismissListener dismissListener) { P.mOnDismissListener = dismissListener; return this; } public Builder setOnKeyListener(OnKeyListener keyListener) { P.mOnKeyListener = keyListener; return this; } /** * 设置文本 */ public Builder setText(int viewId, CharSequence text) { P.mTextArray.put(viewId, text); return this; } /** * 设置点击事件 */ public Builder setOnclickListener(int view, View.OnClickListener listener) { P.mClickArray.put(view, listener); return this; } /** * 设置其他的参数 */ public Builder fullWidth() { P.mWidth = ViewGroup.LayoutParams.MATCH_PARENT; return this; } /** * 是否有动画 * * @param isAnimation b * @return b */ public Builder fromBottom(boolean isAnimation) { if (isAnimation) { P.mAnimation = R.style.dialog_from_bottom_anim; } P.mGravity = Gravity.BOTTOM; return this; } /** * 设置宽高 * * @param width i * @param height i * @return b */ public Builder setWidthAndHeight(int width, int height) { P.mWidth = width; P.mHeight = height; return this; }
/** * 设置默认动画 * * @return b */ public Builder addDefaultAnimation() { P.mAnimation = R.style.dialog_from_bottom_anim; return this; }
/** * 添加动画 * * @param styleAnimation i * @return b */ public Builder addAnimation(int styleAnimation) { P.mAnimation = styleAnimation; return this; } }
/** * 设置文本 * * @param viewId i * @param valueAt s */ public void setText(int viewId, CharSequence valueAt) { mAlert.setText(viewId,valueAt); }
/** * 设置点击事件 * * @param viewId i * @param listener l */ public void setOnClickListener(int viewId, View.OnClickListener listener) { mAlert.setOnClickListener(viewId,listener);
} public <T extends View> T getView(int viewId) { return mAlert.getView(viewId); } }
|