赞
踩
一个可以直接使用的自定义AlertDialog,封装的很完整的一个Demo,可以灵活使用,里面提示图标还额外添加了动画效果,希望可以帮到大家。不多说,展示一部分代码给大家。
文章后面会给出源码。欢迎大家下载!!!
效果图:
// 调用的Activity
- package com.pnikosis.materialishprogress;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Toast;
- import cn.pedant.SweetAlert.SweetAlertDialog;
- import cn.pedant.SweetAlert.SweetAlertDialog.OnSweetClickListener;
-
-
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main1);
- findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- showAlertDiaLog(1, null, "Hello.Title", false, true,
- new OnSweetClickListener() {
- @Override
- public void onClick(
- SweetAlertDialog sweetAlertDialog) {
- Toast.makeText(getApplicationContext(), "NO", 0)
- .show();
- }
- }, new OnSweetClickListener() {
- @Override
- public void onClick(
- SweetAlertDialog sweetAlertDialog) {
- Toast.makeText(getApplicationContext(), "OK", 0).show();
- }
- });
- }
- });
- findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- showAlertDiaLog(2, null, "Hello.Title1", false, true,
- new OnSweetClickListener() {
- @Override
- public void onClick(
- SweetAlertDialog sweetAlertDialog) {
- Toast.makeText(getApplicationContext(), "NO", 0)
- .show();
- }
- }, new OnSweetClickListener() {
- @Override
- public void onClick(
- SweetAlertDialog sweetAlertDialog) {
- Toast.makeText(getApplicationContext(), "OK", 0)
- .show();
- }
- });
- }
- });
-
-
- findViewById(R.id.button3).setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- showAlertDiaLog(3, "NO", "Hello.Title2", true, true,
- new OnSweetClickListener() {
- @Override
- public void onClick(
- SweetAlertDialog sweetAlertDialog) {
- Toast.makeText(getApplicationContext(), "NO", 0)
- .show();
- }
- }, new OnSweetClickListener() {
- @Override
- public void onClick(
- SweetAlertDialog sweetAlertDialog) {
- Toast.makeText(getApplicationContext(), "OK", 0)
- .show();
- }
- });
- }
- });
-
-
- findViewById(R.id.button4).setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- showAlertDiaLog(0, "NO", "Hello.Title3", true, true,
- new OnSweetClickListener() {
- @Override
- public void onClick(
- SweetAlertDialog sweetAlertDialog) {
- Toast.makeText(getApplicationContext(), "NO", 0)
- .show();
- }
- }, new OnSweetClickListener() {
- @Override
- public void onClick(
- SweetAlertDialog sweetAlertDialog) {
- Toast.makeText(getApplicationContext(), "OK", 0)
- .show();
- }
- });
- }
- });
-
- }
-
-
- public void showAlertDiaLog(int type, String CancelText, String TitleText,
- boolean CancelButton, boolean ContentText,
- OnSweetClickListener CancelClickListener,
- OnSweetClickListener ConfirmClickListener) {
- SweetAlertDialog alertDialog = new SweetAlertDialog(MainActivity.this);
- /**
- * AlertType 显示图标类型 1.错误提示 2.正确提示 3.感叹号提示 其他.不显示图标
- */
- alertDialog.changeAlertType(type);
- /**
- * 是否显示取消按钮 true显示 false不显示
- */
- alertDialog.setCancelText(CancelText);
- alertDialog.showCancelButton(CancelButton);
- /**
- * 是否显示提示内容 true显示 false不显示
- */
- alertDialog.setTitleText(TitleText);
- alertDialog.showContentText(ContentText);
- /**
- * 取消按钮的点击事件
- */
- alertDialog.setCancelClickListener(CancelClickListener);
- /**
- * 确定按钮的点击事件
- */
- alertDialog.setConfirmClickListener(ConfirmClickListener);
- alertDialog.show();
- }
- }
//自定义Dialog
- package cn.pedant.SweetAlert;
-
- import java.util.List;
-
-
- import android.app.Dialog;
- import android.content.Context;
- import android.graphics.drawable.Drawable;
- import android.os.Build;
- import android.os.Bundle;
- import android.view.View;
- import android.view.WindowManager;
- import android.view.animation.AlphaAnimation;
- import android.view.animation.Animation;
- import android.view.animation.AnimationSet;
- import android.view.animation.Transformation;
- import android.widget.Button;
- import android.widget.FrameLayout;
- import android.widget.ImageView;
- import android.widget.TextView;
-
-
- import com.pnikosis.materialishprogress.ProgressWheel;
- import com.pnikosis.materialishprogress.R;
-
-
- public class SweetAlertDialog extends Dialog implements View.OnClickListener {
- private View mDialogView;
- private AnimationSet mModalInAnim;
- private AnimationSet mModalOutAnim;
- private Animation mOverlayOutAnim;
- private Animation mErrorInAnim;
- private AnimationSet mErrorXInAnim;
- private AnimationSet mSuccessLayoutAnimSet;
- private Animation mSuccessBowAnim;
- private TextView mTitleTextView;
- private TextView mContentTextView;
- private String mTitleText;
- private String mContentText;
- private boolean mShowCancel;
- private boolean mShowContent;
- private String mCancelText;
- private String mConfirmText;
- private int mAlertType;
- private FrameLayout mErrorFrame;
- private FrameLayout mSuccessFrame;
- private FrameLayout mProgressFrame;
- private SuccessTickView mSuccessTick;
- private ImageView mErrorX;
- private View mSuccessLeftMask;
- private View mSuccessRightMask;
- private Drawable mCustomImgDrawable;
- private ImageView mCustomImage;
- private Button mConfirmButton;
- private Button mCancelButton;
- private ProgressHelper mProgressHelper;
- private FrameLayout mWarningFrame;
- private OnSweetClickListener mCancelClickListener;
- private OnSweetClickListener mConfirmClickListener;
- private boolean mCloseFromCancel;
-
-
- public static final int NORMAL_TYPE = 0;
- public static final int ERROR_TYPE = 1;
- public static final int SUCCESS_TYPE = 2;
- public static final int WARNING_TYPE = 3;
- public static final int CUSTOM_IMAGE_TYPE = 4;
- public static final int PROGRESS_TYPE = 5;
-
-
- public static interface OnSweetClickListener {
- public void onClick(SweetAlertDialog sweetAlertDialog);
- }
-
-
- public SweetAlertDialog(Context context) {
- this(context, NORMAL_TYPE);
- }
-
-
- public SweetAlertDialog(Context context, int alertType) {
- super(context, R.style.alert_dialog);
- setCancelable(true);
- setCanceledOnTouchOutside(false);
- mProgressHelper = new ProgressHelper(context);
- mAlertType = alertType;
- mErrorInAnim = OptAnimationLoader.loadAnimation(getContext(),
- R.anim.error_frame_in);
- mErrorXInAnim = (AnimationSet) OptAnimationLoader.loadAnimation(
- getContext(), R.anim.error_x_in);
- // 2.3.x system don't support alpha-animation on layer-list drawable
- // remove it from animation set
- if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
- List<Animation> childAnims = mErrorXInAnim.getAnimations();
- int idx = 0;
- for (; idx < childAnims.size(); idx++) {
- if (childAnims.get(idx) instanceof AlphaAnimation) {
- break;
- }
- }
- if (idx < childAnims.size()) {
- childAnims.remove(idx);
- }
- }
- mSuccessBowAnim = OptAnimationLoader.loadAnimation(getContext(),
- R.anim.success_bow_roate);
- mSuccessLayoutAnimSet = (AnimationSet) OptAnimationLoader
- .loadAnimation(getContext(), R.anim.success_mask_layout);
- mModalInAnim = (AnimationSet) OptAnimationLoader.loadAnimation(
- getContext(), R.anim.modal_in);
- mModalOutAnim = (AnimationSet) OptAnimationLoader.loadAnimation(
- getContext(), R.anim.modal_out);
- mModalOutAnim.setAnimationListener(new Animation.AnimationListener() {
- @Override
- public void onAnimationStart(Animation animation) {
-
-
- }
-
-
- @Override
- public void onAnimationEnd(Animation animation) {
- mDialogView.setVisibility(View.GONE);
- mDialogView.post(new Runnable() {
- @Override
- public void run() {
- if (mCloseFromCancel) {
- SweetAlertDialog.super.cancel();
- } else {
- SweetAlertDialog.super.dismiss();
- }
- }
- });
- }
-
-
- @Override
- public void onAnimationRepeat(Animation animation) {
-
-
- }
- });
- // dialog overlay fade out
- mOverlayOutAnim = new Animation() {
- @Override
- protected void applyTransformation(float interpolatedTime,
- Transformation t) {
- WindowManager.LayoutParams wlp = getWindow().getAttributes();
- wlp.alpha = 1 - interpolatedTime;
- getWindow().setAttributes(wlp);
- }
- };
- mOverlayOutAnim.setDuration(120);
- }
-
-
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.alert_dialog);
-
-
- mDialogView = getWindow().getDecorView().findViewById(
- android.R.id.content);
- mTitleTextView = (TextView) findViewById(R.id.title_text);
- mContentTextView = (TextView) findViewById(R.id.content_text);
- mErrorFrame = (FrameLayout) findViewById(R.id.error_frame);
- mErrorX = (ImageView) mErrorFrame.findViewById(R.id.error_x);
- mSuccessFrame = (FrameLayout) findViewById(R.id.success_frame);
- mProgressFrame = (FrameLayout) findViewById(R.id.progress_dialog);
- mSuccessTick = (SuccessTickView) mSuccessFrame
- .findViewById(R.id.success_tick);
- mSuccessLeftMask = mSuccessFrame.findViewById(R.id.mask_left);
- mSuccessRightMask = mSuccessFrame.findViewById(R.id.mask_right);
- mCustomImage = (ImageView) findViewById(R.id.custom_image);
- mWarningFrame = (FrameLayout) findViewById(R.id.warning_frame);
- mConfirmButton = (Button) findViewById(R.id.confirm_button);
- mCancelButton = (Button) findViewById(R.id.cancel_button);
- mProgressHelper
- .setProgressWheel((ProgressWheel) findViewById(R.id.progressWheel));
- mConfirmButton.setOnClickListener(this);
- mCancelButton.setOnClickListener(this);
-
-
- setTitleText(mTitleText);
- setContentText(mContentText);
- setCancelText(mCancelText);
- setConfirmText(mConfirmText);
- changeAlertType(mAlertType, true);
-
-
- }
-
-
- private void restore() {
- mCustomImage.setVisibility(View.GONE);
- mErrorFrame.setVisibility(View.GONE);
- mSuccessFrame.setVisibility(View.GONE);
- mWarningFrame.setVisibility(View.GONE);
- mProgressFrame.setVisibility(View.GONE);
- mConfirmButton.setVisibility(View.VISIBLE);
-
-
- mConfirmButton.setBackgroundResource(R.drawable.blue_button_background);
- mErrorFrame.clearAnimation();
- mErrorX.clearAnimation();
- mSuccessTick.clearAnimation();
- mSuccessLeftMask.clearAnimation();
- mSuccessRightMask.clearAnimation();
- }
-
-
- private void playAnimation() {
- if (mAlertType == ERROR_TYPE) {
- mErrorFrame.startAnimation(mErrorInAnim);
- mErrorX.startAnimation(mErrorXInAnim);
- } else if (mAlertType == SUCCESS_TYPE) {
- mSuccessTick.startTickAnim();
- mSuccessRightMask.startAnimation(mSuccessBowAnim);
- }
- }
-
-
- private void changeAlertType(int alertType, boolean fromCreate) {
- mAlertType = alertType;
- // call after created views
- if (mDialogView != null) {
- if (!fromCreate) {
- // restore all of views state before switching alert type
- restore();
- }
- switch (mAlertType) {
- case ERROR_TYPE:
- mErrorFrame.setVisibility(View.VISIBLE);
- break;
- case SUCCESS_TYPE:
- mSuccessFrame.setVisibility(View.VISIBLE);
- // initial rotate layout of success mask
- mSuccessLeftMask.startAnimation(mSuccessLayoutAnimSet
- .getAnimations().get(0));
- mSuccessRightMask.startAnimation(mSuccessLayoutAnimSet
- .getAnimations().get(1));
- break;
- case WARNING_TYPE:
- mConfirmButton
- .setBackgroundResource(R.drawable.red_button_background);
- mWarningFrame.setVisibility(View.VISIBLE);
- break;
- case CUSTOM_IMAGE_TYPE:
- setCustomImage(mCustomImgDrawable);
- break;
- case PROGRESS_TYPE:
- mProgressFrame.setVisibility(View.VISIBLE);
- mConfirmButton.setVisibility(View.GONE);
- break;
- }
- if (!fromCreate) {
- playAnimation();
- }
- }
- }
-
-
- public int getAlerType() {
- return mAlertType;
- }
-
-
- public void changeAlertType(int alertType) {
- changeAlertType(alertType, false);
- }
-
-
- public String getTitleText() {
- return mTitleText;
- }
-
-
- public SweetAlertDialog setTitleText(String text) {
- mTitleText = text;
- if (mTitleTextView != null && mTitleText != null) {
- mTitleTextView.setText(mTitleText);
- }
- return this;
- }
-
-
- public SweetAlertDialog setCustomImage(Drawable drawable) {
- mCustomImgDrawable = drawable;
- if (mCustomImage != null && mCustomImgDrawable != null) {
- mCustomImage.setVisibility(View.VISIBLE);
- mCustomImage.setImageDrawable(mCustomImgDrawable);
- }
- return this;
- }
-
-
- public SweetAlertDialog setCustomImage(int resourceId) {
- return setCustomImage(getContext().getResources().getDrawable(
- resourceId));
- }
-
-
- public String getContentText() {
- return mContentText;
- }
-
-
- public SweetAlertDialog setContentText(String text) {
- mContentText = text;
- if (mContentTextView != null && mContentText != null) {
- showContentText(true);
- mContentTextView.setText(mContentText);
- }
- return this;
- }
-
-
- public boolean isShowCancelButton() {
- return mShowCancel;
- }
-
-
- public SweetAlertDialog showCancelButton(boolean isShow) {
- mShowCancel = isShow;
- if (mCancelButton != null) {
- mCancelButton.setVisibility(mShowCancel ? View.VISIBLE : View.GONE);
- }
- return this;
- }
-
-
- public boolean isShowContentText() {
- return mShowContent;
- }
-
-
- public SweetAlertDialog showContentText(boolean isShow) {
- mShowContent = isShow;
- if (mContentTextView != null) {
- mContentTextView.setVisibility(mShowContent ? View.VISIBLE
- : View.GONE);
- }
- return this;
- }
-
-
- public String getCancelText() {
- return mCancelText;
- }
-
-
- public SweetAlertDialog setCancelText(String text) {
- mCancelText = text;
- if (mCancelButton != null && mCancelText != null) {
- showCancelButton(true);
- mCancelButton.setText(mCancelText);
- }
- return this;
- }
-
-
- public String getConfirmText() {
- return mConfirmText;
- }
-
-
- public SweetAlertDialog setConfirmText(String text) {
- mConfirmText = text;
- if (mConfirmButton != null && mConfirmText != null) {
- mConfirmButton.setText(mConfirmText);
- }
- return this;
- }
-
-
- public SweetAlertDialog setCancelClickListener(OnSweetClickListener listener) {
- mCancelClickListener = listener;
- return this;
- }
-
-
- public SweetAlertDialog setConfirmClickListener(
- OnSweetClickListener listener) {
- mConfirmClickListener = listener;
- return this;
- }
-
-
- protected void onStart() {
- mDialogView.startAnimation(mModalInAnim);
- playAnimation();
- }
-
-
- /**
- * The real Dialog.cancel() will be invoked async-ly after the animation
- * finishes.
- */
- @Override
- public void cancel() {
- dismissWithAnimation(true);
- }
-
-
- /**
- * The real Dialog.dismiss() will be invoked async-ly after the animation
- * finishes.
- */
- public void dismissWithAnimation() {
- dismissWithAnimation(false);
- }
-
-
- private void dismissWithAnimation(boolean fromCancel) {
- mCloseFromCancel = fromCancel;
- mConfirmButton.startAnimation(mOverlayOutAnim);
- mDialogView.startAnimation(mModalOutAnim);
- }
-
-
- @Override
- public void onClick(View v) {
- if (v.getId() == R.id.cancel_button) {
- if (mCancelClickListener != null) {
- mCancelClickListener.onClick(SweetAlertDialog.this);
- dismissWithAnimation();
- } else {
- dismissWithAnimation();
- }
- } else if (v.getId() == R.id.confirm_button) {
- if (mConfirmClickListener != null) {
- mConfirmClickListener.onClick(SweetAlertDialog.this);
- dismissWithAnimation();
- } else {
- dismissWithAnimation();
- }
- }
- }
-
-
- public ProgressHelper getProgressHelper() {
- return mProgressHelper;
- }
-
-
- }
源码下载地址http://download.csdn.net/detail/u013752594/8901985
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。