一个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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package com.alan.dialogdemo;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
/**
* @author Alan
* @date 创建时间:2016年7月25日 下午2:48:05
* @version 0.5
* @Description 通用提示框
* @return
* */
@SuppressLint("NewApi")
public class CommonDialog extends Dialog{
public CommonDialog(Context context){
super(context);
}
public interface OnCommonDialogClickListener{
public void confirm();
public void cancel();
}
public static class Builder{
private Context mContext;
/** 标题*/
private TextView tvTitle;
/** 提示内容*/
private TextView tvContent;
/** 确定按钮*/
private Button btnConfire;
/** 取消按钮*/
private Button btnCancel;
/** 提示标题图片资源*/
private String mTitleStr;
/** 提示内容文本*/
private String mContentStr = "";
/** 确定按钮文本*/
private String mConfireStr = "";
/** 取消按钮文本*/
private String mCancelStr = "";
/** 点击外面是否取消,默认不能取消*/
private boolean mCancelableFlag = false;
/** 操作回调*/
private static OnCommonDialogClickListener mListener;
public Builder(Context context){
this.mContext = context;
this.mConfireStr = "确定";
this.mCancelStr = "取消";
}
public Builder(Context context,String title,String content,OnCommonDialogClickListener listener){
}
public CommonDialog.Builder setOnCommonDialogClick(OnCommonDialogClickListener listener){
mListener = listener;
return this;
}
public CommonDialog.Builder setTitle(String title) {
mTitleStr = title;
return this;
}
public CommonDialog.Builder setContent(String content) {
mContentStr = content;
return this;
}
public CommonDialog.Builder setCancelable(boolean flag){
mCancelableFlag = flag;
return this;
}
public CommonDialog create(){
final CommonDialog dialog = new CommonDialog(mContext);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.setCancelable(mCancelableFlag);
// 根布局
LinearLayout mRootView = new LinearLayout(mContext);
mRootView.setOrientation(LinearLayout.VERTICAL);
mRootView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
mRootView.setBackgroundColor(Color.WHITE);
// 标题布局
LinearLayout mTitleLayout = new LinearLayout(mContext);
// 标题布局参数
LinearLayout.LayoutParams mTitleLayoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,dipTopx(mContext, 50));
mTitleLayoutParams.gravity = Gravity.CENTER;
mTitleLayout.setLayoutParams(mTitleLayoutParams);
mTitleLayout.setBackgroundColor(Color.DKGRAY);
mTitleLayout.setGravity(Gravity.CENTER);
// 标题文本
TextView mTitle = new TextView(mContext);
LinearLayout.LayoutParams mTitleParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
mTitleParams.gravity = Gravity.CENTER;
mTitle.setLayoutParams(mTitleParams);
mTitle.setPadding(dipTopx(mContext, 10), dipTopx(mContext, 10), dipTopx(mContext, 10), dipTopx(mContext, 10));
if(TextUtils.isEmpty(mTitleStr)){
throw new RuntimeException("请设置标题 setTitle");
}
mTitle.setText(mTitleStr);
mTitle.setTextSize(25);
mTitle.setGravity(Gravity.CENTER);
mTitle.setTextColor(Color.WHITE);
mTitleLayout.addView(mTitle);
mRootView.addView(mTitleLayout);
// 提示内容
TextView mContent = new TextView(mContext);
LinearLayout.LayoutParams mContentParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
mContentParams.setMargins(0, dipTopx(mContext, 40), 0, dipTopx(mContext, 40));
mContentParams.gravity = Gravity.CENTER;
mContent.setLayoutParams(mContentParams);
mContent.setGravity(Gravity.CENTER);
if(TextUtils.isEmpty(mContentStr)){
throw new RuntimeException("请设置提示内容 setContent");
}
mContent.setText(mContentStr);
mContent.setTextColor(Color.BLACK);
mContent.setTextSize(18);
mRootView.addView(mContent);
// 按钮布局
LinearLayout mBtnLayout = new LinearLayout(mContext);
mBtnLayout.setGravity(Gravity.CENTER_HORIZONTAL);
LinearLayout.LayoutParams mBtnLayoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
mBtnLayoutParams.setMargins(0, 0, 0, dipTopx(mContext, 10));
mBtnLayout.setLayoutParams(mBtnLayoutParams);
// 取消按钮
Button mCancelBtn = new Button(mContext);
LinearLayout.LayoutParams mCancelBtnParams = new LinearLayout.LayoutParams(dipTopx(mContext, 150), LayoutParams.WRAP_CONTENT);
mCancelBtnParams.leftMargin = (dipTopx(mContext, 10));
mCancelBtn.setLayoutParams(mCancelBtnParams);
mCancelBtn.setText(mCancelStr);
mCancelBtn.setPadding(dipTopx(mContext, 10), dipTopx(mContext, 5), dipTopx(mContext, 10), dipTopx(mContext, 5));
// 取消按钮点击事件
mCancelBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mListener != null){
mListener.cancel();
dialog.dismiss();
}
}
});
// 确定按钮
Button mConfireBtn = new Button(mContext);
LinearLayout.LayoutParams mConfireBtnParams = new LinearLayout.LayoutParams(dipTopx(mContext, 150), LayoutParams.WRAP_CONTENT);
mConfireBtnParams.rightMargin = (dipTopx(mContext, 10));
mConfireBtn.setLayoutParams(mConfireBtnParams);
mConfireBtn.setText(mConfireStr);
mConfireBtn.setPadding(dipTopx(mContext, 10), dipTopx(mContext, 5), dipTopx(mContext, 10), dipTopx(mContext, 5));
// 确定按钮点击事件
mConfireBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mListener != null){
mListener.confirm();
dialog.dismiss();
}
}
});
mBtnLayout.addView(mCancelBtn);
mBtnLayout.addView(mConfireBtn);
mRootView.addView(mBtnLayout);
dialog.setContentView(mRootView);
return dialog;
}
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public int dipTopx(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
*/
public int pxTodip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
}
}
使用代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
new CommonDialog.Builder(MainActivity.this)
.setTitle("支付未完成")
.setContent("你想要取消这次支付吗?")
.setOnCommonDialogClick(new OnCommonDialogClickListener() {
@Override
public void confirm() {
Toast.makeText(MainActivity.this, "你点击了确定", Toast.LENGTH_SHORT).show();
}
@Override
public void cancel() {
Toast.makeText(MainActivity.this, "你点击了取消", Toast.LENGTH_SHORT).show();
}
})
.create()
.show();
最后
根据自己项目的需求更改样式,自行扩展
本文由作者按照
CC BY 4.0
进行授权