開門見程式碼:
public final class CommonDialog extends Dialog {
/*** 內部回撥 */
private interface InnerCallback {
void onLeftButtonClicked();
void onRightButtonClicked();
}
/*** 按鈕監聽器: 預設實現關閉彈窗 */
public static class OnButtonClickedListener implements InnerCallback {
private CommonDialog mDialog;
public OnButtonClickedListener(CommonDialog dialog) {
this.mDialog = dialog;
}
@Override
public void onLeftButtonClicked() {
UserInterfaceHelper.dismiss(mDialog);
mDialog = null;
}
@Override
public void onRightButtonClicked() {
UserInterfaceHelper.dismiss(mDialog);
mDialog = null;
}
}
public Context mContext;
public TextView mButtonLeft;
public TextView mButtonRight;
public TextView tv_title;
public TextView tv_content;
public LinearLayout btnLayout;
public LinearLayout oneBtnLayout;
public TextView mButtonOne;
/*** 左按鈕文字 */
private
@Nullable
CharSequence mLeftBtnText;
/*** 右按鈕文字 */
private
@Nullable
CharSequence mRightBtnText;
/*** 按鈕點選監聽器 */
private
@Nullable
InnerCallback mOnButtonClickedListener;
public CommonDialog(Context context) {
super(context);
mContext = context;
initView();
}
public void initView() {
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.layout_common_dialog);
Window window = getWindow();
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
mButtonLeft = findViewById(R.id.tvLeftBtn);
mButtonRight = findViewById(R.id.tvRightBtn);
tv_title = findViewById(R.id.tvTitle);
tv_content = findViewById(R.id.tvContent);
btnLayout = findViewById(R.id.ll_two_buttons);
oneBtnLayout = findViewById(R.id.ll_one_button);
mButtonOne = findViewById(R.id.button_only_one);
setCanceledOnTouchOutside(false);
}
/*** 設定 標題 */
@Deprecated
public CommonDialog setCustomTitle(@StringRes int resId) {
return setCustomTitle(getString(resId));
}
/*** 設定 標題 */
public CommonDialog setCustomTitle(CharSequence title) {
if (tv_title != null) {
tv_title.setText(title);
tv_title.setVisibility(View.VISIBLE);
}
return this;
}
/*** 設定下標題字型大小 */
public CommonDialog setTitleMargins(int left, int top, int right, int bottom) {
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) tv_title.getLayoutParams();
if (params != null) {
params.setMargins(left, top, right, bottom);
}
return this;
}
/*** 設定下標題字型大小 */
public CommonDialog setCustomTitleSize(int unit, float size) {
if (tv_title != null) {
tv_title.setTextSize(unit, size);
}
return this;
}
/*** 設定下內容字型大小 */
public CommonDialog setContentTextSize(int unit, float size) {
if (tv_content != null) {
tv_content.setTextSize(unit, size);
}
return this;
}
/**
* 設定下內容區文字方向
*
* @param gravity
*
* @return
*/
public CommonDialog setContentGravity(int gravity) {
if (tv_content != null) {
tv_content.setGravity(gravity);
}
return this;
}
/**
* 設定內容區內部Spannable文字可響應點選
*
* @return
*/
public CommonDialog setContentSpanableClick() {
if (tv_content != null) {
tv_content.setMovementMethod(LinkMovementMethod.getInstance());
tv_content.setHighlightColor(Color.TRANSPARENT);
}
return this;
}
/*** 設定標題加粗 */
public CommonDialog setTitleBoldStyle() {
if (tv_title != null) {
TextPaint paint = tv_title.getPaint();
paint.setFakeBoldText(true);
}
return this;
}
/*** 設定右按鈕加粗 */
public CommonDialog setRightButtoneBoldStyle() {
if (mButtonRight != null) {
TextPaint paint = mButtonRight.getPaint();
paint.setFakeBoldText(true);
}
return this;
}
/*** 設定 內容 */
public CommonDialog setCustomContent(@StringRes int resId) {
return setCustomContent(getString(resId));
}
/*** 設定 內容 傳null不顯示*/
public CommonDialog setCustomContent(CharSequence content) {
if (tv_content != null && !TextUtils.isEmpty(content)) {
tv_content.setText(content);
tv_content.setVisibility(View.VISIBLE);
} else if (TextUtils.isEmpty(content)) {
tv_content.setVisibility(View.GONE);
}
return this;
}
/*** 設定 左按鈕文字 */
public CommonDialog setLeftButton(@StringRes int resId) {
return setLeftButton(getString(resId));
}
/*** 設定 左按鈕文字 */
public CommonDialog setLeftButton(String text) {
mLeftBtnText = text;
return this;
}
/*** 設定底部Button的大小 */
public CommonDialog setButtonSize(int unit, float size) {
if (mButtonLeft != null && mButtonRight != null) {
mButtonLeft.setTextSize(unit, size);
mButtonRight.setTextSize(unit, size);
}
if (mButtonOne != null) {
mButtonOne.setTextSize(unit, size);
}
return this;
}
/*** 設定 左按鈕文字顏色 */
public CommonDialog setLeftButtonColor(@ColorRes int colorId) {
mButtonLeft.setTextColor(getContext().getResources().getColor(colorId));
return this;
}
/*** 設定 有按鈕文字顏色 */
public CommonDialog setRightButtonColor(@ColorRes int colorId) {
mButtonRight.setTextColor(getContext().getResources().getColor(colorId));
return this;
}
/*** 設定 右按鈕文字 */
public CommonDialog setRightButton(@StringRes int resId) {
return setRightButton(getString(resId));
}
/*** 設定 右按鈕文字 */
public CommonDialog setRightButton(String text) {
mRightBtnText = text;
return this;
}
/*** 設定 按鈕點選監聽器 */
public CommonDialog setOnButtonClickedListener(InnerCallback l) {
mOnButtonClickedListener = l;
return this;
}
/*** 設定 對話方塊 可取消 */
public CommonDialog setDialogCancelable(boolean flag) {
setCancelable(flag);
return this;
}
/**
* 設定左邊button是否可用
*/
public void setLeftButtonEnable(boolean enable) {
mButtonLeft.setEnabled(enable);
}
/**
* 設定右邊Button是否可用
*/
public void setRightButtonEnable(boolean enable) {
mButtonRight.setEnabled(enable);
}
/**
* 設定底部唯一Button是否可用
*/
public void setOneButtonEnable(boolean enable) {
mButtonOne.setEnabled(enable);
}
/*** 設定 對話方塊 可點選外部取消 */
public CommonDialog setDialogCanceledOnTouchOutside(boolean flag) {
setCanceledOnTouchOutside(flag);
return this;
}
/*** 設定 對話方塊 對齊方式是否為橫屏 */
@Deprecated
public CommonDialog setDialogOritention(boolean isLandspace) {
return this;
}
/*** setXX() 之後,最後呼叫 */
public boolean build() {
if (getContext() instanceof Activity) {
Activity act = (Activity) getContext();
if (act.isFinishing()) {
return false;
}
}
return onBuild();
}
private boolean onBuild() {
if (!TextUtils.isEmpty(mLeftBtnText) && !TextUtils.isEmpty(mRightBtnText)) {
btnLayout.setVisibility(View.VISIBLE);
oneBtnLayout.setVisibility(View.GONE);
mButtonLeft.setText(mLeftBtnText);
mButtonLeft.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mOnButtonClickedListener != null) {
mOnButtonClickedListener.onLeftButtonClicked();
}
}
});
mButtonRight.setText(mRightBtnText);
mButtonRight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mOnButtonClickedListener != null) {
mOnButtonClickedListener.onRightButtonClicked();
}
}
});
} else {
if (!TextUtils.isEmpty(mRightBtnText)) {
btnLayout.setVisibility(View.GONE);
oneBtnLayout.setVisibility(View.VISIBLE);
mButtonOne.setText(mRightBtnText);
mButtonOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mOnButtonClickedListener != null) {
mOnButtonClickedListener.onRightButtonClicked();
}
}
});
}
}
show();
return true;
}
private
@NonNull
String getString(@StringRes int resId) {
return getContext().getString(resId);
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/dialogLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/shape_rectangle_c_ffffff_r_6"
android:orientation="vertical">
<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="@dimen/dp_15"
android:layout_marginRight="@dimen/dp_15"
android:layout_marginTop="@dimen/dp_21"
android:gravity="center"
android:textColor="@android:color/black"
android:textSize="@dimen/font_30"
android:textStyle="bold"/>
<TextView
android:id="@+id/tvContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tvTitle"
android:paddingLeft="@dimen/dp_15"
android:paddingRight="@dimen/dp_15"
android:paddingTop="@dimen/dp_11"
android:paddingBottom="@dimen/dp_5"
android:gravity="center"
android:maxHeight="@dimen/dp_420"
android:textColor="@color/skin_color_text_primary"
android:textSize="@dimen/font_20"/>
<LinearLayout
android:id="@+id/btnLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tvContent"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="@dimen/dp_0.5"
android:background="@color/base_color_DFE1E5"/>
<LinearLayout
android:id="@+id/ll_two_buttons"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_42">
<FrameLayout
android:id="@+id/leftBtnFrame"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.0">
<TextView
android:id="@+id/tvLeftBtn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:drawablePadding="@dimen/dp_5"
android:gravity="center"
android:textColor="@color/color_767676"
android:textSize="@dimen/font_32"/>
</FrameLayout>
<View
android:id="@+id/dividerVertical"
android:layout_width="@dimen/dp_0.5"
android:layout_height="match_parent"
android:background="@color/base_color_DFE1E5"/>
<FrameLayout
android:id="@+id/rightBtnFrame"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.0">
<TextView
android:id="@+id/tvRightBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@color/color_v60_mgtv"
android:textSize="@dimen/font_32"/>
</FrameLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_one_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
<TextView
android:id="@+id/button_only_one"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_44"
android:clickable="true"
android:gravity="center"
android:textColor="@color/color_v60_mgtv"
android:textSize="@dimen/font_32"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
使用:
mDialog = new CommonDialog(getActivity());
String title = "xxxx";
mConfirmDialog.setCustomTitle(title).setLeftButton(R.string
.download_cancel).setCustomContent(sb).setRightButton(R.string.xxx)
.setDialogCanceledOnTouchOutside(false)
.setOnButtonClickedListener(new CommonDialog.OnButtonClickedListener(mConfirmDialog) {
@Override
public void onLeftButtonClicked() {
super.onLeftButtonClicked();
}
@Override
public void onRightButtonClicked() {
super.onRightButtonClicked();
}
}
});
mDialog.build();
mUserDialog.setCustomTitle(xxxxx)).setCustomTitleSize(TypedValue.COMPLEX_UNIT_PX,
getResources()
.getDimension(R.dimen.font_34)).setTitleBoldStyle().setCustomContent(spanableBuilder)
.setContentTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.font_26))
.setContentGravity(Gravity.START).setLeftButton(R.string.xxx).setRightButton(R.string.xxx)
.setRightButtonColor(R.color.xxx).setRightButtoneBoldStyle().setDialogCanceledOnTouchOutside(false)
.setButtonSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.font_30))
.setContentSpanableClick().setOnButtonClickedListener(new CommonDialog.OnButtonClickedListener(mUserDialog) {
@Override
public void onLeftButtonClicked() {
// stay here,no dismiss.
}
@Override
public void onRightButtonClicked() {
super.onRightButtonClicked();
}
});
mUserDialog.setCancelable(false);
mUserDialog.build();