十七、自定义进度对话框

矫情吗;* 2022-09-25 08:26 285阅读 0赞

当用户进行需要等待一段时间的操作的时候,我们需要给用户一个等待的反馈,不然用户会以为你的APP卡死了,一般会用一个圆圈来转,但是系统提供的圆圈实在是太丑,所以我自定义一个进度对话框。

进度对话框由两部分组成,一个转的圆圈,和提示文字,布局如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="horizontal"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content"
  6. android:background="@drawable/dialog_bg"
  7. android:padding="5dp"
  8. android:gravity="center_vertical">
  9. <ImageView
  10. android:id="@+id/iv_progress"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:src="@drawable/loading"/>
  14. <TextView
  15. android:id="@+id/tv_msg"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:layout_marginLeft="5dp"
  19. android:layout_marginRight="5dp"
  20. android:text="加载中..."
  21. android:textColor="@color/font_white"
  22. android:textSize="@dimen/font_smaller"/>
  23. </LinearLayout>

自定义进度对话框代码:

  1. public class CustomProgressDialog extends BaseDialog{
  2. private ImageView progressImageView;
  3. private TextView msgTextView;
  4. private Animation anim;
  5. /**
  6. *
  7. * @param context
  8. * @param isCancelOutside 点击界外关闭dialog
  9. * @param canBack 返回键关闭dialog
  10. */
  11. public CustomProgressDialog(Context context, boolean isCancelOutside, final boolean canBack) {
  12. super(context);
  13. if (view == null) {
  14. view = LayoutInflater.from(context).inflate(R.layout.dialog_progress, null);
  15. }
  16. setContentView(view);
  17. initView();
  18. setCanceledOnTouchOutside(isCancelOutside);
  19. setOnKeyListener(new OnKeyListener() {
  20. @Override
  21. public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
  22. if (keyCode == KeyEvent.KEYCODE_BACK && !canBack) {
  23. // 过滤返回键
  24. return true;
  25. }
  26. return false;
  27. }
  28. });
  29. setOnShowListener(new OnShowListener() {
  30. @Override
  31. public void onShow(DialogInterface dialog) {
  32. // 开始旋转动画
  33. progressImageView.startAnimation(anim);
  34. }
  35. });
  36. }
  37. private void initView() {
  38. progressImageView = (ImageView) view.findViewById(R.id.iv_progress);
  39. msgTextView = (TextView) view.findViewById(R.id.tv_msg);
  40. anim = AnimationUtils.loadAnimation(context, R.anim.dialog_rotate);
  41. }
  42. /**
  43. * 设置提示文字内容
  44. * @param msg
  45. */
  46. public void setMessage(String msg) {
  47. if (msg != null) {
  48. msgTextView.setText(msg);
  49. }
  50. }
  51. }

这样自定义对话框的内容就完成了,这样显然逼格不够,我们做一个对话框管理类,安全显示和关闭对话框:

  1. public class DialogManager {
  2. /**
  3. * 创建进度对话框
  4. * @param context
  5. * @param isCancelOutside 点击界外关闭dialog
  6. * @param canBack 返回键关闭dialog
  7. * @return
  8. */
  9. public static CustomProgressDialog createProgressDialog(Context context, boolean isCancelOutside, final boolean canBack) {
  10. return new CustomProgressDialog(context, isCancelOutside, canBack);
  11. }
  12. /**
  13. * 安全打开dialog
  14. * @param activity
  15. * @param dialog
  16. */
  17. public static void showDialog(Activity activity, Dialog dialog) {
  18. if (!activity.isFinishing() && dialog!=null && !dialog.isShowing()) {
  19. dialog.show();
  20. }
  21. }
  22. /**
  23. * 安全关闭dialog
  24. * @param dialog
  25. */
  26. public static void dismissDialog(Dialog dialog) {
  27. if (dialog!=null && dialog.isShowing()) {
  28. dialog.dismiss();
  29. }
  30. }
  31. }

在activity中去打开对话框,并且设置提示文字内容:

  1. CustomProgressDialog dialog = DialogManager.createProgressDialog(getActivity(), false, true);
  2. dialog.setMessage("上传中");
  3. DialogManager.showDialog(getActivity(), dialog);

效果如下:

Center

满心欢喜的以为我们做了一个逼格高大上的dialog,结果发现丑得掉渣,都是样式style惹的祸,我们在style文件中自定义dialog样式:

  1. <style name="CommonDialog" parent="@android:style/Theme.Dialog">
  2. <item name="android:windowFrame">@null</item>
  3. <item name="android:windowIsFloating">true</item>
  4. <item name="android:windowIsTranslucent">true</item>
  5. <item name="android:windowNoTitle">true</item>
  6. <item name="android:background">@null</item>
  7. <item name="android:windowFullscreen">true</item>
  8. <item name="android:windowBackground">@color/transparent_color</item>
  9. <item name="android:backgroundDimEnabled">false</item>
  10. <item name="android:windowAnimationStyle">@null</item>
  11. </style>

在我们的dialog的基类文件BaseDialog去加载样式文件:

  1. public BaseDialog(Context context) {
  2. super(context, R.style.CommonDialog);
  3. }

好了,再来看看效果吧:

Center 1

参考:

http://blog.csdn.net/wwj_748/article/details/24592747

http://www.android100.org/html/201304/24/2282.html

发表评论

表情:
评论列表 (有 0 条评论,285人围观)

还没有评论,来说两句吧...

相关阅读

    相关 定义对话框详解

    不鸡汤上干货 本篇博客内容:讲解如何自定义对话框,并且介绍如何使用(纯手敲,会有点小错,自己改下吧),本功能也可以适用于不同的窗体之间传递参数 可解决问题:系统默认对话框的

    相关 定义进度对话框

    当用户进行需要等待一段时间的操作的时候,我们需要给用户一个等待的反馈,不然用户会以为你的APP卡死了,一般会用一个圆圈来转,但是系统提供的圆圈实在是太丑,所以我自定义一个进度对