banner
NEWS LETTER

Builder模式创建万能Dialog

Scroll down

资源准备,先创建dialogTheme和dialogAnimation

  • 在res/values/styles.xml中创建
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<style name="dialog" parent="Theme.AppCompat.Dialog">
<item name="android:windowFrame">@null</item>
<!--边框-->
<item name="android:windowIsFloating">true</item>
<!--是否浮现在activity之上-->
<item name="android:windowIsTranslucent">true</item>
<!--无标题-->
<item name="android:windowBackground">@android:color/transparent</item>
<!--背景透明-->
<item name="android:backgroundDimEnabled">true</item>
<!--模糊-->
<item name="android:windowNoTitle">true</item>
</style>

<style name="dialog_from_bottom_anim">
<item name="android:windowEnterAnimation">@anim/dialog_from_bottom_anim_in</item>
<item name="android:windowExitAnimation">@anim/dialog_from_bottom_anim_out</item>
</style>
  • 在res/anim/中创建
    in
1
2
3
4
5
6
7
8
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="400"
android:fromXDelta="0"
android:fromYDelta="1000"
android:toXDelta="0"
android:toYDelta="0" />
</set>
out
1
2
3
4
5
6
7
8
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="400"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="0"
android:toYDelta="1000" />
</set>

创建MyAlertDialog.class

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);
}
}

创建MyAlertController.class

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
class MyAlertController {
private final MyAlertDialog myAlertDialog;
private final Window mWindow;
private MyDialogViewHelper viewHelper;

public MyAlertController(MyAlertDialog dialog, Window window) {
this.myAlertDialog = dialog;
this.mWindow = window;
}

/**
* 获取dialog
*
* @return d
*/
public MyAlertDialog getMyAlertDialog() {
return myAlertDialog;
}

/**
* 获取dialog的window
*
* @return w
*/
public Window getMyWindow() {
return mWindow;
}

public static class AlertParams {
public Context mContext;
public int mThemResId;
//点击空白是否可以取消
public boolean mCancelable = true;
//点击监听
public DialogInterface.OnCancelListener mOnCancelListener;
//消失监听
public DialogInterface.OnDismissListener mOnDismissListener;
//按键监听
public DialogInterface.OnKeyListener mOnKeyListener;
//布局
public View mView;
//布局layout id
public int mViewLayoutResId;
//存放字
public SparseArray<CharSequence> mTextArray = new SparseArray<>();
//存放点击事件
public SparseArray<View.OnClickListener> mClickArray = new SparseArray<>();
//宽度
public int mWidth = ViewGroup.LayoutParams.WRAP_CONTENT;
//高度
public int mHeight = ViewGroup.LayoutParams.WRAP_CONTENT;
//动画
public int mAnimation = 0;
//位置
public int mGravity = Gravity.CENTER;

public AlertParams(Context context, int themeResId) {
this.mContext = context;
this.mThemResId = themeResId;
}

/**
* 绑定和设置参数
*
* @param mAlert c
*/
public void apply(MyAlertController mAlert) {
//设置布局 DialogViewHelper
MyDialogViewHelper viewHelper = null;
if (mViewLayoutResId != 0) {
viewHelper = new MyDialogViewHelper(mContext, mViewLayoutResId);
}
if (mView != null) {
viewHelper = new MyDialogViewHelper();
viewHelper.setContentView(mView);
}
if (viewHelper == null) {
throw new IllegalArgumentException("请设置布局setContentView()");
}
//给dialog设置布局
mAlert.getMyAlertDialog().setContentView(viewHelper.getContentView());
//设置controller的辅助类
mAlert.setViewHelper(viewHelper);
//设置文本
int textArraySize = mTextArray.size();
for (int i = 0; i < textArraySize; i++) {
viewHelper.setText(mTextArray.keyAt(i), mTextArray.valueAt(i));
}
//设置点击事件
int clickArraySize = mClickArray.size();
for (int i = 0; i < clickArraySize; i++) {
viewHelper.setOnClickListener(mClickArray.keyAt(i), mClickArray.valueAt(i));
}
//设置自定义的一些效果
Window window = mAlert.getMyWindow();
//设置位置
window.setGravity(mGravity);
//设置动画
if (mAnimation != 0) {
window.setWindowAnimations(mAnimation);
}
//设置宽高
WindowManager.LayoutParams params = window.getAttributes();
params.width = mWidth;
params.height = mHeight;
window.setAttributes(params);
}
}

public void setViewHelper(MyDialogViewHelper viewHelper) {
this.viewHelper = viewHelper;
}

/**
* 设置文本
*
* @param viewId i
* @param valueAt s
*/
public void setText(int viewId, CharSequence valueAt) {
viewHelper.setText(viewId, valueAt);
}

/**
* 设置点击事件
*
* @param viewId i
* @param listener l
*/
public void setOnClickListener(int viewId, View.OnClickListener listener) {
viewHelper.setOnClickListener(viewId, listener);

}

public <T extends View> T getView(int viewId) {
return viewHelper.getView(viewId);
}

}

创建MyDialogViewHelper.class

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
class MyDialogViewHelper {
private View mContentView = null;
/**
* 防止内存泄露
*/
private final SparseArray<WeakReference<View>> mViews;

public MyDialogViewHelper(Context mContext, int layoutResId) {
this();
mContentView = LayoutInflater.from(mContext).inflate(layoutResId, null);
}

public MyDialogViewHelper() {
mViews = new SparseArray<>();
}

public void setContentView(View mView) {
this.mContentView = mView;
}

/**
* 设置文本
*
* @param viewId v
* @param valueAt t
*/
public void setText(int viewId, CharSequence valueAt) {
TextView textView = getView(viewId);
if (textView != null) {
textView.setText(valueAt);
}
}

/**
* 设置点击事件
*
* @param viewId v
* @param listener l
*/
public void setOnClickListener(int viewId, View.OnClickListener listener) {
View view = getView(viewId);
if (view != null) {
view.setOnClickListener(listener);
}

}

public View getContentView() {
return mContentView;
}

@SuppressWarnings("unchecked")
public <T extends View> T getView(int viewId) {
WeakReference<View> weakReference = mViews.get(viewId);
View view = null;
if (weakReference != null) {
view = weakReference.get();
}
if (view == null) {
view = mContentView.findViewById(viewId);
if (view != null) {
mViews.put(viewId, new WeakReference<View>(view));
}
}
return (T) view;
}
}

使用方法

1
2
3
4
5
6
7
8
9
10
MyAlertDialog dialog = new MyAlertDialog.Builder(this)
.setContentView(R.layout.dialog_xxx)
.fromBottom(true).fullWidth().show();
EditText editText = dialog.getView(R.id.edit_query);
dialog.setOnClickListener(R.id.fasong, new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(XActivity.this, editText.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
其他文章
目录导航 置顶
  1. 1. 资源准备,先创建dialogTheme和dialogAnimation
  2. 2. 创建MyAlertDialog.class
  3. 3. 创建MyAlertController.class
  4. 4. 创建MyDialogViewHelper.class
  5. 5. 使用方法
请输入关键词进行搜索