Android 进度条怎么做?多线程处理(小白速成11)

本文介绍了Android中长条状和圈型进度条的使用,包括如何通过max和progress属性控制进度,以及如何在多线程环境中通过view.post()和Handler删除组件。示例代码展示了如何在点击按钮后延迟五秒删除进度条,强调了主线程与子线程交互的重要性,同时提到了使用Handler的Callback方法以提高代码可读性和可维护性。
摘要由CSDN通过智能技术生成

进度条分为不断滚动的小圆圈,和长条状的进度条

长条状进度条的使用

<ProgressBar
        android:id="@+id/progressBar2"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="448dp"
        android:max="100"
        android:progress="47"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

我们按照上述代码,可以实现一个水平的进度条,我们也可以通过max参数来控制进度条的最大值,通过progress参数控制进度条的初始值。
同时我们也可以通过下面这条java语句控制进度条的增减,在我们获取进度条对象以后。

progressBar.incrementProgressBy(10);

参数为正数时进度条增加,参数为负数时进度条减少。

圈型进度条的小例子

我们实现一下点击按钮之后,过五秒钟进度条消失。
这里涉及到删除组件的情况,我们不能在主线程之外删除组件,但是如果不设置多线程,五秒钟会造成线程阻塞,导致程序意外退出。这里我们采取了view.post()方法来实现。

view.post()来实现组件的删除

   <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="116dp"
        app:layout_constraintBottom_toTopOf="@+id/progressBar2"
        app:layout_constraintEnd_toEndOf="@+id/progressBar2"
        app:layout_constraintStart_toStartOf="@+id/progressBar2"
        app:layout_constraintTop_toTopOf="parent" />


   <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="134dp"
        android:layout_marginRight="134dp"
        android:layout_marginBottom="63dp"
        a
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值