android 进度条组件ProgressBar
作者:再见,少年 发布时间:2023-10-20 15:05:33
标签:android,进度条
首先是main.xml文件
代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ProgressBar
android:id="@+id/myprobarA"
style="?android:attr/progressBarStyle"
android:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ProgressBar
android:id="@+id/myprobarB"
style="?android:attr/progressBarStyleHorizontal"
android:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ProgressBar
android:id="@+id/myprobarC"
style="?android:attr/progressBarStyleHorizontal"
android:visibility="gone"
android:max="120"
android:progress="0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ProgressBar
android:id="@+id/myprobarD"
android:visibility="gone"
android:max="120"
android:progress="50"
android:secondaryProgress="70"
style="?android:attr/progressBarStyleLarge"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ProgressBar
android:id="@+id/myprobarE"
android:visibility="gone"
android:max="120"
android:progress="50"
android:secondaryProgress="70"
style="?android:attr/progressBarStyleSmall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/mybut"
android:text="显示进度条"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
然后编写Activity.java类
代码如下:
package com.example.myfirstproject;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.*;
public class MainActivity extends Activity {
private ProgressBar myprobarA,myprobarB,myprobarC,myprobarD,myprobarE;
private Button mybut;
protected static final int STOP = 1;
protected static final int CONTINUE = 2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.myprobarA = (ProgressBar)this.findViewById(R.id.myprobarA);
this.myprobarB = (ProgressBar)this.findViewById(R.id.myprobarB);
this.myprobarC = (ProgressBar)this.findViewById(R.id.myprobarC);
this.myprobarD = (ProgressBar)this.findViewById(R.id.myprobarD);
this.myprobarE = (ProgressBar)this.findViewById(R.id.myprobarE);
this.mybut = (Button)this.findViewById(R.id.mybut);
this.myprobarA.setIndeterminate(false);
this.myprobarB.setIndeterminate(false);
this.myprobarC.setIndeterminate(true);
this.myprobarD.setIndeterminate(false);
this.myprobarE.setIndeterminate(false);
this.mybut.setOnClickListener(new OnClickListenerlmpl());
}
private class OnClickListenerlmpl implements OnClickListener{
public void onClick(View view){
MainActivity.this.myprobarB.setSecondaryProgress(0);
MainActivity.this.myprobarA.setVisibility(View.VISIBLE);
MainActivity.this.myprobarB.setVisibility(View.VISIBLE);
MainActivity.this.myprobarC.setVisibility(View.VISIBLE);
MainActivity.this.myprobarD.setVisibility(View.VISIBLE);
MainActivity.this.myprobarE.setVisibility(View.VISIBLE);
MainActivity.this.myprobarA.setMax(120);
MainActivity.this.myprobarB.setMax(120);
MainActivity.this.myprobarA.setProgress(0);
MainActivity.this.myprobarB.setProgress(0);
new Thread(new Runnable(){
public void run(){
int count = 0;
for(int i = 0;i < 10;i++){
try{
count = (i+1)*20;
Thread.sleep(500);
if(i==6){
Message m = new Message();
m.what = MainActivity.STOP;
MainActivity.this.myMessageHandler.sendMessage(m);
break;
}else{
Message m = new Message();
m.arg1 = count;
m.what = MainActivity.CONTINUE;
MainActivity.this.myMessageHandler.sendMessage(m);
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}
}).start();
}
}
private Handler myMessageHandler = new Handler(){
public void handleMessage(Message msg){
switch(msg.what){
case MainActivity.STOP:
myprobarA.setVisibility(View.GONE);
myprobarB.setVisibility(View.GONE);
myprobarC.setVisibility(View.GONE);
myprobarD.setVisibility(View.GONE);
myprobarE.setVisibility(View.GONE);
Thread.currentThread().interrupt();
break;
case MainActivity.CONTINUE:
if(!Thread.currentThread().isInterrupted()){
myprobarA.setProgress(msg.arg1);
myprobarB.setProgress(msg.arg1);
myprobarC.setProgress(msg.arg1);
myprobarD.setProgress(msg.arg1);
myprobarE.setProgress(msg.arg1);
}
break;
}
}
};
}
运行效果:


猜你喜欢
- Volley简介我们平时在开发Android应用的时候不可避免地都需要用到网络技术,而多数情况下应用程序都会使用HTTP协议来发
- 完整代码:https://github.com/iyuanyb/Downloader多线程下载及断点续传的实现是使用 HTTP/1.1 引入
- 从配置获取的配置默认是明文的,有些像数据源这样的配置需要加密的话,需要对配置中心进行加密处理。下面使用对称性加密来加密配置,需要配置一个密钥
- try catch介绍我们编译运行程序出错的时候,编译器就会抛出异常。抛出异常要比终止程序灵活许多,这是因为Java提供了一个“捕获”异常的
- 一、封装的查询方法/*** solr查询方法* @param client solr客户端* @param query solr查询对象*
- 1、static关键字1.1 使用static关键字定义属性在讲解static定义属性操作之前,首先编写如下一道程序。现在定义一个表示中国人
- 图片准备hole.png和slider.png为png是因为图片带有透明度。合成目标最终为前端生成两张图片:out_slider.png高度
- 1.企业实际项目中Git的使用在实际的企业项目开发中,我们一般Java的项目在公司都有自己的局域网代码仓库,仓库上存放着很多的项目。以我工作
- Android自定义view是什么在我们的日常开发中,很多时候系统提供的view是无法满足我们的需求的,例如,我们想给一个edittext加
- 一、前言高效、合理的使用hibernate-validator校验框架可以提高程序的可读性,以及减少不必要的代码逻辑。接下来会介绍一下常用一
- 本文介绍了android视频截屏&手机录屏实现代码,分享给大家,希望对大家有帮助问题在android中有时候我们需要对屏幕进行截屏操
- 基本布局演示1. 定义包含GridView 的 main.xmk<?xml version="1.0" encod
- 前言本文介绍的是Android如何实现数字跳动效果的TextView,主要运用了DancingNumberView,DancingNumbe
- spring的一大功能是依赖注入 通过把javabean放入spring的ioc容器中进行统一管理过程如图所示最常见的例子是使用xml配置b
- 1. 简介很早就听说了Google的Lifecycle组件,因为项目没有使用过,所以并没有过多的接触。不过最近看到了一篇文章,其中的一条评论
- refresh()该方法是 Spring Bean 加载的核心,它是 ClassPathXmlApplicationContext 的父类
- 前言:List 去重指的是将 List 中的重复元素删除掉的过程。此题目考察的是对 List 迭代器、Set 集合和 JDK 8 中新特性的
- 前言Elasticsearch是一个非常流行的搜索引擎,已经成为了许多企业的首选解决方案。然而,我们要想成为一个优秀的程序员,就必须掌握各种
- 本文实例讲述了C#集合遍历时删除和增加元素的方法。分享给大家供大家参考,具体如下:大多数时候,遍历集合元素的时候并不需要对元素进行增加或者删
- Message的创建消息Message一般不支持大家直接通过new的方式进行创建的,因为Message作为Android系统中使用频率非常高