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

2020-10-09 0 685

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

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

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

在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 Android实现进度条(ProgressBar)的功能与用法 http://www.ox520.com/23729.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务