創(chuàng)建簡書賬號差不多一年,想想還沒有寫過什么內(nèi)容。剛好最近想沉淀一下自己,總結(jié)一下自己工作以來的所接觸和用過的有價值的東西。既是總結(jié)自己,也希望分享給大家。后面我會陸續(xù)更新我的博客
所以,第一篇文章從什么開始呢,那就從封裝一套通用的Dialog提示框開始吧。這套dialog是我在工作中自己用建造者模式封裝的,使用起來很方便,今天分享給大家。
如果對建造者模式不太了解的小伙伴請自行查閱資料,此處不進(jìn)行進(jìn)一步探討。
先看看效果圖吧。



----?如何使用?----
準(zhǔn)備步驟(具體代碼見后文"源代碼"):
1、創(chuàng)建布局文件:dialog_layout.xml
2、創(chuàng)建圓角背景:dialog_round_bg.xml
3、如果需要我圖上默認(rèn)的icon,這是我直接從google發(fā)布的material-design-icons-master中拷貝的:

4、創(chuàng)建自定義dalog類:BuilderModeDialog.class
開始使用:

----?方法說明?----

---- 源代碼?----
1、dialog圓角背景:dialog_round_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:shape="rectangle">
? ? <solid android:color="#FFFFFF" />
? ? ? ? android:radius="40dp"/>
</shape>
2、布局文件:dialog_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:id="@+id/main_layout"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? android:background="@drawable/dialog_round_bg"
? ? android:orientation="vertical">
? ? <RelativeLayout
? ? ? ? android:id="@+id/title_layout"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:paddingTop="15dp"
? ? ? ? android:paddingBottom="15dp">
? ? ? ? <TextView
? ? ? ? ? ? android:id="@+id/dialog_title"
? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:gravity="center"
? ? ? ? ? ? android:text="這是提示標(biāo)題"
? ? ? ? ? ? android:paddingLeft="50dp"
? ? ? ? ? ? android:paddingRight="50dp"
? ? ? ? ? ? android:layout_centerVertical="true"
? ? ? ? ? ? android:textSize="16sp"
? ? ? ? ? ? android:textColor="#000000"/>
? ? ? ? <ImageView
? ? ? ? ? ? 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" />
? ? </RelativeLayout>
? ? <TextView
? ? ? ? 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="這是提示內(nèi)容"
? ? ? ? android:paddingLeft="40dp"
? ? ? ? android:paddingRight="40dp"
? ? ? ? android:textSize="14sp"
? ? ? ? android:textColor="#000000"/>
? ? <View
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="1dp"
? ? ? ? android:background="#f3f3f3" />
? ? <LinearLayout
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:orientation="horizontal">
? ? ? ? <TextView
? ? ? ? ? ? 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"/>
? ? ? ? <View
? ? ? ? ? ? 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" />
? ? ? ? <TextView
? ? ? ? ? ? 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"/>
? ? ? ? <View
? ? ? ? ? ? android:layout_width="0.5dp"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:layout_marginTop="4dp"
? ? ? ? ? ? android:layout_marginBottom="4dp"
? ? ? ? ? ? android:background="#f3f3f3" />
? ? ? ? <TextView
? ? ? ? ? ? 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"/>
? ? </LinearLayout>
? ? <View
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="1dp"
? ? ? ? android:background="#f3f3f3" />
</LinearLayout>
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;//標(biāo)題
? ? private TextViewdialogContent;//提示內(nèi)容
? ? private TextViewdialogLeftBt;//左側(cè)按鈕
? ? private TextViewdialogMidBt;//中間按鈕
? ? private TextViewdialogRightBt;//右側(cè)按鈕
? ? private ViewbtCenterLine;//按鈕分隔線
? ? /**
* 按鈕個數(shù)-默認(rèn)
*/
? ? public static final int BUTTON_MODE_DEFAULT =0;
? ? /**
* 按鈕個數(shù)-1
*/
? ? public static final int BUTTON_MODE_MID =1;
? ? /**
* 按鈕個數(shù)-2
*/
? ? public static final int BUTTON_MODE_LEFT_RIGHT =2;
? ? /**
* 按鈕個數(shù)-3
*/
? ? public static final int BUTTON_MODE_LEFT_MID_RIGHT =3;
? ? /**
* 存放設(shè)置的參數(shù)
*/
? ? 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:
//默認(rèn)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進(jìn)制顏色
? ? ? ? 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;
? ? ? ? }
}
/**
* 設(shè)置的參數(shù)。通過該實體保存建造者收到的參數(shù)
*/
? ? 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;
? ? }
/**
* 建造者。所有設(shè)置參數(shù)都通過建造者設(shè)置
*/
? ? public static class Builder {
private Contextcontext;//上下文
? ? ? ? private Stringtitle;//標(biāo)題
? ? ? ? private Stringmsg;//內(nèi)容
? ? ? ? private int icon;//圖標(biāo)
? ? ? ? private StringleftText;//左側(cè)按鈕文字
? ? ? ? private StringmidText;//中間按鈕文字
? ? ? ? private StringrightText;//右側(cè)按鈕文字
? ? ? ? private int buttonMode;//按鈕格式。默認(rèn)雙按鈕
? ? ? ? private int titleColorInt;//標(biāo)題文字顏色
? ? ? ? private int msgColorInt;//內(nèi)容文字顏色
? ? ? ? private int leftColorInt;//左側(cè)按鈕文字顏色
? ? ? ? private int midColorInt;//中間按鈕文字顏色
? ? ? ? private int rightColorInt;//右側(cè)按鈕文字顏色
? ? ? ? private StringtitleColorHex;//標(biāo)題文字顏色-16進(jìn)制。如"#DC143C"
? ? ? ? private StringmsgColorHex;//內(nèi)容文字顏色-16進(jìn)制
? ? ? ? private StringleftColorHex;//左側(cè)按鈕文字顏色-16進(jìn)制
? ? ? ? private StringmidColorHex;//中間按鈕文字顏色-16進(jìn)制
? ? ? ? private StringrightColorHex;//右側(cè)按鈕文字顏色-16進(jìn)制
? ? ? ? private boolean isShowTitle =true;//是否顯示標(biāo)題
? ? ? ? private boolean isShowContent =true;//是否顯示內(nèi)容
? ? ? ? private boolean canceledOnTouchOutside =false;//是否點擊dialog外部取消dialog,默認(rèn)為false
? ? ? ? private OnDialogClickListenerleftListener;//左側(cè)按鈕點擊事件
? ? ? ? private OnDialogClickListenermidListener;//中間按鈕點擊事件
? ? ? ? private OnDialogClickListenerrightListener;//右側(cè)按鈕點擊事件
? ? ? ? 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;
? ? ? ? }
/**
* 建造者參數(shù)設(shè)置完畢必須調(diào)用此方法,返回dialog實例并調(diào)用dialog.show()方法才能顯示出來
*
? ? ? ? * @return
? ? ? ? */
? ? ? ? public BuilderModeDialogcreate() {
return new BuilderModeDialog(this);
? ? ? ? }
}
public interface OnDialogClickListener {
void onDialogClick();
? ? }
}
有任何疑問請聯(lián)系我電話+微信 18061495586~