前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android studio SweetAlert for Android

Android studio SweetAlert for Android

作者头像
用户9227784
发布2021-12-02 14:45:56
2.5K0
发布2021-12-02 14:45:56
举报

build.gradle

代码语言:javascript
复制
compile 'cn.pedant.sweetalert:library:1.3'

colors.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?  
\>  
<resources>  
    <color name="alertDialog\_Progress\_Color">#ff4c72dc</color>  
    <color name="alertDialog\_Progress\_hintColor">#40dcd8d5</color>  
</resources>

attrs.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>  
<resources>  
    <declare-styleable name="ProgressWheel">  
        <attr name="progressIndeterminate" format="boolean" />  
        <attr name="barColor" format="color" />  
        <attr name="rimColor" format="color" />  
        <attr name="rimWidth" format="dimension" />  
        <attr name="spinSpeed" format="float" />  
        <attr name="barSpinCycleTime" format="integer" />  
        <attr name="circleRadius" format="dimension" />  
        <attr name="fillRadius" format="boolean" />  
        <attr name="barWidth" format="dimension" />  
    </declare-styleable>  
    <!-- From: file:/Users/nicolas/AndroidProjects/materialish-progress/library/src/main/res/values/strings.xml -->  
    <eat-comment />  
    <string name="app\_name">Library</string>  
</resources>

測试界面:

測试类 MainActivity

代码语言:javascript
复制
import android.graphics.Color;  
import android.view.View;  
import android.widget.Button;  
  
import com.lidroid.xutils.view.annotation.ViewInject;  
import com.lidroid.xutils.view.annotation.event.OnClick;  
  
import cn.pedant.SweetAlert.SweetAlertDialog;  
  
/\*\*  
 \* Created by Administrator on 2015/7/1.  
 \*/  
public class MainActivity extends BasicActivity {  
  
    @ViewInject(R.id.sweetAlertDialog)  
    private Button btSweetAlertDialog;  
  
    @ViewInject(R.id.basicMessage)  
    private Button btBasicMessage;  
  
    //...  
  
    @Override  
    public int getLayoutID() {  
        return R.layout.activity\_main;  
    }  
  
    @OnClick({R.id.sweetAlertDialog,R.id.basicMessage,R.id.titleUnder,R.id.error,R.id.warning,R.id.success,R.id.custom\_icon,R.id.confirm\_button,R.id.cancel\_bindlistener,R.id.opup\_confirming})  
    public void onClick(View v) {  
        switch (v.getId()) {  
            case R.id.sweetAlertDialog:  
                showSweetAlertDialog();  
                break;  
            case R.id.basicMessage:  
                showBasicMessage();  
                break;  
            case R.id.titleUnder:  
                showTitleUnder();  
                break;  
            case R.id.error:  
                showError();  
                break;  
            case R.id.warning:  
                showWarning();  
                break;  
            case R.id.success:  
                showSuccess();  
                break;  
            case R.id.custom\_icon:  
                showCustmIcon();  
                break;  
            case R.id.confirm\_button:  
                showConfirming();  
                break;  
            case R.id.cancel\_bindlistener:  
                showCancelBind();  
                break;  
            case R.id.opup\_confirming:  
                showPupConfirming();  
                break;  
            default:  
                break;  
        }  
    }  
  
    private void showSweetAlertDialog() {  
        final SweetAlertDialog pDialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS\_TYPE);  
        pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.alertDialog\_Progress\_Color));  
        pDialog.getProgressHelper().setBarWidth(5);  
        pDialog.getProgressHelper().setCircleRadius(100);  
        pDialog.getProgressHelper().setRimColor(getResources().getColor(R.color.alertDialog\_Progress\_hintColor));  
        pDialog.getProgressHelper().setRimWidth(5);  
        pDialog.setTitleText("载入中..");  
        pDialog.setCancelable(false);  
        pDialog.show();  
        dismiss(pDialog);  
    }  
  
    private void showBasicMessage() {  
        SweetAlertDialog pDialog=new SweetAlertDialog(this)  
                .setTitleText("Here's a message!");  
                pDialog.show();  
    }  
  
    private void showTitleUnder() {  
        new SweetAlertDialog(this)  
                .setTitleText("Here's a message!")  
                .setContentText("It's pretty, isn't it?")  
                .show();  
    }  
  
    private void showError(){  
        new SweetAlertDialog(this, SweetAlertDialog.ERROR\_TYPE)  
                .setTitleText("Oops...")  
                .setContentText("Something went wrong!")  
                .show();  
    }  
  
    private void showWarning(){  
        new SweetAlertDialog(this, SweetAlertDialog.WARNING\_TYPE)  
                .setTitleText("Are you sure?")  
                .setContentText("Won't be able to recover this file!")  
                .setConfirmText("Yes,delete it!")  
                .show();  
    }  
  
    private void showSuccess(){  
        new SweetAlertDialog(this, SweetAlertDialog.SUCCESS\_TYPE)  
                .setTitleText("Good job!")  
                .setContentText("You clicked the button!")  
                .show();  
    }  
  
    private void showCustmIcon(){  
        new SweetAlertDialog(this, SweetAlertDialog.CUSTOM\_IMAGE\_TYPE)  
                .setTitleText("Sweet!")  
                .setContentText("Here's a custom image.")  
                .setCustomImage(R.drawable.ic\_launcher)  
                .show();  
    }  
  
    private void showConfirming(){  
        new SweetAlertDialog(this, SweetAlertDialog.WARNING\_TYPE)  
                .setTitleText("Are you sure?  
")  
                .setContentText("Won't be able to recover this file!")  
                .setConfirmText("Yes,delete it!")  
                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {  
                    @Override  
                    public void onClick(SweetAlertDialog sDialog) {  
                        sDialog.dismissWithAnimation();  
                    }  
                })  
                .show();  
    }  
  
    private void showCancelBind(){  
        new SweetAlertDialog(this, SweetAlertDialog.WARNING\_TYPE)  
                .setTitleText("Are you sure?")  
                .setContentText("Won't be able to recover this file!")  
                .setCancelText("No,cancel plx!")  
                .setConfirmText("Yes,delete it!")  
                .showCancelButton(true)  
                .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {  
                    @Override  
                    public void onClick(SweetAlertDialog sDialog) {  
                        sDialog.cancel();  
                    }  
                })  
                .show();  
    }  
  
    private void showPupConfirming(){  
        new SweetAlertDialog(this, SweetAlertDialog.WARNING\_TYPE)  
                .setTitleText("Are you sure?")  
                .setContentText("Won't be able to recover this file!")  
                .setConfirmText("Yes,delete it!")  
                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {  
                    @Override  
                    public void onClick(final SweetAlertDialog pDialog) {  
                       /\* sDialog  
                                .setTitleText("Deleted!")  
                                .setContentText("Your imaginary file has been deleted!")  
                                .setConfirmText("OK")  
                                .setConfirmClickListener(null)  
                                .changeAlertType(SweetAlertDialog.SUCCESS\_TYPE);\*/  
                        pDialog.changeAlertType(SweetAlertDialog.PROGRESS\_TYPE);  
                        pDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.alertDialog\_Progress\_Color));  
                        pDialog.getProgressHelper().setBarWidth(5);  
                        pDialog.getProgressHelper().setCircleRadius(100);  
                        pDialog.getProgressHelper().setRimColor(getResources().getColor(R.color.alertDialog\_Progress\_hintColor));  
                        pDialog.getProgressHelper().setRimWidth(5);  
                        pDialog.setTitleText("Please later...");  
                        pDialog.setContentText("");  
                        pDialog.setCancelable(false);  
                        mHandler.postDelayed(new Runnable() {  
                            @Override  
                            public void run() {  
                                pDialog.setTitleText("Deleted!")  
                                        .setContentText("Your imaginary file has been deleted!")  
                                        .setConfirmText("OK")  
                                        .setConfirmClickListener(null)  
                                        .changeAlertType(SweetAlertDialog.SUCCESS\_TYPE);  
                                pDialog.show();  
                            }  
                        }, 3000);  
                    }  
                })  
                .show();  
    }  
  
    public void dismiss(final SweetAlertDialog pDialog){  
        mHandler.postDelayed(new Runnable() {  
            @Override  
            public void run() {  
                pDialog.dismiss();  
            }  
        }, 5000);  
    }  
  
}

SweetAlertDialog

代码语言:javascript
复制
import android.app.Dialog;  
import android.content.Context;  
import android.graphics.drawable.Drawable;  
import android.os.Bundle;  
import android.os.Build.VERSION;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.view.WindowManager.LayoutParams;  
import android.view.animation.AlphaAnimation;  
import android.view.animation.Animation;  
import android.view.animation.AnimationSet;  
import android.view.animation.Transformation;  
import android.view.animation.Animation.AnimationListener;  
import android.widget.Button;  
import android.widget.FrameLayout;  
import android.widget.ImageView;  
import android.widget.TextView;  
import cn.pedant.SweetAlert.OptAnimationLoader;  
import cn.pedant.SweetAlert.ProgressHelper;  
import cn.pedant.SweetAlert.SuccessTickView;  
import cn.pedant.SweetAlert.R.anim;  
import cn.pedant.SweetAlert.R.drawable;  
import cn.pedant.SweetAlert.R.id;  
import cn.pedant.SweetAlert.R.layout;  
import cn.pedant.SweetAlert.R.style;  
import com.pnikosis.materialishprogress.ProgressWheel;  
import java.util.List;  
  
public class SweetAlertDialog extends Dialog implements 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 SweetAlertDialog.OnSweetClickListener mCancelClickListener;  
    private SweetAlertDialog.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 SweetAlertDialog(Context context) {  
        this(context, 0);  
    }  
  
    public SweetAlertDialog(Context context, int alertType) {  
        super(context, style.alert\_dialog);  
        this.setCancelable(true);  
        this.setCanceledOnTouchOutside(false);  
        this.mProgressHelper = new ProgressHelper(context);  
        this.mAlertType = alertType;  
        this.mErrorInAnim = OptAnimationLoader.loadAnimation(this.getContext(), anim.error\_frame\_in);  
        this.mErrorXInAnim = (AnimationSet)OptAnimationLoader.loadAnimation(this.getContext(), anim.error\_x\_in);  
        if(VERSION.SDK\_INT <= 10) {  
            List childAnims = this.mErrorXInAnim.getAnimations();  
  
            int idx;  
            for(idx = 0; idx < childAnims.size() && !(childAnims.get(idx) instanceof AlphaAnimation); ++idx) {  
                ;  
            }  
  
            if(idx < childAnims.size()) {  
                childAnims.remove(idx);  
            }  
        }  
  
        this.mSuccessBowAnim = OptAnimationLoader.loadAnimation(this.getContext(), anim.success\_bow\_roate);  
        this.mSuccessLayoutAnimSet = (AnimationSet)OptAnimationLoader.loadAnimation(this.getContext(), anim.success\_mask\_layout);  
        this.mModalInAnim = (AnimationSet)OptAnimationLoader.loadAnimation(this.getContext(), anim.modal\_in);  
        this.mModalOutAnim = (AnimationSet)OptAnimationLoader.loadAnimation(this.getContext(), anim.modal\_out);  
        this.mModalOutAnim.setAnimationListener(new AnimationListener() {  
            public void onAnimationStart(Animation animation) {  
            }  
  
            public void onAnimationEnd(Animation animation) {  
                SweetAlertDialog.this.mDialogView.setVisibility(8);  
                SweetAlertDialog.this.mDialogView.post(new Runnable() {  
                    public void run() {  
                        if(SweetAlertDialog.this.mCloseFromCancel) {  
                            SweetAlertDialog.super.cancel();  
                        } else {  
                            SweetAlertDialog.super.dismiss();  
                        }  
  
                    }  
                });  
            }  
  
            public void onAnimationRepeat(Animation animation) {  
            }  
        });  
        this.mOverlayOutAnim = new Animation() {  
            protected void applyTransformation(float interpolatedTime, Transformation t) {  
                LayoutParams wlp = SweetAlertDialog.this.getWindow().getAttributes();  
                wlp.alpha = 1.0F - interpolatedTime;  
                SweetAlertDialog.this.getWindow().setAttributes(wlp);  
            }  
        };  
        this.mOverlayOutAnim.setDuration(120L);  
    }  
  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        this.setContentView(layout.alert\_dialog);  
        this.mDialogView = this.getWindow().getDecorView().findViewById(16908290);  
        this.mTitleTextView = (TextView)this.findViewById(id.title\_text);  
        this.mContentTextView = (TextView)this.findViewById(id.content\_text);  
        this.mErrorFrame = (FrameLayout)this.findViewById(id.error\_frame);  
        this.mErrorX = (ImageView)this.mErrorFrame.findViewById(id.error\_x);  
        this.mSuccessFrame = (FrameLayout)this.findViewById(id.success\_frame);  
        this.mProgressFrame = (FrameLayout)this.findViewById(id.progress\_dialog);  
        this.mSuccessTick = (SuccessTickView)this.mSuccessFrame.findViewById(id.success\_tick);  
        this.mSuccessLeftMask = this.mSuccessFrame.findViewById(id.mask\_left);  
        this.mSuccessRightMask = this.mSuccessFrame.findViewById(id.mask\_right);  
        this.mCustomImage = (ImageView)this.findViewById(id.custom\_image);  
        this.mWarningFrame = (FrameLayout)this.findViewById(id.warning\_frame);  
        this.mConfirmButton = (Button)this.findViewById(id.confirm\_button);  
        this.mCancelButton = (Button)this.findViewById(id.cancel\_button);  
        this.mProgressHelper.setProgressWheel((ProgressWheel)this.findViewById(id.progressWheel));  
        this.mConfirmButton.setOnClickListener(this);  
        this.mCancelButton.setOnClickListener(this);  
        this.setTitleText(this.mTitleText);  
        this.setContentText(this.mContentText);  
        this.setCancelText(this.mCancelText);  
        this.setConfirmText(this.mConfirmText);  
        this.changeAlertType(this.mAlertType, true);  
    }  
  
    private void restore() {  
        this.mCustomImage.setVisibility(8);  
        this.mErrorFrame.setVisibility(8);  
        this.mSuccessFrame.setVisibility(8);  
        this.mWarningFrame.setVisibility(8);  
        this.mProgressFrame.setVisibility(8);  
        this.mConfirmButton.setVisibility(0);  
        this.mConfirmButton.setBackgroundResource(drawable.blue\_button\_background);  
        this.mErrorFrame.clearAnimation();  
        this.mErrorX.clearAnimation();  
        this.mSuccessTick.clearAnimation();  
        this.mSuccessLeftMask.clearAnimation();  
        this.mSuccessRightMask.clearAnimation();  
    }  
  
    private void playAnimation() {  
        if(this.mAlertType == 1) {  
            this.mErrorFrame.startAnimation(this.mErrorInAnim);  
            this.mErrorX.startAnimation(this.mErrorXInAnim);  
        } else if(this.mAlertType == 2) {  
            this.mSuccessTick.startTickAnim();  
            this.mSuccessRightMask.startAnimation(this.mSuccessBowAnim);  
        }  
  
    }  
  
    private void changeAlertType(int alertType, boolean fromCreate) {  
        this.mAlertType = alertType;  
        if(this.mDialogView != null) {  
            if(!fromCreate) {  
                this.restore();  
            }  
  
            switch(this.mAlertType) {  
            case 1:  
                this.mErrorFrame.setVisibility(0);  
                break;  
            case 2:  
                this.mSuccessFrame.setVisibility(0);  
                this.mSuccessLeftMask.startAnimation((Animation)this.mSuccessLayoutAnimSet.getAnimations().get(0));  
                this.mSuccessRightMask.startAnimation((Animation)this.mSuccessLayoutAnimSet.getAnimations().get(1));  
                break;  
            case 3:  
                this.mConfirmButton.setBackgroundResource(drawable.red\_button\_background);  
                this.mWarningFrame.setVisibility(0);  
                break;  
            case 4:  
                this.setCustomImage(this.mCustomImgDrawable);  
                break;  
            case 5:  
                this.mProgressFrame.setVisibility(0);  
                this.mConfirmButton.setVisibility(8);  
            }  
  
            if(!fromCreate) {  
                this.playAnimation();  
            }  
        }  
  
    }  
  
    public int getAlerType() {  
        return this.mAlertType;  
    }  
  
    public void changeAlertType(int alertType) {  
        this.changeAlertType(alertType, false);  
    }  
  
    public String getTitleText() {  
        return this.mTitleText;  
    }  
  
    public SweetAlertDialog setTitleText(String text) {  
        this.mTitleText = text;  
        if(this.mTitleTextView != null && this.mTitleText != null) {  
            this.mTitleTextView.setText(this.mTitleText);  
        }  
  
        return this;  
    }  
  
    public SweetAlertDialog setCustomImage(Drawable drawable) {  
        this.mCustomImgDrawable = drawable;  
        if(this.mCustomImage != null && this.mCustomImgDrawable != null) {  
            this.mCustomImage.setVisibility(0);  
            this.mCustomImage.setImageDrawable(this.mCustomImgDrawable);  
        }  
  
        return this;  
    }  
  
    public SweetAlertDialog setCustomImage(int resourceId) {  
        return this.setCustomImage(this.getContext().getResources().getDrawable(resourceId));  
    }  
  
    public String getContentText() {  
        return this.mContentText;  
    }  
  
    public SweetAlertDialog setContentText(String text) {  
        this.mContentText = text;  
        if(this.mContentTextView != null && this.mContentText != null) {  
            this.showContentText(true);  
            this.mContentTextView.setText(this.mContentText);  
        }  
  
        return this;  
    }  
  
    public boolean isShowCancelButton() {  
        return this.mShowCancel;  
    }  
  
    public SweetAlertDialog showCancelButton(boolean isShow) {  
        this.mShowCancel = isShow;  
        if(this.mCancelButton != null) {  
            this.mCancelButton.setVisibility(this.mShowCancel?0:8);  
        }  
  
        return this;  
    }  
  
    public boolean isShowContentText() {  
        return this.mShowContent;  
    }  
  
    public SweetAlertDialog showContentText(boolean isShow) {  
        this.mShowContent = isShow;  
        if(this.mContentTextView != null) {  
            this.mContentTextView.setVisibility(this.mShowContent?0:8);  
        }  
  
        return this;  
    }  
  
    public String getCancelText() {  
        return this.mCancelText;  
    }  
  
    public SweetAlertDialog setCancelText(String text) {  
        this.mCancelText = text;  
        if(this.mCancelButton != null && this.mCancelText != null) {  
            this.showCancelButton(true);  
            this.mCancelButton.setText(this.mCancelText);  
        }  
  
        return this;  
    }  
  
    public String getConfirmText() {  
        return this.mConfirmText;  
    }  
  
    public SweetAlertDialog setConfirmText(String text) {  
        this.mConfirmText = text;  
        if(this.mConfirmButton != null && this.mConfirmText != null) {  
            this.mConfirmButton.setText(this.mConfirmText);  
        }  
  
        return this;  
    }  
  
    public SweetAlertDialog setCancelClickListener(SweetAlertDialog.OnSweetClickListener listener) {  
        this.mCancelClickListener = listener;  
        return this;  
    }  
  
    public SweetAlertDialog setConfirmClickListener(SweetAlertDialog.OnSweetClickListener listener) {  
        this.mConfirmClickListener = listener;  
        return this;  
    }  
  
    protected void onStart() {  
        this.mDialogView.startAnimation(this.mModalInAnim);  
        this.playAnimation();  
    }  
  
    public void cancel() {  
        this.dismissWithAnimation(true);  
    }  
  
    public void dismissWithAnimation() {  
        this.dismissWithAnimation(false);  
    }  
  
    private void dismissWithAnimation(boolean fromCancel) {  
        this.mCloseFromCancel = fromCancel;  
        this.mConfirmButton.startAnimation(this.mOverlayOutAnim);  
        this.mDialogView.startAnimation(this.mModalOutAnim);  
    }  
  
    public void onClick(View v) {  
        if(v.getId() == id.cancel\_button) {  
            if(this.mCancelClickListener != null) {  
                this.mCancelClickListener.onClick(this);  
            } else {  
                this.dismissWithAnimation();  
            }  
        } else if(v.getId() == id.confirm\_button) {  
            if(this.mConfirmClickListener != null) {  
                this.mConfirmClickListener.onClick(this);  
            } else {  
                this.dismissWithAnimation();  
            }  
        }  
  
    }  
  
    public ProgressHelper getProgressHelper() {  
        return this.mProgressHelper;  
    }  
  
    public interface OnSweetClickListener {  
        void onClick(SweetAlertDialog var1);  
    }  
}

Android零基础系列教程:Android基础课程UI-动画_哔哩哔哩_bilibili

本文转自 https://blog.51cto.com/u_15127630/4604864,如有侵权,请联系删除。

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档