当前位置:   article > 正文

android 自定义Dialog提示+动画效果(完整版)_android 提示dialog

android 提示dialog

 一个可以直接使用的自定义AlertDialog,封装的很完整的一个Demo,可以灵活使用,里面提示图标还额外添加了动画效果,希望可以帮到大家。不多说,展示一部分代码给大家。

文章后面会给出源码。欢迎大家下载!!!​​​​​​​

效果图:

// 调用的Activity​​​​​​​

  1. package com.pnikosis.materialishprogress;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.View.OnClickListener;
  6. import android.widget.Toast;
  7. import cn.pedant.SweetAlert.SweetAlertDialog;
  8. import cn.pedant.SweetAlert.SweetAlertDialog.OnSweetClickListener;
  9. public class MainActivity extends Activity {
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main1);
  14. findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
  15. @Override
  16. public void onClick(View v) {
  17. showAlertDiaLog(1, null, "Hello.Title", false, true,
  18. new OnSweetClickListener() {
  19. @Override
  20. public void onClick(
  21. SweetAlertDialog sweetAlertDialog) {
  22. Toast.makeText(getApplicationContext(), "NO", 0)
  23. .show();
  24. }
  25. }, new OnSweetClickListener() {
  26. @Override
  27. public void onClick(
  28. SweetAlertDialog sweetAlertDialog) {
  29. Toast.makeText(getApplicationContext(), "OK", 0).show();
  30. }
  31. });
  32. }
  33. });
  34. findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
  35. @Override
  36. public void onClick(View v) {
  37. showAlertDiaLog(2, null, "Hello.Title1", false, true,
  38. new OnSweetClickListener() {
  39. @Override
  40. public void onClick(
  41. SweetAlertDialog sweetAlertDialog) {
  42. Toast.makeText(getApplicationContext(), "NO", 0)
  43. .show();
  44. }
  45. }, new OnSweetClickListener() {
  46. @Override
  47. public void onClick(
  48. SweetAlertDialog sweetAlertDialog) {
  49. Toast.makeText(getApplicationContext(), "OK", 0)
  50. .show();
  51. }
  52. });
  53. }
  54. });
  55. findViewById(R.id.button3).setOnClickListener(new OnClickListener() {
  56. @Override
  57. public void onClick(View v) {
  58. showAlertDiaLog(3, "NO", "Hello.Title2", true, true,
  59. new OnSweetClickListener() {
  60. @Override
  61. public void onClick(
  62. SweetAlertDialog sweetAlertDialog) {
  63. Toast.makeText(getApplicationContext(), "NO", 0)
  64. .show();
  65. }
  66. }, new OnSweetClickListener() {
  67. @Override
  68. public void onClick(
  69. SweetAlertDialog sweetAlertDialog) {
  70. Toast.makeText(getApplicationContext(), "OK", 0)
  71. .show();
  72. }
  73. });
  74. }
  75. });
  76. findViewById(R.id.button4).setOnClickListener(new OnClickListener() {
  77. @Override
  78. public void onClick(View v) {
  79. showAlertDiaLog(0, "NO", "Hello.Title3", true, true,
  80. new OnSweetClickListener() {
  81. @Override
  82. public void onClick(
  83. SweetAlertDialog sweetAlertDialog) {
  84. Toast.makeText(getApplicationContext(), "NO", 0)
  85. .show();
  86. }
  87. }, new OnSweetClickListener() {
  88. @Override
  89. public void onClick(
  90. SweetAlertDialog sweetAlertDialog) {
  91. Toast.makeText(getApplicationContext(), "OK", 0)
  92. .show();
  93. }
  94. });
  95. }
  96. });
  97. }
  98. public void showAlertDiaLog(int type, String CancelText, String TitleText,
  99. boolean CancelButton, boolean ContentText,
  100. OnSweetClickListener CancelClickListener,
  101. OnSweetClickListener ConfirmClickListener) {
  102. SweetAlertDialog alertDialog = new SweetAlertDialog(MainActivity.this);
  103. /**
  104. * AlertType 显示图标类型 1.错误提示 2.正确提示 3.感叹号提示 其他.不显示图标
  105. */
  106. alertDialog.changeAlertType(type);
  107. /**
  108. * 是否显示取消按钮 true显示 false不显示
  109. */
  110. alertDialog.setCancelText(CancelText);
  111. alertDialog.showCancelButton(CancelButton);
  112. /**
  113. * 是否显示提示内容 true显示 false不显示
  114. */
  115. alertDialog.setTitleText(TitleText);
  116. alertDialog.showContentText(ContentText);
  117. /**
  118. * 取消按钮的点击事件
  119. */
  120. alertDialog.setCancelClickListener(CancelClickListener);
  121. /**
  122. * 确定按钮的点击事件
  123. */
  124. alertDialog.setConfirmClickListener(ConfirmClickListener);
  125. alertDialog.show();
  126. }
  127. }

 //自定义Dialog​​​​​​​

  1. package cn.pedant.SweetAlert;
  2. import java.util.List;
  3. import android.app.Dialog;
  4. import android.content.Context;
  5. import android.graphics.drawable.Drawable;
  6. import android.os.Build;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.view.WindowManager;
  10. import android.view.animation.AlphaAnimation;
  11. import android.view.animation.Animation;
  12. import android.view.animation.AnimationSet;
  13. import android.view.animation.Transformation;
  14. import android.widget.Button;
  15. import android.widget.FrameLayout;
  16. import android.widget.ImageView;
  17. import android.widget.TextView;
  18. import com.pnikosis.materialishprogress.ProgressWheel;
  19. import com.pnikosis.materialishprogress.R;
  20. public class SweetAlertDialog extends Dialog implements View.OnClickListener {
  21. private View mDialogView;
  22. private AnimationSet mModalInAnim;
  23. private AnimationSet mModalOutAnim;
  24. private Animation mOverlayOutAnim;
  25. private Animation mErrorInAnim;
  26. private AnimationSet mErrorXInAnim;
  27. private AnimationSet mSuccessLayoutAnimSet;
  28. private Animation mSuccessBowAnim;
  29. private TextView mTitleTextView;
  30. private TextView mContentTextView;
  31. private String mTitleText;
  32. private String mContentText;
  33. private boolean mShowCancel;
  34. private boolean mShowContent;
  35. private String mCancelText;
  36. private String mConfirmText;
  37. private int mAlertType;
  38. private FrameLayout mErrorFrame;
  39. private FrameLayout mSuccessFrame;
  40. private FrameLayout mProgressFrame;
  41. private SuccessTickView mSuccessTick;
  42. private ImageView mErrorX;
  43. private View mSuccessLeftMask;
  44. private View mSuccessRightMask;
  45. private Drawable mCustomImgDrawable;
  46. private ImageView mCustomImage;
  47. private Button mConfirmButton;
  48. private Button mCancelButton;
  49. private ProgressHelper mProgressHelper;
  50. private FrameLayout mWarningFrame;
  51. private OnSweetClickListener mCancelClickListener;
  52. private OnSweetClickListener mConfirmClickListener;
  53. private boolean mCloseFromCancel;
  54. public static final int NORMAL_TYPE = 0;
  55. public static final int ERROR_TYPE = 1;
  56. public static final int SUCCESS_TYPE = 2;
  57. public static final int WARNING_TYPE = 3;
  58. public static final int CUSTOM_IMAGE_TYPE = 4;
  59. public static final int PROGRESS_TYPE = 5;
  60. public static interface OnSweetClickListener {
  61. public void onClick(SweetAlertDialog sweetAlertDialog);
  62. }
  63. public SweetAlertDialog(Context context) {
  64. this(context, NORMAL_TYPE);
  65. }
  66. public SweetAlertDialog(Context context, int alertType) {
  67. super(context, R.style.alert_dialog);
  68. setCancelable(true);
  69. setCanceledOnTouchOutside(false);
  70. mProgressHelper = new ProgressHelper(context);
  71. mAlertType = alertType;
  72. mErrorInAnim = OptAnimationLoader.loadAnimation(getContext(),
  73. R.anim.error_frame_in);
  74. mErrorXInAnim = (AnimationSet) OptAnimationLoader.loadAnimation(
  75. getContext(), R.anim.error_x_in);
  76. // 2.3.x system don't support alpha-animation on layer-list drawable
  77. // remove it from animation set
  78. if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
  79. List<Animation> childAnims = mErrorXInAnim.getAnimations();
  80. int idx = 0;
  81. for (; idx < childAnims.size(); idx++) {
  82. if (childAnims.get(idx) instanceof AlphaAnimation) {
  83. break;
  84. }
  85. }
  86. if (idx < childAnims.size()) {
  87. childAnims.remove(idx);
  88. }
  89. }
  90. mSuccessBowAnim = OptAnimationLoader.loadAnimation(getContext(),
  91. R.anim.success_bow_roate);
  92. mSuccessLayoutAnimSet = (AnimationSet) OptAnimationLoader
  93. .loadAnimation(getContext(), R.anim.success_mask_layout);
  94. mModalInAnim = (AnimationSet) OptAnimationLoader.loadAnimation(
  95. getContext(), R.anim.modal_in);
  96. mModalOutAnim = (AnimationSet) OptAnimationLoader.loadAnimation(
  97. getContext(), R.anim.modal_out);
  98. mModalOutAnim.setAnimationListener(new Animation.AnimationListener() {
  99. @Override
  100. public void onAnimationStart(Animation animation) {
  101. }
  102. @Override
  103. public void onAnimationEnd(Animation animation) {
  104. mDialogView.setVisibility(View.GONE);
  105. mDialogView.post(new Runnable() {
  106. @Override
  107. public void run() {
  108. if (mCloseFromCancel) {
  109. SweetAlertDialog.super.cancel();
  110. } else {
  111. SweetAlertDialog.super.dismiss();
  112. }
  113. }
  114. });
  115. }
  116. @Override
  117. public void onAnimationRepeat(Animation animation) {
  118. }
  119. });
  120. // dialog overlay fade out
  121. mOverlayOutAnim = new Animation() {
  122. @Override
  123. protected void applyTransformation(float interpolatedTime,
  124. Transformation t) {
  125. WindowManager.LayoutParams wlp = getWindow().getAttributes();
  126. wlp.alpha = 1 - interpolatedTime;
  127. getWindow().setAttributes(wlp);
  128. }
  129. };
  130. mOverlayOutAnim.setDuration(120);
  131. }
  132. protected void onCreate(Bundle savedInstanceState) {
  133. super.onCreate(savedInstanceState);
  134. setContentView(R.layout.alert_dialog);
  135. mDialogView = getWindow().getDecorView().findViewById(
  136. android.R.id.content);
  137. mTitleTextView = (TextView) findViewById(R.id.title_text);
  138. mContentTextView = (TextView) findViewById(R.id.content_text);
  139. mErrorFrame = (FrameLayout) findViewById(R.id.error_frame);
  140. mErrorX = (ImageView) mErrorFrame.findViewById(R.id.error_x);
  141. mSuccessFrame = (FrameLayout) findViewById(R.id.success_frame);
  142. mProgressFrame = (FrameLayout) findViewById(R.id.progress_dialog);
  143. mSuccessTick = (SuccessTickView) mSuccessFrame
  144. .findViewById(R.id.success_tick);
  145. mSuccessLeftMask = mSuccessFrame.findViewById(R.id.mask_left);
  146. mSuccessRightMask = mSuccessFrame.findViewById(R.id.mask_right);
  147. mCustomImage = (ImageView) findViewById(R.id.custom_image);
  148. mWarningFrame = (FrameLayout) findViewById(R.id.warning_frame);
  149. mConfirmButton = (Button) findViewById(R.id.confirm_button);
  150. mCancelButton = (Button) findViewById(R.id.cancel_button);
  151. mProgressHelper
  152. .setProgressWheel((ProgressWheel) findViewById(R.id.progressWheel));
  153. mConfirmButton.setOnClickListener(this);
  154. mCancelButton.setOnClickListener(this);
  155. setTitleText(mTitleText);
  156. setContentText(mContentText);
  157. setCancelText(mCancelText);
  158. setConfirmText(mConfirmText);
  159. changeAlertType(mAlertType, true);
  160. }
  161. private void restore() {
  162. mCustomImage.setVisibility(View.GONE);
  163. mErrorFrame.setVisibility(View.GONE);
  164. mSuccessFrame.setVisibility(View.GONE);
  165. mWarningFrame.setVisibility(View.GONE);
  166. mProgressFrame.setVisibility(View.GONE);
  167. mConfirmButton.setVisibility(View.VISIBLE);
  168. mConfirmButton.setBackgroundResource(R.drawable.blue_button_background);
  169. mErrorFrame.clearAnimation();
  170. mErrorX.clearAnimation();
  171. mSuccessTick.clearAnimation();
  172. mSuccessLeftMask.clearAnimation();
  173. mSuccessRightMask.clearAnimation();
  174. }
  175. private void playAnimation() {
  176. if (mAlertType == ERROR_TYPE) {
  177. mErrorFrame.startAnimation(mErrorInAnim);
  178. mErrorX.startAnimation(mErrorXInAnim);
  179. } else if (mAlertType == SUCCESS_TYPE) {
  180. mSuccessTick.startTickAnim();
  181. mSuccessRightMask.startAnimation(mSuccessBowAnim);
  182. }
  183. }
  184. private void changeAlertType(int alertType, boolean fromCreate) {
  185. mAlertType = alertType;
  186. // call after created views
  187. if (mDialogView != null) {
  188. if (!fromCreate) {
  189. // restore all of views state before switching alert type
  190. restore();
  191. }
  192. switch (mAlertType) {
  193. case ERROR_TYPE:
  194. mErrorFrame.setVisibility(View.VISIBLE);
  195. break;
  196. case SUCCESS_TYPE:
  197. mSuccessFrame.setVisibility(View.VISIBLE);
  198. // initial rotate layout of success mask
  199. mSuccessLeftMask.startAnimation(mSuccessLayoutAnimSet
  200. .getAnimations().get(0));
  201. mSuccessRightMask.startAnimation(mSuccessLayoutAnimSet
  202. .getAnimations().get(1));
  203. break;
  204. case WARNING_TYPE:
  205. mConfirmButton
  206. .setBackgroundResource(R.drawable.red_button_background);
  207. mWarningFrame.setVisibility(View.VISIBLE);
  208. break;
  209. case CUSTOM_IMAGE_TYPE:
  210. setCustomImage(mCustomImgDrawable);
  211. break;
  212. case PROGRESS_TYPE:
  213. mProgressFrame.setVisibility(View.VISIBLE);
  214. mConfirmButton.setVisibility(View.GONE);
  215. break;
  216. }
  217. if (!fromCreate) {
  218. playAnimation();
  219. }
  220. }
  221. }
  222. public int getAlerType() {
  223. return mAlertType;
  224. }
  225. public void changeAlertType(int alertType) {
  226. changeAlertType(alertType, false);
  227. }
  228. public String getTitleText() {
  229. return mTitleText;
  230. }
  231. public SweetAlertDialog setTitleText(String text) {
  232. mTitleText = text;
  233. if (mTitleTextView != null && mTitleText != null) {
  234. mTitleTextView.setText(mTitleText);
  235. }
  236. return this;
  237. }
  238. public SweetAlertDialog setCustomImage(Drawable drawable) {
  239. mCustomImgDrawable = drawable;
  240. if (mCustomImage != null && mCustomImgDrawable != null) {
  241. mCustomImage.setVisibility(View.VISIBLE);
  242. mCustomImage.setImageDrawable(mCustomImgDrawable);
  243. }
  244. return this;
  245. }
  246. public SweetAlertDialog setCustomImage(int resourceId) {
  247. return setCustomImage(getContext().getResources().getDrawable(
  248. resourceId));
  249. }
  250. public String getContentText() {
  251. return mContentText;
  252. }
  253. public SweetAlertDialog setContentText(String text) {
  254. mContentText = text;
  255. if (mContentTextView != null && mContentText != null) {
  256. showContentText(true);
  257. mContentTextView.setText(mContentText);
  258. }
  259. return this;
  260. }
  261. public boolean isShowCancelButton() {
  262. return mShowCancel;
  263. }
  264. public SweetAlertDialog showCancelButton(boolean isShow) {
  265. mShowCancel = isShow;
  266. if (mCancelButton != null) {
  267. mCancelButton.setVisibility(mShowCancel ? View.VISIBLE : View.GONE);
  268. }
  269. return this;
  270. }
  271. public boolean isShowContentText() {
  272. return mShowContent;
  273. }
  274. public SweetAlertDialog showContentText(boolean isShow) {
  275. mShowContent = isShow;
  276. if (mContentTextView != null) {
  277. mContentTextView.setVisibility(mShowContent ? View.VISIBLE
  278. : View.GONE);
  279. }
  280. return this;
  281. }
  282. public String getCancelText() {
  283. return mCancelText;
  284. }
  285. public SweetAlertDialog setCancelText(String text) {
  286. mCancelText = text;
  287. if (mCancelButton != null && mCancelText != null) {
  288. showCancelButton(true);
  289. mCancelButton.setText(mCancelText);
  290. }
  291. return this;
  292. }
  293. public String getConfirmText() {
  294. return mConfirmText;
  295. }
  296. public SweetAlertDialog setConfirmText(String text) {
  297. mConfirmText = text;
  298. if (mConfirmButton != null && mConfirmText != null) {
  299. mConfirmButton.setText(mConfirmText);
  300. }
  301. return this;
  302. }
  303. public SweetAlertDialog setCancelClickListener(OnSweetClickListener listener) {
  304. mCancelClickListener = listener;
  305. return this;
  306. }
  307. public SweetAlertDialog setConfirmClickListener(
  308. OnSweetClickListener listener) {
  309. mConfirmClickListener = listener;
  310. return this;
  311. }
  312. protected void onStart() {
  313. mDialogView.startAnimation(mModalInAnim);
  314. playAnimation();
  315. }
  316. /**
  317. * The real Dialog.cancel() will be invoked async-ly after the animation
  318. * finishes.
  319. */
  320. @Override
  321. public void cancel() {
  322. dismissWithAnimation(true);
  323. }
  324. /**
  325. * The real Dialog.dismiss() will be invoked async-ly after the animation
  326. * finishes.
  327. */
  328. public void dismissWithAnimation() {
  329. dismissWithAnimation(false);
  330. }
  331. private void dismissWithAnimation(boolean fromCancel) {
  332. mCloseFromCancel = fromCancel;
  333. mConfirmButton.startAnimation(mOverlayOutAnim);
  334. mDialogView.startAnimation(mModalOutAnim);
  335. }
  336. @Override
  337. public void onClick(View v) {
  338. if (v.getId() == R.id.cancel_button) {
  339. if (mCancelClickListener != null) {
  340. mCancelClickListener.onClick(SweetAlertDialog.this);
  341. dismissWithAnimation();
  342. } else {
  343. dismissWithAnimation();
  344. }
  345. } else if (v.getId() == R.id.confirm_button) {
  346. if (mConfirmClickListener != null) {
  347. mConfirmClickListener.onClick(SweetAlertDialog.this);
  348. dismissWithAnimation();
  349. } else {
  350. dismissWithAnimation();
  351. }
  352. }
  353. }
  354. public ProgressHelper getProgressHelper() {
  355. return mProgressHelper;
  356. }
  357. }


源码下载地址icon-default.png?t=N7T8http://download.csdn.net/detail/u013752594/8901985​​​​​​​​​​​​​​

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/318535
推荐阅读
相关标签
  

闽ICP备14008679号