android:progressbar实现进度条

2024-02-09 16:32

本文主要是介绍android:progressbar实现进度条,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在听网络音乐或者下载软件的时候总能看到进度条的身影 它是怎么实现的呢

应该是已下载的大小/总的应用的大小  哈哈 

progress小例子

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><Buttonandroid:id="@+id/add"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="增加" /><Buttonandroid:id="@+id/sub"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="减少" /><Buttonandroid:id="@+id/reset"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="重置" /><Buttonandroid:id="@+id/show"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="显示进度条对话框" /><ProgressBarandroid:id="@+id/progressBar"style="?android:attr/progressBarStyleHorizontal"android:layout_width="fill_parent"android:layout_height="wrap_content"android:progress="20"android:secondaryProgress="30" /><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TextView" /></LinearLayout>


代码很简单

就不说了  主要的都有注释

package com.example.progressbar;import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener {private Button button1;private Button button2;private Button button3;private Button show;private TextView textView;private ProgressBar progress, progress1;private ProgressDialog progressDialog;private int max;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);/** 启用窗口特征,启用两种形式进度的进度条*/requestWindowFeature(Window.FEATURE_PROGRESS);requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);setContentView(R.layout.activity_main);/** 显示两种进度*///进度可见setProgressBarVisibility(true);//明确显示进度的setProgressBarIndeterminate(false);//最大进度为10000setProgress(3000);//进度可见setProgressBarIndeterminateVisibility(true);button1 = (Button) findViewById(R.id.add);button2 = (Button) findViewById(R.id.sub);button3 = (Button) findViewById(R.id.reset);show = (Button) findViewById(R.id.show);textView = (TextView) findViewById(R.id.textView1);progress = (ProgressBar) findViewById(R.id.progressBar);init();button1.setOnClickListener(this);button2.setOnClickListener(this);button3.setOnClickListener(this);show.setOnClickListener(this);}//初始化private void init() {// TODO Auto-generated method stubmax = progress.getMax();progress.setProgress(50);progress.setSecondaryProgress(60);textView.setText("第一进度的百分比为" + (int)((float) progress.getProgress()/ (float) max *100)+ "%,第二进度条的百分比为"+ (int)((float) progress.getSecondaryProgress() / (float) max *100)+ "%");}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (v.getId()) {//点击增加按钮  通过incrementProgressBy方法 增加进度case R.id.add:progress.incrementProgressBy(10);progress.incrementSecondaryProgressBy(10);break;//点击减少按钮  通过incrementProgressBy方法 减少进度case R.id.sub:progress.incrementProgressBy(-10);progress.incrementSecondaryProgressBy(-10);break;case R.id.reset:init();break;case R.id.show:progressDialog=new ProgressDialog(MainActivity.this);//设置对话框进度条的图标progressDialog.setIcon(R.drawable.ic_launcher);//设置对话框进度条的标题progressDialog.setTitle("苏苏爱自由");//设置对话框进度条的内容progressDialog.setMessage("欢迎进入苏苏的博客");//设置对话框进度条的显示风格progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//不是明确显示进度的progressDialog.setIndeterminate(true);//添加一个确定按钮 并为其添加事件progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubToast.makeText(MainActivity.this, "谢谢大家的支持", Toast.LENGTH_SHORT).show();}});//显示对话框进度条progressDialog.show();break;}textView.setText("第一进度的百分比为" + (int)((float) progress.getProgress()/ (float) max *100)+ "%,第二进度条的百分比为"+ (int)((float) progress.getSecondaryProgress() / (float) max *100)+ "%");}}




这篇关于android:progressbar实现进度条的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/694741

相关文章

使用animation.css库快速实现CSS3旋转动画效果

《使用animation.css库快速实现CSS3旋转动画效果》随着Web技术的不断发展,动画效果已经成为了网页设计中不可或缺的一部分,本文将深入探讨animation.css的工作原理,如何使用以及... 目录1. css3动画技术简介2. animation.css库介绍2.1 animation.cs

Java进行日期解析与格式化的实现代码

《Java进行日期解析与格式化的实现代码》使用Java搭配ApacheCommonsLang3和Natty库,可以实现灵活高效的日期解析与格式化,本文将通过相关示例为大家讲讲具体的实践操作,需要的可以... 目录一、背景二、依赖介绍1. Apache Commons Lang32. Natty三、核心实现代

SpringBoot实现接口数据加解密的三种实战方案

《SpringBoot实现接口数据加解密的三种实战方案》在金融支付、用户隐私信息传输等场景中,接口数据若以明文传输,极易被中间人攻击窃取,SpringBoot提供了多种优雅的加解密实现方案,本文将从原... 目录一、为什么需要接口数据加解密?二、核心加解密算法选择1. 对称加密(AES)2. 非对称加密(R

基于Go语言实现Base62编码的三种方式以及对比分析

《基于Go语言实现Base62编码的三种方式以及对比分析》Base62编码是一种在字符编码中使用62个字符的编码方式,在计算机科学中,,Go语言是一种静态类型、编译型语言,它由Google开发并开源,... 目录一、标准库现状与解决方案1. 标准库对比表2. 解决方案完整实现代码(含边界处理)二、关键实现细

python通过curl实现访问deepseek的API

《python通过curl实现访问deepseek的API》这篇文章主要为大家详细介绍了python如何通过curl实现访问deepseek的API,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编... API申请和充值下面是deepeek的API网站https://platform.deepsee

SpringBoot实现二维码生成的详细步骤与完整代码

《SpringBoot实现二维码生成的详细步骤与完整代码》如今,二维码的应用场景非常广泛,从支付到信息分享,二维码都扮演着重要角色,SpringBoot是一个非常流行的Java基于Spring框架的微... 目录一、环境搭建二、创建 Spring Boot 项目三、引入二维码生成依赖四、编写二维码生成代码五

MyBatisX逆向工程的实现示例

《MyBatisX逆向工程的实现示例》本文主要介绍了MyBatisX逆向工程的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录逆向工程准备好数据库、表安装MyBATisX插件项目连接数据库引入依赖pom.XML生成实体类、

C#实现查找并删除PDF中的空白页面

《C#实现查找并删除PDF中的空白页面》PDF文件中的空白页并不少见,因为它们有可能是作者有意留下的,也有可能是在处理文档时不小心添加的,下面我们来看看如何使用Spire.PDFfor.NET通过C#... 目录安装 Spire.PDF for .NETC# 查找并删除 PDF 文档中的空白页C# 添加与删

Java实现MinIO文件上传的加解密操作

《Java实现MinIO文件上传的加解密操作》在云存储场景中,数据安全是核心需求之一,MinIO作为高性能对象存储服务,支持通过客户端加密(CSE)在数据上传前完成加密,下面我们来看看如何通过Java... 目录一、背景与需求二、技术选型与原理1. 加密方案对比2. 核心算法选择三、完整代码实现1. 加密上

Java使用WebView实现桌面程序的技术指南

《Java使用WebView实现桌面程序的技术指南》在现代软件开发中,许多应用需要在桌面程序中嵌入Web页面,例如,你可能需要在Java桌面应用中嵌入一部分Web前端,或者加载一个HTML5界面以增强... 目录1、简述2、WebView 特点3、搭建 WebView 示例3.1 添加 JavaFX 依赖3