进度条ProgressBar及ProgressDialog(实例)

发布时间 - 2026-01-11 02:09:30    点击率:

废话不多说,直接上代码

Main代码
package processdemo.example.administrator.processbardemo;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
  /*ProgressBar
  简介:ProgressBar是进度条组件,通常用于向用户展示某个耗时操作完成的进度,而不让用户感觉是程序失去了响应,从而更好地提升用户界面的友好性
  课程目标:
      1、制定ProgressBar显示风格(系统默认)
      2、ProgressBar的分类
      水平进度条,能精确显示,圆圈进度条,不精确显示
  3、标题上ProgressBar的设置
  4、ProgressBar的关键属性
  5、ProgressBar的关键方法
  6、ProgressDiglog的基础使用
  7、自定义ProgressBar样式*/

  private ProgressBar progressBar3;
  private Button show;
  private Button add;
  private Button res;
  private Button reset;
  private TextView textView;
  private ProgressDialog progressDialog;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
//    启用窗口特征,启用带进度的进度条和不带进度的进度条,
    requestWindowFeature(Window.FEATURE_PROGRESS);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.activity_main);
    setProgressBarVisibility(true);
    setProgressBarIndeterminateVisibility(false);
//    最大值为Max=10000;
    //setProgress(600);
    init();

  }

  private void init() {
    ;
    progressBar3= (ProgressBar) findViewById(R.id.progressBar3);
    show= (Button) findViewById(R.id.show);
    add= (Button) findViewById(R.id.add);
    res= (Button) findViewById(R.id.res);
    reset= (Button) findViewById(R.id.reset);
    textView= (TextView) findViewById(R.id.textView);
    int first=progressBar3.getProgress();/*获取第一进度*/
    int second=progressBar3.getSecondaryProgress();/*获取第二进度*/
    int max=progressBar3.getMax();/*获取最大进度*/
    textView.setText("第一进度条百分比"+(int)((first/(float)max)*100)+"%"+"第二进度条百分比"+(int)(second/(float)max*100)+"%");

    add.setOnClickListener(this);
    res.setOnClickListener(this);
    reset.setOnClickListener(this);
    show.setOnClickListener(this);

  }


  @Override
  public void onClick(View v) {
    switch (v.getId()){
      case R.id.add:
        progressBar3.incrementProgressBy(10);
        progressBar3.incrementSecondaryProgressBy(10);
        break;
      case R.id.res:
        progressBar3.incrementProgressBy(-10);
        progressBar3.incrementSecondaryProgressBy(-10);
        break;
      case R.id.reset:
        progressBar3.setProgress(50);
        progressBar3.setSecondaryProgress(50);
        break;
      case R.id.show:
//        新建ProgressDialog对象
        progressDialog=new ProgressDialog(MainActivity.this);
//        设置显示风格
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//          设置标题
        progressDialog.setTitle("慕课网");
//        设置对话框的内容
        progressDialog.setMessage("欢迎大家支持慕课网");
//       设置图标
        progressDialog.setIcon(R.mipmap.ic_launcher);

       /*设置关于进度条的一些属性*/
//        设置最大进度
        progressDialog.setMax(100);
//        设置初始化已经增长的进度
        progressDialog.incrementProgressBy(50);
//        设置进度条明确显示进度
        progressDialog.setIndeterminate(false);

        /* 设定一个确定按钮*/
        progressDialog.setButton(DialogInterface.BUTTON_POSITIVE,"确定", new Dialog.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(MainActivity.this,"欢迎大家支持慕课网",Toast.LENGTH_SHORT).show();
          }
        });
//        是否可以通过返回按钮来取消对话框
        progressDialog.setCancelable(true);
//        显示ProgressDialog
        progressDialog.show();
    }
    textView.setText("第一进度条百分比"+(int)((progressBar3.getProgress()/(float)progressBar3.getMax())*100)+"%"+"第二进度条百分比"+(int)(progressBar3.getSecondaryProgress()/(float)progressBar3.getMax()*100)+"%");
  }
}

layout中activity_main.xml代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="processdemo.example.administrator.processbardemo.MainActivity">


  <ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/progressBar"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="112dp" />

  <ProgressBar
    style="?android:attr/progressBarStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/progressBar2"
    android:layout_centerVertical="true"
    android:layout_alignParentEnd="true"
    android:layout_marginEnd="256dp" />

  <ProgressBar
    style="@android:style/Widget.ProgressBar.Horizontal"
    android:max="100"
    android:progress="50"
    android:secondaryProgress="80"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/progressBar3"
    android:layout_alignParentBottom="true"
    android:layout_alignStart="@+id/progressBar2"
    android:layout_marginBottom="81dp"
    android:layout_alignTop="@+id/res"
    android:progressDrawable="@layout/progress"/><!--progressDrawable改变样式-->

  <ProgressBar
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/progressBar4"
    android:layout_above="@+id/reset"
    android:layout_centerHorizontal="true" />

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/增加"
    android:id="@+id/add"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true" />

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/减少"
    android:id="@+id/res"
    android:layout_above="@+id/add"
    android:layout_alignParentEnd="true" />

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/重置"
    android:id="@+id/reset"

    android:layout_alignBottom="@+id/progressBar3"
    android:layout_alignParentEnd="true" />

  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="进度"
    android:id="@+id/textView"
    android:layout_below="@+id/progressBar"
    android:layout_alignParentEnd="true" />

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/显示进度条"
    android:id="@+id/show"
    android:layout_below="@+id/progressBar2"
    android:layout_alignParentEnd="true" />
</RelativeLayout>
  <!-- ProgressBar关键属性
  1.android:max -&#45;&#45;最大显示进度
  2.android:progress -&#45;&#45;第一显示进度
  3.android:secondaryProgress-&#45;&#45;第二显示进度
  4.android:isdeterminate -&#45;&#45;设置是否精确显示(false为精确,true为不精确)-->

layout中progress.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <item android:id="@android:id/background">
    <shape>
      <corners android:radius="5dip" />
      <gradient
        android:startColor="#76cf76"
        android:centerColor="#125912"
        android:centerY="0.75"
        android:endColor="#212621"
        android:angle="270"
        />
    </shape>
  </item>

  <item android:id="@android:id/secondaryProgress">
    <clip>
      <shape>
        <corners android:radius="5dip" />
        <gradient
          android:startColor="#80b51638"
          android:centerColor="#80a47d1a"
          android:centerY="0.75"
          android:endColor="#a066c3a7"
          android:angle="270"
          />
      </shape>
    </clip>
  </item>

  <item android:id="@android:id/progress">
    <clip>
      <shape>
        <corners android:radius="5dip" />
        <gradient
          android:startColor="#a38c1c"
          android:centerColor="#55a6b6"
          android:centerY="0.75"
          android:endColor="#b59826"
          android:angle="270"
          />
      </shape>
    </clip>
  </item>

</layer-list>

以上这篇进度条ProgressBar及ProgressDialog(实例)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。


# 进度条ProgressBar及ProgressDialog  # Android ProgressDialog进度条使用详解  # 老生常谈ProgressBar、ProgessDialog的用法  # Android 七种进度条的样式  # Android 中通过实现线程更新Progressdialog (对话进度条)  # 解析android中ProgressBar的用法  # Android ProgressBar进度条使用详解  # 进度条  # 给大家  # 欢迎大家  # 对话框  # 不精确  # 可以通过  # 希望能  # 自定义  # 这篇  # 不带  # 多说  # 小编  # 值为  # 大家多多  # 失去了  # 更好地  # false  # background  # Max  # setProgressBarIndeterminateVisibility 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: Laravel如何生成API文档?(Swagger/OpenAPI教程)  Win11搜索栏无法输入_解决Win11开始菜单搜索没反应问题【技巧】  如何用腾讯建站主机快速创建免费网站?  中山网站制作网页,中山新生登记系统登记流程?  如何获取PHP WAP自助建站系统源码?  微信小程序制作网站有哪些,微信小程序需要做网站吗?  Laravel路由Route怎么设置_Laravel基础路由定义与参数传递规则【详解】  Laravel如何实现事件和监听器?(Event & Listener实战)  Laravel数据库迁移怎么用_Laravel Migration管理数据库结构的正确姿势  如何快速搭建高效可靠的建站解决方案?  网站建设要注意的标准 促进网站用户好感度!  如何快速查询域名建站关键信息?  如何在IIS中新建站点并解决端口绑定冲突?  如何用搬瓦工VPS快速搭建个人网站?  Java解压缩zip - 解压缩多个文件或文件夹实例  网站广告牌制作方法,街上的广告牌,横幅,用PS还是其他软件做的?  在线ppt制作网站有哪些软件,如何把网页的内容做成ppt?  如何快速生成专业多端适配建站电话?  Laravel事件和监听器如何实现_Laravel Events & Listeners解耦应用的实战教程  Linux虚拟化技术教程_KVMQEMU虚拟机安装与调优  如何在浏览器中启用Flash_2025年继续使用Flash Player的方法【过时】  Laravel Debugbar怎么安装_Laravel调试工具栏配置指南  Laravel Docker环境搭建教程_Laravel Sail使用指南  Laravel集合Collection怎么用_Laravel集合常用函数详解  做企业网站制作流程,企业网站制作基本流程有哪些?  如何用JavaScript实现文本编辑器_光标和选区怎么处理  如何在IIS管理器中快速创建并配置网站?  零基础网站服务器架设实战:轻量应用与域名解析配置指南  历史网站制作软件,华为如何找回被删除的网站?  JavaScript如何实现错误处理_try...catch如何捕获异常?  如何利用DOS批处理实现定时关机操作详解  个人网站制作流程图片大全,个人网站如何注销?  Laravel如何使用Blade模板引擎?(完整语法和示例)  手机怎么制作网站教程步骤,手机怎么做自己的网页链接?  Laravel怎么配置S3云存储驱动_Laravel集成阿里云OSS或AWS S3存储桶【教程】  夸克浏览器网页跳转延迟怎么办 夸克浏览器跳转优化  如何为不同团队 ID 动态生成多个“认领值班”按钮  javascript中闭包概念与用法深入理解  Laravel怎么判断请求类型_Laravel Request isMethod用法  如何将凡科建站内容保存为本地文件?  高防服务器:AI智能防御DDoS攻击与数据安全保障  开心动漫网站制作软件下载,十分开心动画为何停播?  Laravel观察者模式如何使用_Laravel Model Observer配置  iOS正则表达式验证手机号、邮箱、身份证号等  Laravel如何理解并使用服务容器(Service Container)_Laravel依赖注入与容器绑定说明  Laravel项目结构怎么组织_大型Laravel应用的最佳目录结构实践  佛山网站制作系统,佛山企业变更地址网上办理步骤?  Laravel怎么做缓存_Laravel Cache系统提升应用速度的策略与技巧  PHP正则匹配日期和时间(时间戳转换)的实例代码  Laravel请求验证怎么写_Laravel Validator自定义表单验证规则教程