由于Android系统的碎片化,导致对话框的样式在每款手机上都不尽相同,基于这个原因和UI的需求,在项目中自己定义了一个对话框,提供基本的样式设定,效果如图
我们定义一个CustomerDialog继承自Dialog,并对外暴露设置属性的相关方法,类代码如下.
package com.qfang.androidclient.widgets.dialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.qfang.qfangmobilecore.R;
/**
* 自定义Dialog,提供设置标题,内容,确定,取消点击事件
* 采用Builder构造模式创建,下边是代码示例
* <p/>a
* CustomerDialog.Builder builder = new CustomerDialog.Builder(this);
* CustomerDialog dialog = builder.setTitle("标题").setMessage("内容").setPositiveButton("确定", new DialogInterface.OnClickListener() {
*
* @Override public void onClick(DialogInterface dialog, int which) {
* dialog.dismiss();
* }
* }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
* @Override public void onClick(DialogInterface dialog, int which) {
* dialog.dismiss();
* }
* }).setPositiveButtonTextColor(getResources().getColor(R.color.orange_ff9933))
* .create();
* dialog.setCanceledOnTouchOutside(true);
* dialog.show();
* <p>
* created by shidong
* modified by shidong 4.6.0
*/
public class CustomerDialog extends Dialog {
public CustomerDialog(Context context) {
super(context);
}
public CustomerDialog(Context context, int theme) {
super(context, theme);
}
public static class Builder {
private Context context;
private String title;
private String message;
private String positiveButtonText;
private String negativeButtonText;
private View contentView;
private OnClickListener positiveButtonClickListener;
private OnClickListener negativeButtonClickListener;
private int positiveButtonTextColor;
private int negativeButtonTextColor;
private boolean isCenter;
public Builder(Context context) {
this.context = context;
}
public Builder setMessage(String message) {
this.message = message;
return this;
}
/**
* Set the Dialog message from resource
*
* @param message
* @return
*/
public Builder setMessage(int message) {
this.message = (String) context.getText(message);
return this;
}
/**
* Set the Dialog title from resource
*
* @param title
* @return
*/
public Builder setTitle(int title) {
this.title = (String) context.getText(title);
return this;
}
/**
* Set the Dialog title from String
*
* @param title
* @return
*/
public Builder setTitle(String title) {
this.title = title;
return this;
}
public Builder setContentView(View v) {
this.contentView = v;
return this;
}
/**
* Set the positive button resource and it's listener
*
* @param positiveButtonText
* @return
*/
public Builder setPositiveButton(int positiveButtonText,
OnClickListener listener) {
this.positiveButtonText = (String) context.getText(positiveButtonText);
this.positiveButtonClickListener = listener;
return this;
}
public Builder setPositiveButton(String positiveButtonText,
OnClickListener listener) {
this.positiveButtonText = positiveButtonText;
this.positiveButtonClickListener = listener;
return this;
}
public Builder setNegativeButton(int negativeButtonText,
OnClickListener listener) {
this.negativeButtonText = (String) context.getText(negativeButtonText);
this.negativeButtonClickListener = listener;
return this;
}
public Builder setNegativeButton(String negativeButtonText,
OnClickListener listener) {
this.negativeButtonText = negativeButtonText;
this.negativeButtonClickListener = listener;
return this;
}
public Builder setPositiveButtonTextColor(int color) {
this.positiveButtonTextColor = color;
return this;
}
public Builder setNegativeButtonTextColor(int color) {
this.negativeButtonTextColor = color;
return this;
}
/**
* 设置Message居中
*
* @param isCenter
* @return
*/
public Builder setMessageCenter(boolean isCenter) {
this.isCenter = isCenter;
return this;
}
public CustomerDialog create() {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// instantiate the dialog with the custom Theme
final CustomerDialog dialog = new CustomerDialog(context, R.style.ExitDialog);
View layout = inflater.inflate(R.layout.layout_normal_dialog, null);
dialog.addContentView(layout, new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
// 设置标题
((TextView) layout.findViewById(R.id.tv_title)).setText(title);
// 设置确定按钮
if (positiveButtonText != null) {
((TextView) layout.findViewById(R.id.tv_positive))
.setText(positiveButtonText);
if (positiveButtonClickListener != null) {
layout.findViewById(R.id.tv_positive)
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
positiveButtonClickListener.onClick(dialog,
DialogInterface.BUTTON_POSITIVE);
}
});
}
if (positiveButtonTextColor != 0) {
((TextView) layout.findViewById(R.id.tv_positive))
.setTextColor(positiveButtonTextColor);
}
} else {
// if no confirm button just set the visibility to GONE
layout.findViewById(R.id.tv_positive).setVisibility(View.GONE);
layout.findViewById(R.id.view_vertical_line).setVisibility(View.GONE);
}
// 设置取消按钮
if (negativeButtonText != null) {
((TextView) layout.findViewById(R.id.tv_negative))
.setText(negativeButtonText);
if (negativeButtonClickListener != null) {
layout.findViewById(R.id.tv_negative)
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
negativeButtonClickListener.onClick(dialog,
DialogInterface.BUTTON_NEGATIVE);
}
});
}
if (negativeButtonTextColor != 0) {
((TextView) layout.findViewById(R.id.tv_negative))
.setTextColor(negativeButtonTextColor);
}
} else {
// if no confirm button just set the visibility to GONE
layout.findViewById(R.id.tv_negative).setVisibility(View.GONE);
layout.findViewById(R.id.view_vertical_line).setVisibility(View.GONE);
}
// set the content message
if (message != null) {
((TextView) layout.findViewById(R.id.tv_message)).setText(message);
if (isCenter) {
((TextView) layout.findViewById(R.id.tv_message)).setGravity(Gravity.CENTER);
}
} else {
if (contentView != null) {
// if no message set
// add the contentView to the dialog body
((LinearLayout) layout.findViewById(R.id.content)).removeAllViews();
((LinearLayout) layout.findViewById(R.id.content)).addView(contentView, new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
} else {
layout.findViewById(R.id.content).setVisibility(View.GONE);
}
}
dialog.setContentView(layout);
return dialog;
}
}
}