创建账号差不多一年,想想还没有写过什么内容。刚好最近想沉淀一下自己,总结一下自己工作以来的所接触和用过的有价值的东西。既是总结自己,也希望分享给大家。后面我会陆续更新我的博客
所以,第一篇文章从什么开始呢,那就从封装一套通用的Dialog提示框开始吧。这套dialog是我在工作中自己用建造者模式封装的,使用起来很方便,今天分享给大家。
如果对建造者模式不太了解的小伙伴请自行查阅资料,此处不进行进一步探讨。
先看看效果图吧。
---- 如何使用 ----
准备步骤(具体代码见后文"源代码"):
1、创建布局文件:dialog_layout.xml
2、创建圆角背景:dialog_round_bg.xml
3、如果需要我图上默认的icon,这是我直接从google发布的material-design-icons-master中拷贝的:
4、创建自定义dalog类:BuilderModeDialog.class
开始使用:
---- 方法说明 ----
---- 源代码 ----
1、dialog圆角背景:dialog_round_bg.xml
android:shape="rectangle">
android:radius="40dp"/>
2、布局文件:dialog_layout.xml
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/dialog_round_bg"
android:orientation="vertical">
android:id="@+id/title_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="15dp"
android:paddingBottom="15dp">
android:id="@+id/dialog_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="这是提示标题"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:layout_centerVertical="true"
android:textSize="16sp"
android:textColor="#000000"/>
android:id="@+id/dialog_icon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:src="@mipmap/dialog_icon" />
android:id="@+id/dialog_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="30dp"
android:paddingBottom="30dp"
android:text="这是提示内容"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:textSize="14sp"
android:textColor="#000000"/>
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#f3f3f3" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:id="@+id/dialog_left_bt"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center"
android:text="左边"
android:textSize="14sp"
android:visibility="gone"/>
android:id="@+id/view_1"
android:layout_width="0.5dp"
android:layout_height="match_parent"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:background="#f3f3f3" />
android:id="@+id/dialog_mid_bt"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center"
android:text="中间"
android:textSize="14sp"
android:visibility="gone"/>
android:layout_width="0.5dp"
android:layout_height="match_parent"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:background="#f3f3f3" />
android:id="@+id/dialog_right_bt"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:gravity="center"
android:text="右边"
android:textSize="14sp"
android:visibility="gone"/>
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#f3f3f3" />
3、自定义Dialog类:BuilderModeDialog.class
package com.htf.widget.widget;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.htf.widget.R;
public class BuilderModeDialogextends Dialogimplements View.OnClickListener {
private RelativeLayouttitleLayout;
private ImageViewdialogIcon;//icon
private TextViewdialogTitle;//标题
private TextViewdialogContent;//提示内容
private TextViewdialogLeftBt;//左侧按钮
private TextViewdialogMidBt;//中间按钮
private TextViewdialogRightBt;//右侧按钮
private ViewbtCenterLine;//按钮分隔线
/**
* 按钮个数-默认
*/
public static final int BUTTON_MODE_DEFAULT =0;
/**
* 按钮个数-1
*/
public static final int BUTTON_MODE_MID =1;
/**
* 按钮个数-2
*/
public static final int BUTTON_MODE_LEFT_RIGHT =2;
/**
* 按钮个数-3
*/
public static final int BUTTON_MODE_LEFT_MID_RIGHT =3;
/**
* 存放设置的参数
*/
public DialogParamparam;
private BuilderModeDialog(Builder builder) {
super(builder.context);
param =new DialogParam();
param.title = builder.title;
param.msg = builder.msg;
param.icon = builder.icon;
param.leftText = builder.leftText;
param.midText = builder.midText;
param.rightText = builder.rightText;
param.buttonMode = builder.buttonMode;
param.titleColorInt = builder.titleColorInt;
param.msgColorInt = builder.msgColorInt;
param.leftColorInt = builder.leftColorInt;
param.midColorInt = builder.midColorInt;
param.rightColorInt = builder.rightColorInt;
param.titleColorHex = builder.titleColorHex;
param.msgColorHex = builder.msgColorHex;
param.leftColorHex = builder.leftColorHex;
param.midColorHex = builder.midColorHex;
param.rightColorHex = builder.rightColorHex;
param.isShowTitle = builder.isShowTitle;
param.isShowContent = builder.isShowContent;
param.canceledOnTouchOutside = builder.canceledOnTouchOutside;
param.leftListener = builder.leftListener;
param.midListener = builder.midListener;
param.rightListener = builder.rightListener;
}
public BuilderModeDialog(@NonNull Context context) {
super(context);
}
public BuilderModeDialog(@NonNull Context context, int themeResId) {
super(context, themeResId);
}
protected BuilderModeDialog(@NonNull Context context, boolean cancelable, @NonNull DialogInterface.OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_layout);
setCanceledOnTouchOutside(param.canceledOnTouchOutside);
initView();
}
/**
* 初始化view
*/
private void initView() {
titleLayout = (RelativeLayout) findViewById(R.id.title_layout);
dialogIcon = (ImageView) findViewById(R.id.dialog_icon);
dialogTitle = (TextView) findViewById(R.id.dialog_title);
dialogContent = (TextView) findViewById(R.id.dialog_content);
dialogLeftBt = (TextView) findViewById(R.id.dialog_left_bt);
dialogMidBt = (TextView) findViewById(R.id.dialog_mid_bt);
dialogRightBt = (TextView) findViewById(R.id.dialog_right_bt);
btCenterLine = (View) findViewById(R.id.view_1);
if (!param.isShowTitle) {
titleLayout.setVisibility(View.GONE);
}
if (!param.isShowContent) {
dialogContent.setVisibility(View.GONE);
}
if (param.icon !=0) {
dialogIcon.setImageResource(param.icon);
}
if (!TextUtils.isEmpty(param.rightColorHex)) {
try {
dialogRightBt.setTextColor(Color.parseColor(param.rightColorHex));
}catch (Exception e) {
e.printStackTrace();
}
}
switch (param.buttonMode) {
case BUTTON_MODE_DEFAULT:
//默认left+right
showButton(dialogLeftBt);
showButton(dialogRightBt);
btCenterLine.setVisibility(View.GONE);
break;
case BUTTON_MODE_MID:
showButton(dialogMidBt);
break;
case BUTTON_MODE_LEFT_RIGHT:
showButton(dialogLeftBt);
showButton(dialogRightBt);
btCenterLine.setVisibility(View.GONE);
break;
case BUTTON_MODE_LEFT_MID_RIGHT:
showButton(dialogLeftBt);
showButton(dialogMidBt);
showButton(dialogRightBt);
break;
}
if (!TextUtils.isEmpty(param.title)) {
dialogTitle.setText(param.title);
}
if (!TextUtils.isEmpty(param.msg)) {
dialogContent.setText(param.msg);
}
if (!TextUtils.isEmpty(param.leftText)) {
dialogLeftBt.setText(param.leftText);
}
if (!TextUtils.isEmpty(param.midText)) {
dialogMidBt.setText(param.midText);
}
if (!TextUtils.isEmpty(param.rightText)) {
dialogRightBt.setText(param.rightText);
}
//int型颜色
if (param.titleColorInt !=0) {
dialogTitle.setTextColor(param.titleColorInt);
}
if (param.msgColorInt !=0) {
dialogContent.setTextColor(param.msgColorInt);
}
if (param.leftColorInt !=0) {
dialogLeftBt.setTextColor(param.leftColorInt);
}
if (param.midColorInt !=0) {
dialogMidBt.setTextColor(param.midColorInt);
}
if (param.rightColorInt !=0) {
dialogRightBt.setTextColor(param.rightColorInt);
}
//16进制颜色
if (!TextUtils.isEmpty(param.titleColorHex)) {
try {
dialogTitle.setTextColor(Color.parseColor(param.titleColorHex));
}catch (Exception e) {
e.printStackTrace();
}
}
if (!TextUtils.isEmpty(param.msgColorHex)) {
try {
dialogContent.setTextColor(Color.parseColor(param.msgColorHex));
}catch (Exception e) {
e.printStackTrace();
}
}
if (!TextUtils.isEmpty(param.leftColorHex)) {
try {
dialogLeftBt.setTextColor(Color.parseColor(param.leftColorHex));
}catch (Exception e) {
e.printStackTrace();
}
}
if (!TextUtils.isEmpty(param.midColorHex)) {
try {
dialogMidBt.setTextColor(Color.parseColor(param.midColorHex));
}catch (Exception e) {
e.printStackTrace();
}
}
if (!TextUtils.isEmpty(param.rightColorHex)) {
try {
dialogRightBt.setTextColor(Color.parseColor(param.rightColorHex));
}catch (Exception e) {
e.printStackTrace();
}
}
}
private void showButton(TextView tv) {
tv.setVisibility(View.VISIBLE);
tv.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.dialog_left_bt:
if (param.leftListener !=null) {
param.leftListener.onDialogClick();
this.dismiss();
}
break;
case R.id.dialog_mid_bt:
if (param.midListener !=null) {
param.midListener.onDialogClick();
this.dismiss();
}
break;
case R.id.dialog_right_bt:
if (param.rightListener !=null) {
param.rightListener.onDialogClick();
this.dismiss();
}
break;
}
}
/**
* 设置的参数。通过该实体保存建造者收到的参数
*/
public class DialogParam {
public Stringtitle;
public Stringmsg;
public int icon;
public StringleftText;
public StringmidText;
public StringrightText;
public int buttonMode;
public int titleColorInt;
public int msgColorInt;
public int leftColorInt;
public int midColorInt;
public int rightColorInt;
public StringtitleColorHex;
public StringmsgColorHex;
public StringleftColorHex;
public StringmidColorHex;
public StringrightColorHex;
public boolean canceledOnTouchOutside;
public boolean isShowTitle;
public boolean isShowContent;
public OnDialogClickListenerleftListener;
public OnDialogClickListenermidListener;
public OnDialogClickListenerrightListener;
}
/**
* 建造者。所有设置参数都通过建造者设置
*/
public static class Builder {
private Contextcontext;//上下文
private Stringtitle;//标题
private Stringmsg;//内容
private int icon;//图标
private StringleftText;//左侧按钮文字
private StringmidText;//中间按钮文字
private StringrightText;//右侧按钮文字
private int buttonMode;//按钮格式。默认双按钮
private int titleColorInt;//标题文字颜色
private int msgColorInt;//内容文字颜色
private int leftColorInt;//左侧按钮文字颜色
private int midColorInt;//中间按钮文字颜色
private int rightColorInt;//右侧按钮文字颜色
private StringtitleColorHex;//标题文字颜色-16进制。如"#DC143C"
private StringmsgColorHex;//内容文字颜色-16进制
private StringleftColorHex;//左侧按钮文字颜色-16进制
private StringmidColorHex;//中间按钮文字颜色-16进制
private StringrightColorHex;//右侧按钮文字颜色-16进制
private boolean isShowTitle =true;//是否显示标题
private boolean isShowContent =true;//是否显示内容
private boolean canceledOnTouchOutside =false;//是否点击dialog外部取消dialog,默认为false
private OnDialogClickListenerleftListener;//左侧按钮点击事件
private OnDialogClickListenermidListener;//中间按钮点击事件
private OnDialogClickListenerrightListener;//右侧按钮点击事件
public Builder(Context context) {
this.context = context;
}
public BuildersetTitle(String title) {
this.title = title;
return this;
}
public BuildersetMsg(String msg) {
this.msg = msg;
return this;
}
public BuildersetIcon(int icon) {
this.icon = icon;
return this;
}
public BuildersetLeftText(String leftText) {
this.leftText = leftText;
return this;
}
public BuildersetMidText(String midText) {
this.midText = midText;
return this;
}
public BuildersetRightText(String rightText) {
this.rightText = rightText;
return this;
}
public BuildersetButtonMode(int buttonMode) {
this.buttonMode = buttonMode;
return this;
}
public BuildersetTitleColorInt(int titleColorInt) {
this.titleColorInt = titleColorInt;
return this;
}
public BuildersetMsgColorInt(int msgColorInt) {
this.msgColorInt = msgColorInt;
return this;
}
public BuildersetLeftColorInt(int leftColorInt) {
this.leftColorInt = leftColorInt;
return this;
}
public BuildersetMidColorInt(int midColorInt) {
this.midColorInt = midColorInt;
return this;
}
public BuildersetRightColorInt(int rightColorInt) {
this.rightColorInt = rightColorInt;
return this;
}
public BuildersetTitleColorHex(String titleColorHex) {
this.titleColorHex = titleColorHex;
return this;
}
public BuildersetMsgColorHex(String msgColorHex) {
this.msgColorHex = msgColorHex;
return this;
}
public BuildersetLeftColorHex(String leftColorHex) {
this.leftColorHex = leftColorHex;
return this;
}
public BuildersetMidColorHex(String midColorHex) {
this.midColorHex = midColorHex;
return this;
}
public BuildersetRightColorHex(String rightColorHex) {
this.rightColorHex = rightColorHex;
return this;
}
public BuildersetLeftListener(OnDialogClickListener leftListener) {
this.leftListener = leftListener;
return this;
}
public BuildersetMidListener(OnDialogClickListener midListener) {
this.midListener = midListener;
return this;
}
public BuildersetRightListener(OnDialogClickListener rightListener) {
this.rightListener = rightListener;
return this;
}
public BuildersetShowTitle(boolean showTitle) {
isShowTitle = showTitle;
return this;
}
public BuildersetShowContent(boolean showContent) {
isShowContent = showContent;
return this;
}
public BuildersetCanceledOnTouchOutside(boolean canceledOnTouchOutside) {
this.canceledOnTouchOutside = canceledOnTouchOutside;
return this;
}
/**
* 建造者参数设置完毕必须调用此方法,返回dialog实例并调用dialog.show()方法才能显示出来
*
* @return
*/
public BuilderModeDialogcreate() {
return new BuilderModeDialog(this);
}
}
public interface OnDialogClickListener {
void onDialogClick();
}
}
有任何疑问请联系我电话+微信 18061495586~