1、在做支付宝时,发现支付宝弹出一个自定义对话框,觉得不错,后来就查看了支付宝源码,以后自定义对话框可以借鉴来使用。
1.1、对话框布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="300dp"
android:orientation="vertical" >
<TextView
android:id="@+id/dialog_title"
android:layout_width="match_parent"
android:layout_height="40dp"
android:drawablePadding="10dp"
android:gravity="center"
android:textColor="#2277BB"
android:textSize="18sp"
tools:ignore="SelectableText" />
<ImageView
android:id="@+id/dialog_divider"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginLeft="14dp"
android:layout_marginRight="14dp"
android:scaleType="fitXY"
android:src="#2277BB"
tools:ignore="ContentDescription" />
<TextView
android:id="@+id/dialog_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="14dp"
android:layout_marginRight="14dp"
android:layout_marginTop="10dp"
android:maxWidth="225dp"
android:minHeight="30dp"
android:textColor="#666666"
android:textSize="16sp"
tools:ignore="SelectableText" />
<FrameLayout
android:id="@+id/dialog_content_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/dialog_button_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="22dp"
android:scaleType="fitXY"
android:src="#D9D9D9"
tools:ignore="ContentDescription" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="45dp" >
<Button
android:id="@+id/left_button"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@drawable/dialog_button_submit"
android:textColor="@drawable/dialog_button_colorlist"
android:textSize="18sp"
android:visibility="gone" />
<ImageView
android:id="@+id/dialog_split_v"
android:layout_width="1dp"
android:layout_height="45dp"
android:layout_gravity="center_horizontal"
android:scaleType="fitXY"
android:background="#D9D9D9"
android:visibility="gone"
tools:ignore="ContentDescription" />
<Button
android:id="@+id/right_button"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@drawable/dialog_button_submit"
android:textColor="@drawable/dialog_button_colorlist"
android:textSize="18sp"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
1.2、按钮的背景dialog_button_submit
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:drawable="@drawable/dialog_button_gray" android:state_focused="false" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="@drawable/dialog_button_gray" android:state_focused="false" android:state_pressed="false" android:state_selected="true"/>
<!-- Focused states -->
<item android:drawable="@drawable/dialog_button_blue" android:state_focused="true" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="@drawable/dialog_button_blue" android:state_focused="true" android:state_pressed="false" android:state_selected="true"/>
<!-- Pressed -->
<item android:drawable="@drawable/dialog_button_blue" android:state_pressed="true"/>
</selector>
1.3、按钮文字的颜色切换:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@android:color/white"/>
<item android:state_focused="true" android:color="@android:color/white"/>
<item android:color="#333333"/>
</selector>
1.4、蓝色和灰色的按钮背景:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#FFFFFF" />
<corners
android:bottomLeftRadius="5dip"
android:bottomRightRadius="5dip" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#3798DC" />
<corners
android:bottomLeftRadius="5dip"
android:bottomRightRadius="5dip" />
</shape>
2、自定义对话框:
public class CustomAlertDialog extends Dialog implements DialogInterface {
private DialogCache mDialogCache;
private DialogInterface mDialogInterface;
private Handler mHandler;
private Button mLeftButton;
private Button mRightButton;
private View mButtonDivider;
private TextView mTitle;
private TextView mMessage;
private ImageView mDivider;
private FrameLayout mContentView;
private View mButtonGroup;
private boolean mBeyondHoneycomb = Build.VERSION.SDK_INT >= 11;
private View.OnClickListener mButtonHandler = new View.OnClickListener() {
public void onClick(View v) {
Message buttonMessage = (Message) v.getTag();
if (buttonMessage == null) {
CustomAlertDialog.this.dismiss();
return;
}
Message m = Message.obtain(buttonMessage);
if (m != null) {
m.sendToTarget();
}
CustomAlertDialog.this.mHandler.obtainMessage(1,
CustomAlertDialog.this.mDialogInterface).sendToTarget();
}
};
public CustomAlertDialog(DialogCache dialogCache) {
super(dialogCache.mContext, R.style.AlertDialog);
this.mDialogCache = dialogCache;
this.mDialogInterface = this;
this.mHandler = new ButtonHandler(this.mDialogInterface);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_alert);
this.mLeftButton = ((Button) findViewById(R.id.left_button));
this.mRightButton = ((Button) findViewById(R.id.right_button));
this.mButtonDivider = findViewById(R.id.dialog_split_v);
this.mTitle = ((TextView) findViewById(R.id.dialog_title));
this.mMessage = ((TextView) findViewById(R.id.dialog_message));
this.mDivider = ((ImageView) findViewById(R.id.dialog_divider));
this.mContentView = ((FrameLayout) findViewById(R.id.dialog_content_view));
this.mButtonGroup = findViewById(R.id.dialog_button_group);
setupTitle();
setupMessage();
setupView();
setupButtons();
}
private void setupTitle() {
if (TextUtils.isEmpty(this.mDialogCache.mTitle)) {
this.mTitle.setVisibility(8);
this.mDivider.setVisibility(8);
} else {
this.mTitle.setVisibility(0);
this.mDivider.setVisibility(0);
if (this.mDialogCache.mIcon != null) {
this.mTitle.setCompoundDrawablesWithIntrinsicBounds(
this.mDialogCache.mIcon, null, null, null);
}
this.mTitle.setText(this.mDialogCache.mTitle);
}
}
private void setupMessage() {
boolean showMessage = (!TextUtils.isEmpty(this.mDialogCache.mMessage))
&& (this.mDialogCache.mView == null);
if (showMessage) {
this.mMessage.setVisibility(0);
this.mMessage.setText(this.mDialogCache.mMessage);
} else {
this.mMessage.setVisibility(8);
}
}
private void setupView() {
if (this.mDialogCache.mView == null) {
return;
}
this.mContentView.removeAllViews();
this.mContentView.addView(this.mDialogCache.mView);
}
private boolean setupButtons() {
int buttonCount = 0;
Button positiveButton = this.mBeyondHoneycomb ? this.mRightButton
: this.mLeftButton;
Button negativeButton = this.mBeyondHoneycomb ? this.mLeftButton
: this.mRightButton;
if (TextUtils.isEmpty(this.mDialogCache.mPositiveButton)) {
positiveButton.setVisibility(8);
} else {
positiveButton.setVisibility(0);
positiveButton.setText(this.mDialogCache.mPositiveButton);
positiveButton.setOnClickListener(this.mButtonHandler);
positiveButton.setTag(this.mDialogCache.mButtonPositiveMessage);
buttonCount++;
}
if (TextUtils.isEmpty(this.mDialogCache.mNegativeButton)) {
negativeButton.setVisibility(8);
} else {
negativeButton.setVisibility(0);
negativeButton.setText(this.mDialogCache.mNegativeButton);
negativeButton.setOnClickListener(this.mButtonHandler);
negativeButton.setTag(this.mDialogCache.mButtonNegativeMessage);
buttonCount++;
}
this.mButtonDivider.setVisibility(buttonCount > 1 ? 0 : 8);
this.mButtonGroup.setVisibility(buttonCount == 0 ? 8 : 0);
return buttonCount != 0;
}
public void setButton(int whichButton, CharSequence text,
DialogInterface.OnClickListener listener, Message msg) {
if ((msg == null) && (listener != null)) {
msg = this.mHandler.obtainMessage(whichButton, listener);
}
switch (whichButton) {
case -1:
this.mDialogCache.mPositiveButton = text;
this.mDialogCache.mButtonPositiveMessage = msg;
break;
case -2:
this.mDialogCache.mNegativeButton = text;
this.mDialogCache.mButtonNegativeMessage = msg;
break;
default:
throw new IllegalArgumentException("Button does not exist");
}
}
public static class Builder {
private final CustomAlertDialog.DialogCache mDialogCache;
public Builder(Context context) {
this.mDialogCache = new CustomAlertDialog.DialogCache();
this.mDialogCache.mContext = context;
}
public Builder setMessage(int resId) {
this.mDialogCache.mMessage = this.mDialogCache.mContext
.getText(resId);
return this;
}
public Builder setMessage(CharSequence message) {
this.mDialogCache.mMessage = message;
return this;
}
public Builder setTitle(int resId) {
return setTitle(this.mDialogCache.mContext.getText(resId));
}
public Builder setTitle(CharSequence title) {
this.mDialogCache.mTitle = title;
return this;
}
public Builder setIcon(int resId) {
return setIcon(this.mDialogCache.mContext.getResources()
.getDrawable(resId));
}
public Builder setIcon(Drawable drawable) {
this.mDialogCache.mIcon = drawable;
return this;
}
public Builder setView(View view) {
this.mDialogCache.mView = view;
return this;
}
public Builder setPositiveButton(int resId,
DialogInterface.OnClickListener listener) {
return setPositiveButton(
this.mDialogCache.mContext.getString(resId), listener);
}
public Builder setPositiveButton(CharSequence text,
DialogInterface.OnClickListener listener) {
this.mDialogCache.mPositiveButton = text;
this.mDialogCache.mPositiveButtonListener = listener;
return this;
}
public Builder setNegativeButton(int resId,
DialogInterface.OnClickListener listener) {
return setNegativeButton(this.mDialogCache.mContext.getText(resId),
listener);
}
public Builder setNegativeButton(CharSequence text,
DialogInterface.OnClickListener listener) {
this.mDialogCache.mNegativeButton = text;
this.mDialogCache.mNegativeButtonListener = listener;
return this;
}
public Builder setSingleChoiceItems(ListAdapter adapter,
int checkedItem, DialogInterface.OnClickListener listener) {
return this;
}
public void apply(CustomAlertDialog dialog) {
if (this.mDialogCache.mPositiveButton != null) {
dialog.setButton(-1, this.mDialogCache.mPositiveButton,
this.mDialogCache.mPositiveButtonListener, null);
}
if (this.mDialogCache.mNegativeButton != null)
dialog.setButton(-2, this.mDialogCache.mNegativeButton,
this.mDialogCache.mNegativeButtonListener, null);
}
public void setCancelable(boolean flag) {
this.mDialogCache.mCancelable = flag;
}
public void setOnKeyListener(DialogInterface.OnKeyListener onKeyListener) {
this.mDialogCache.mOnKeyListener = onKeyListener;
}
public CustomAlertDialog create() {
CustomAlertDialog dialog = new CustomAlertDialog(this.mDialogCache);
apply(dialog);
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(this.mDialogCache.mCancelable);
dialog.setOnCancelListener(this.mDialogCache.mOnCancelListener);
if (this.mDialogCache.mOnKeyListener != null) {
dialog.setOnKeyListener(this.mDialogCache.mOnKeyListener);
}
return dialog;
}
public CustomAlertDialog show() {
CustomAlertDialog dialog = create();
dialog.show();
return dialog;
}
}
private static final class ButtonHandler extends Handler {
private static final int MSG_DISMISS_DIALOG = 1;
private WeakReference<DialogInterface> mDialog;
public ButtonHandler(DialogInterface dialog) {
super();
this.mDialog = new WeakReference(dialog);
}
public void handleMessage(Message msg) {
switch (msg.what) {
case -3:
case -2:
case -1:
((DialogInterface.OnClickListener) msg.obj).onClick(
(DialogInterface) this.mDialog.get(), msg.what);
break;
case 1:
((DialogInterface) msg.obj).dismiss();
case 0:
}
}
}
private static class DialogCache {
CharSequence mTitle;
CharSequence mMessage;
Drawable mIcon;
Context mContext;
View mView;
DialogInterface.OnCancelListener mOnCancelListener;
DialogInterface.OnKeyListener mOnKeyListener;
CharSequence mPositiveButton;
CharSequence mNegativeButton;
DialogInterface.OnClickListener mPositiveButtonListener;
DialogInterface.OnClickListener mNegativeButtonListener;
Message mButtonPositiveMessage;
Message mButtonNegativeMessage;
boolean mCancelable = false;
}
}
3、调用:
CustomAlertDialog.Builder dialog = new CustomAlertDialog.Builder(ScoreMallActivity.this);
dialog.setTitle("提示");
dialog.setMessage("请先登录");
dialog.setPositiveButton("确定",new CustomAlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog.setNegativeButton("取消", new CustomAlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog.show();