Android中ProgressDialog的用法

Android中有一个进度对话框,可以实现等待进度和具有具体数值进度的显示。说白了,一个是圆形进度条,一个是横向进度条。但是原生的,大家都明白,比较丑,一般不是很符合公司UI的规范。正常情况下我们都是要自己自定义一个Dialog或者使用三方的一些控件库。不过如果我们开发一些对于细节UI要求不是很高,可以使用原生控件的弹框等场景,可以考虑使用这种控件,毕竟不用引用三方,也不用自定义,使用起来自然是快捷方便的。所以今天我们介绍一下这个ProgressDialog对话框。

1.ProgressDialog相关常用方法

  • setTitle:设置对话框的标题文本。
  • setMessage:这是对话框的消息内容。
  • setIcon:设置对话框的图标。
  • setProgress:设置当前进度的数值。
  • setSecondaryProgress:设置当前第二进度的数值。
  • setMax:设置进度条的最大进度数值。
  • setProgressStyle:设置进度条的样式。取值ProgressDialog.STYLE_SPINNER表示转圈风格(默认值),取值ProgressDialog.STYLE_HORIZONTAL表示长条风格。
  • show:显示对话框。需要在各属性设置完成后调用show方法。
  • isShowing:判断对话框是否正在显示。
  • dismiss:关闭对话框
  • 静态的show方法:简化的调用方法,一句代码就搞定进度对话框的设置与显示。可同时指定标题文字和消息内容吗,进度条样式为默认的转圈。

2.ProgressDialog网络加载等待框的简单使用示例

定义一个成员变量

private ProgressDialog progressDialog;

显示对话框

progressDialog = ProgressDialog.show(this,"","正在努力加载页面");

消失对话框

progressDialog.dismiss();

样式如下

3.ProgressDialog进度对话框的简单使用示例

创建属性

private ProgressDialog progressDialog;

设置相关参数

progressDialog = new ProgressDialog(this);
progressDialog.setTitle("请稍后");
progressDialog.setMessage("正在努力加载页面");
progressDialog.setMax(100);
progressDialog.setProgress(0);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.show();

样式如下

  • 2
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想要在 Android 应用使用自定义的加载动画,可以通过创建自定义的布局和使用 Animation 类来实现。以下是一个简单的示例,演示如何在 Android 应用自定义加载动画: 首先,在 res/layout 目录下创建一个新的布局文件,例如 custom_loading.xml,其包含自定义的加载动画布局,例如: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center"> <ImageView android:id="@+id/loading_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/custom_loading_animation" android:layout_marginBottom="16dp"/> <TextView android:id="@+id/loading_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Loading..." android:textColor="@android:color/white"/> </LinearLayout> ``` 其,loading_image 是一个自定义的加载动画的 ImageView,loading_text 是一个文本视图,用于显示加载消息。 接下来,在 res/drawable 目录下创建一个新的动画文件,例如 custom_loading_animation.xml,其包含自定义的加载动画动画,例如: ```xml <?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/custom_loading_frame1" android:duration="100" /> <item android:drawable="@drawable/custom_loading_frame2" android:duration="100" /> <item android:drawable="@drawable/custom_loading_frame3" android:duration="100" /> <item android:drawable="@drawable/custom_loading_frame4" android:duration="100" /> </animation-list> ``` 其,custom_loading_frame1、custom_loading_frame2、custom_loading_frame3、custom_loading_frame4 是自定义的加载动画的帧。 最后,在你的 Activity ,使用 LayoutInflater 类将 custom_loading.xml 布局文件实例化为 View 对象,并使用 Animation 类将 custom_loading_animation.xml 动画文件加载到 ImageView ,例如: ```java LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.custom_loading, null); ImageView imageView = (ImageView) view.findViewById(R.id.loading_image); Animation animation = AnimationUtils.loadAnimation(this, R.drawable.custom_loading_animation); imageView.startAnimation(animation); ProgressDialog progressDialog = new ProgressDialog(this); progressDialog.setCancelable(false); progressDialog.show(); progressDialog.setContentView(view); ``` 其,使用 LayoutInflater 类将 custom_loading.xml 布局文件实例化为 View 对象,并使用 findViewById() 方法获取 loading_image ImageView 对象。然后,使用 AnimationUtils.loadAnimation() 方法将 custom_loading_animation.xml 动画文件加载到 ImageView ,并调用 startAnimation() 方法开始播放动画。最后,将 View 对象设置为 ProgressDialog 的内容视图,调用 show() 方法显示 ProgressDialog

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值