在开发的过程当中,我们经常会用到各种各样的弹框样式,然而Android自带的弹框样式在UI看来是一个特别丑的存在,所以总是给我们设计各种各样不同样式的弹框,这里就不一一阐述了,这里我们只记录一种公用的弹框样式,也就是仿iOS的样式,先上布局
然后在进行定义之前,我们需要对于弹框的进入和淡出进行一个样式的设置
这里顺便贴上样式的效果
enter_dialog_anim
exit_dialog_anim
接下来就是定义Dialog了,直接上干货
public class NormalDialog extends Dialog implements View.OnClickListener {
private TextView txt_dialog_tip;
private EditText txt_input;
private final String title, tip, negativeName, positiveName, centerName;
private @ColorInt
final int negativeColor, positiveColor, titleColor, centerColor;
private final DialogOnClickListener negativeListener, positiveListener, centerListener;
private final int inputType;//输入框 的输入类型
private final boolean autoClose;//点击确定取消后是否关闭,默认是
private int DIALOG_TYPE = 0;//dialog类型 0:单个确定按钮dialog 1:确定取消dialog 2:单个输入框dialog
public static final int DIALOG_CONFIRM = 0, DIALOG_SELECT = 1, DIALOG_ONE_INPUT = 2;
private final boolean touchOutsideCancel, cancelable;
private NormalDialog(Builder builder) {
super(builder.context);
this.title = builder.title;
this.tip = builder.tip;
this.negativeName = builder.negativeName;
this.positiveName = builder.positiveName;
this.centerName = builder.centerName;
this.negativeListener = builder.negativeListener;
this.positiveListener = builder.positiveListener;
this.centerListener = builder.centerListener;
this.negativeColor = builder.negativeColor;
this.positiveColor = builder.positiveColor;
this.centerColor = builder.centerColor;
this.titleColor = builder.titleColor;
this.inputType = builder.inputType;
this.autoClose = builder.autoClose;
this.DIALOG_TYPE = builder.DIALOG_TYPE;
this.touchOutsideCancel = builder.touchOutsideCancel;
this.cancelable = builder.cancelable;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去掉title
requestWindowFeature(Window.FEATURE_NO_TITLE);
//设置背景透明
getWindow().setBackgroundDrawableResource(android.R.color.transparent);
setContentView(R.layout.dialog_normal);
initView();
}
private void initView() {
//title
TextView txt_dialog_title = findViewById(R.id.txt_dialog_title);
if (title != null) {
txt_dialog_title.setText(title);
}
if (titleColor != 0) txt_dialog_title.setTextColor(titleColor);
//tip
txt_dialog_tip = findViewById(R.id.txt_dialog_tip);
if (tip != null) txt_dialog_tip.setText(tip);
//negativeTX
TextView btn_selectNegative = findViewById(R.id.btn_selectNegative);
if (negativeName != null) btn_selectNegative.setText(negativeName);
if (negativeColor != 0) btn_selectNegative.setTextColor(negativeColor);
btn_selectNegative.setOnClickListener(this);
//centerTX
TextView btn_selectCenter = findViewById(R.id.btn_center);
if (centerName != null) btn_selectCenter.setText(centerName);
if (centerColor != 0) btn_selectCenter.setTextColor(centerColor);
btn_selectCenter.setOnClickListener(this);
//positiveTX
TextView btn_selectPositive = findViewById(R.id.btn_selectPositive);
if (positiveName != null) btn_selectPositive.setText(positiveName);
if (positiveColor != 0) btn_selectPositive.setTextColor(positiveColor);
btn_selectPositive.setOnClickListener(this);
//设置输入框类型
txt_input = findViewById(R.id.txt_input);
if (inputType != 0) txt_input.setInputType(inputType);
setCanceledOnTouchOutside(touchOutsideCancel);
setCancelable(cancelable);
getWindow().setWindowAnimations(R.style.iOSAnimStyle);
switch (DIALOG_TYPE) {
case DIALOG_CONFIRM://单选确定
findViewById(R.id.split_vertical_1).setVisibility(View.GONE);
findViewById(R.id.split_vertical_2).setVisibility(View.GONE);
btn_selectNegative.setVisibility(View.GONE);
btn_selectCenter.setVisibility(View.GONE);
btn_selectPositive.setBackgroundResource(R.drawable.button_dialog_one);
break;
case DIALOG_SELECT://双选
findViewById(R.id.split_vertical_1).setVisibility(View.GONE);
btn_selectCenter.setVisibility(View.GONE);
btn_selectPositive.setBackgroundResource(R.drawable.button_dialog_right);
break;
case DIALOG_ONE_INPUT://带输入框 ,且左右两个按钮
txt_input.setVisibility(View.VISIBLE);
findViewById(R.id.split_vertical_1).setVisibility(View.GONE);
btn_selectCenter.setVisibility(View.GONE);
btn_selectPositive.setBackgroundResource(R.drawable.button_dialog_right);
break;
}
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_selectNegative) {
//取消
if (negativeListener != null) negativeListener.onClick(this, v);
} else if (v.getId() == R.id.btn_selectPositive) {
//确定
if (positiveListener != null) positiveListener.onClick(this, v);
} else if (v.getId() == R.id.btn_center) {
if (centerListener != null) centerListener.onClick(this, v);
}
if (autoClose) dismiss();
}
public void setTip(String tip) {
txt_dialog_tip.setText(tip);
}
/**
* 获取输入框的文本
*
* @return m
*/
public String getInputText() {
return txt_input.getText().toString().trim();
}
public static class Builder {
private Context context;
private String title, tip, negativeName, positiveName, centerName;
private @ColorInt
int negativeColor, positiveColor, titleColor, centerColor;
private DialogOnClickListener negativeListener, positiveListener, centerListener;
private int inputType;
private boolean autoClose = true;
private int DIALOG_TYPE = 0;
private boolean touchOutsideCancel = true, cancelable = true;
public Builder setCanceledOnTouchOutside(boolean canceled) {
touchOutsideCancel = canceled;
return this;
}
public Builder setCancelable(boolean cancelable) {
this.cancelable = cancelable;
return this;
}
/**
* 设置dialog的类型
*
* @param DIALOG_TYPE DIALOG_CONFIRM=0,DIALOG_SELECT=1,DIALOG_ONEINPUT=2
* @return m
*/
public Builder setDialogType(int DIALOG_TYPE) {
this.DIALOG_TYPE = DIALOG_TYPE;
return this;
}
public Builder setTitle(String title) {
this.title = title;
return this;
}
public Builder setTitleColor(@ColorInt int color) {
this.titleColor = color;
return this;
}
public Builder setTip(String tip) {
this.tip = tip;
return this;
}
public Builder setNegativeName(String negativeName) {
this.negativeName = negativeName;
return this;
}
public Builder setNegativeColor(@ColorInt int color) {
this.negativeColor = color;
return this;
}
public Builder setNegativeListener(DialogOnClickListener negativeListener) {
this.negativeListener = negativeListener;
return this;
}
public Builder setPositiveName(String positiveName) {
this.positiveName = positiveName;
return this;
}
public Builder setPositiveColor(@ColorInt int color) {
this.positiveColor = color;
return this;
}
public Builder setPositiveListener(DialogOnClickListener positiveListener) {
this.positiveListener = positiveListener;
return this;
}
public Builder setCenterName(String centerName) {
this.centerName = centerName;
return this;
}
public Builder setCenterColor(@ColorInt int color) {
this.centerColor = color;
return this;
}
public Builder setCenterListener(DialogOnClickListener listener) {
this.centerListener = listener;
return this;
}
/**
* 是否自动关闭(点击确定和取消后)
*
* @param autoClose 默认是
* @return m
*/
public Builder setAutoClose(boolean autoClose) {
this.autoClose = autoClose;
return this;
}
/**
* 设置输入框文本类型
*
* @param type InputType.TYPE_CLASS_TEXT InputType.TYPE_CLASS_NUMBER InputType.TYPE_NUMBER_FLAG_DECIMAL
* InputType.TYPE_TEXT_VARIATION_PASSWORD
* @return m
*/
public Builder setInputType(int type) {
this.inputType = type;
return this;
}
public NormalDialog build(Context context) {
this.context = context;
return new NormalDialog(this);
}
}
}
最后是在我们需要引用的地方进行去引用就大功告成了,也避免了我们在每次需要弹框的时候都要重新定义؏؏☝ᖗ乛◡乛ᖘ☝؏؏
NormalDialog.Builder()
.setDialogType(NormalDialog.DIALOG_SELECT)
.setTitle("hello")
.setTip("确定取消dialog")
.setPositiveListener { _, _ ->
ToastUtils.showMessage(mContext, "确定后可以操作")
}
.build(mContext)
.show()
代码传送门