Android studio SweetAlert for Android
找到个开源项目。github:https://github.com/pedant/sweet-alert-dialog
效果图:https://raw.githubusercontent.com/pedant/sweet-alert-dialog/master/change_type.gif
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQW5kZHJvaWRfTGFuWWFu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
build.gradle
compile 'cn.pedant.sweetalert:library:1.3'
colors.xml
<? 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
<?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
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
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);
}
}
项目引用依赖:
进度条项目github:https://github.com/pnikosis/materialish-progress
demo项目地址:http://download.csdn.net/detail/anddroid_lanyan/8859017
新增:Eclipse Demo源代码地址 http://download.csdn.net/detail/anddroid_lanyan/8861939
Android studio SweetAlert for Android的更多相关文章
- android studio :com.android.support:appcompat-v7:21.+ 报错
android studio :com.android.support:appcompat-v7:21.+ 报错: 在project——>app——>build.gradle修改: app ...
- 【Android Studio】为Android Studio设置HTTP代理
[Android Studio]为Android Studio设置HTTP代理 大陆的墙很厚很高,初次安装Android Studio下载SDK等必定失败,设置代理方法如下: 1. 到androi ...
- 在Android Studio中打开Android Device Monitor时报错的解决方法
在Android Studio中打开Android Device Monitor时报以下错误时(Android-SDK\tools\lib\monitor-x86_64\configuration\1 ...
- 【转】在Android Studio中下载Android SDK的两种方式(Android Studio3.0、windows)
在Android Studio中下载Android SDK的两种方式(Android Studio3.0.windows) 方式一.设置HTTP Proxy1. 打开Settings2. 点击HTTP ...
- Android Studio快捷键【Android学习入门】
Studio快捷键[Android学习入门]" title="Android Studio快捷键[Android学习入门]"> 提示 Ctrl+P方法参数提示 Ct ...
- Android studio怎么创建Android虚拟机?
进行Android studio中进行开发app应用的情况,如果在进行调式app的应用的情况下,没有真机手机机器是没有办法调式的,那么只能通过Android studio中sdk提供虚拟机进行调式ap ...
- Android Studio(八):Android Studio设置教程
Android Studio相关博客: Android Studio(一):介绍.安装.配置 Android Studio(二):快捷键设置.插件安装 Android Studio(三):设置Andr ...
- Android Studio(六):Android Studio添加注释模板
Android Studio相关博客: Android Studio(一):介绍.安装.配置 Android Studio(二):快捷键设置.插件安装 Android Studio(三):设置Andr ...
- Android Studio(四):Android Studio集成Genymotion
Android Studio相关博客: Android Studio(一):介绍.安装.配置 Android Studio(二):快捷键设置.插件安装 Android Studio(三):设置Andr ...
随机推荐
- 【C++基础之十四】函参的缺省
可能会有这么一个函数,在大部分的情况下,我们不用给它传递参数,但在某些特殊情况下,我们需要给它传递参数,那怎么办呢? 简单啊,写两个一样的方法,一个带参,一个不带参... 这样也太没水准了.来点高端的 ...
- GDB+GDBServer调试Linux应用程序
参考:http://blog.csdn.net/shanghaiqianlun/article/details/7820401 一.gdb+gdbserver总体介绍 远程调试环境由宿主机GDB和目标 ...
- 格而知之8:我所理解的Runtime(3)
关联对象 14.使用Category对类进行拓展的时候,只能添加方法,而不适合添加属性(可以添加属性,也可以正常使用get方法和set方法,只是不会自动生成以下划线开头命名的成员变量). 可以通过关联 ...
- js获取浏览器的keydown事件(附keycode码)
<script type="text/javascript" language=JavaScript charset="UTF-8"> docume ...
- js埋点(转载)
页面埋点的作用,其实就是用于流量分析.而流量的意思,包含了很多:页面浏览数(PV).独立访问者数量(UV).IP.页面停留时间.页面操作时间.页面访问次数.按钮点击次数.文件下载次数等.而流量分析又有 ...
- 有二级目录的IIS配置
当项目配置文件中配置了二级目录时,如下: <!--二级目录地址--> <add key="SecondCatalog" value="/hotel&qu ...
- 第一个MyBatis程序
最近研究了一些MyBatis技术,虽然工作中还未用到,但是觉得了解一下也是不错的.这里记录了第一个简单的Demo程序,防止自己忘记. 第一步需要配置Mybatis-config.xml文件.注意:这里 ...
- Android 的权限设置大全
android.permission.ACCESS_CHECKIN_PROPERTIES //同意读写訪问"properties"表在checkin数据库中.改值可以改动上传 an ...
- git使用经验
一直想写一点关于git的文章,但是平时太懒了,没有写,现在写些经验这里,方便以后自己忘记了.
- Android一次退出所有Activity的方法(升级版)
一.思路和方法: 首先创建一个ActivityManager类来存放Activity的对象. 返回ActivityManager的对象,供BaseActivity来进行操作. 所有其他子Activit ...