Android实现进度条(ProgressBar)的功能与用法

所属分类: 软件编程 / Android 阅读数: 118
收藏 0 赞 0 分享

进度条(ProgressBar)的功能与用法,供大家参考,具体内容如下

进度条是UI界面中一种实用的UI组件,用于显示一个耗时操作显示出来的百分比,进度条可以动态的显示进度,避免是用户觉得系统长时间未反应,提高用户的体验。
下面程序简单示范了进度条的用法,界面布局文件如下:

在layout下的activity_main中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:orientation="vertical"
  android:layout_height="match_parent"
  tools:context=".Main5Activity">
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"/>
<!--  定义大环形进度条-->
  <ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="@android:style/Widget.ProgressBar.Large"/>
<!--  定义中等环形进度条-->
  <ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
<!--  定义小环形进度条-->
  <ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="@android:style/Widget.ProgressBar.Small"/>
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="任务完成进度条"
    android:textSize="24dp"/>
<!--  定义水平进度条-->
  <ProgressBar
    android:id="@+id/bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="100"
    style="@android:style/Widget.ProgressBar.Horizontal"/>
<!--  定义水平进度条,改变轨道外观-->
  <ProgressBar
    android:id="@+id/bar2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="100"
    android:progressDrawable="@drawable/c4"
    style="@android:style/Widget.ProgressBar.Horizontal"/>
</LinearLayout>

在drawable下的文件下的my_bar中:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <!--    定义轨道的背景-->
  <item android:id="@android:id/background"
    android:drawable="@drawable/c4"/>
<!--  定义已完成部分的样式-->
  <item android:id="@android:id/progress"
    android:drawable="@drawable/c2"/>
</layer-list>

在MainActivity.java中:

package com.example.test03;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;

import java.lang.ref.WeakReference;

public class Main5Activity extends AppCompatActivity {
//  该模拟填充长度为100的数组
  private int[] data=new int[100];
  private int hasdata=0;
//  记录ProgressBar的完成进度
  int status=0;
  private ProgressBar bar;
  private ProgressBar bar2;
  static class MyHandler extends Handler{
    private WeakReference<Main5Activity> activity;
    MyHandler(WeakReference<Main5Activity> activity){
      this.activity=activity;
    }

    @Override
    public void handleMessage(@NonNull Message msg) {
//      表明该消息是该程序发送的
      if (msg.what==0x111){
        activity.get().bar.setProgress(activity.get().status);
        activity.get().bar2.setProgress(activity.get().status);
      }
    }
  }
//  负责更新进度
  MyHandler myHandler=new MyHandler(new WeakReference<>(this));
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main5);
    bar=findViewById(R.id.bar);
    bar2=findViewById(R.id.bar2);
//    启动线程在执行进度
    new Thread(){
      @Override
      public void run() {
        while (status<100){
//          获取耗时操作的完成百分比
          status=doWork();
//          发送消息
          myHandler.sendEmptyMessage(0x111);
        }
      }
    }.start();
  }
//  模拟耗时操作
  public int doWork() {
//    为数组元素赋值
    data[hasdata++] = (int) (Math.random() * 100);
    try {
      Thread.sleep(1000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    return hasdata;
  }
}

**以上就介绍到这里,上面简单实现了一些进度条的方法。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多