Android开发--ProgressBar的应用

1.简介

Progress为加载进度的进度条。

2.构建

构建如图所示的界面,当按+键按钮时进度条增加;当按-键按钮时进度条减少。

      

<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="example.progressbar.Activity1" >

<ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:progress="10"                //设置进度条初始长度为10%
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />

<ProgressBar
        android:id="@+id/progressBar2"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/progressBar1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="47dp" />

<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/progressBar2"
        android:layout_marginTop="72dp"
        android:layout_toStartOf="@+id/progressBar2"
        android:text="@string/bt1" />

<Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button1"
        android:layout_toEndOf="@+id/progressBar2"
        android:text="@string/bt2" />
</RelativeLayout>

3.代码

public class Activity1 extends Activity implements OnClickListener{
    Button b1,b2;
    ProgressBar pb;  
    @Override
    public void onClick(View v) {
              //给按钮做监听,通过ID进行匹配
        if(v.getId()==R.id.button1){
             // getPorgress()获取初始进度条的长度,乘以2则每次增加一倍,再通过setProgress()设置进度条        
            pb.setProgress((int) (pb.getProgress()*2));
        }
        else{
            pb.setProgress((int) (pb.getProgress()*0.6));
        }       
    }

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act1);
        b1=(Button) findViewById(R.id.button1);
        b2=(Button) findViewById(R.id.button2);
        pb=(ProgressBar) findViewById(R.id.progressBar1);
        //设置监听器
        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
    }

4.效果

时间: 2024-12-29 12:22:11

Android开发--ProgressBar的应用的相关文章

Android开发之打开闪光灯录制视频

Android的SDK在线API上对录制视频的方法.步骤都写得非常清楚,但是如果没有一点思路,写起来也比较式费事.录制视频的全过程要打开闪光灯(可能是因为项目需要,或者特殊原因),则必须按照一定的顺序进行开关,毕竟容易出错.要实现录制的同时开启闪光灯也不难,官方API给出了一个大体的步骤.因为要采集点视频数据,临时写了个简单的Demo学习下,必要时再深度开发. 首先在工程中的AndroidManifest.xml中添加权限声明,因为要使用到摄像头,故需要添加Camera的相关权限,另外还需要写S

android开发中遇到的问题汇总【九】

244.http请求的url含有中字符时.须要Uri编码.Uri.encoder() 245.使用androidstudio时,不知道什么原因svn不见了 Android Studio missing Subversion plugin Please make sure that the "SubversionIntegration" plugin is enabled in Preferences > Plugins 246.Error:Execution failed for

【Android学习】《Android开发视频教程》第一季笔记

视频地址: http://study.163.com/course/courseMain.htm?courseId=207001 课时5    Activity基础概念 1.Android开发技术结构图 2.Android四大组件 Activity(页面).Service(后台运行的服务).Content Provider(数据提供者,向外暴露数据的方法,数据共享).Broadcast Receiver(监听手机发生的一切事情) 课时6    文本框与按钮的使用方法(Activity初步一) 1

Android开发笔记(一百一十六)网络学习资源

知名网站 本系列的开发笔记,对Android开发来说只是沧海一瓢,还有更多的技术等待我们去汲取.下面列出几个常用的开发网站,供初学者上路: 首先当然是国内首屈一指的技术网站csdn啦,csdn提供了众多频道,包括博客.论坛.下载.问答等等,其中博客专栏提供了最新的技术文章,值得推荐.csdn博客专栏的地址是 http://blog.csdn.net/column.html 下面是csdn博客专栏的网页截图: 其次是国外有名的开源网站GitHub,这里有众多的开源项目源码,是开发者分享代码的乐园.

android开发开源宝贝——持续更新。。。

2016年11月11日更新 http://www.apkbus.com/forum-417-1.html http://p.codekk.com/detail/Android/hejunlin2013/LivePlayback www.codekk.com https://github.com/Trinea/android-open-project Android 开源项目分类汇总 我们的微信公众号:codekk.二维码如下: 专注于 Android 开源分享.源码解析.框架设计.Android

android开发零基础入门教程

最实用.最容易上手的android课程 很多java学习者都想尽快掌握android开发的流程!为此,我们推出了12小时系列课程,学习者只需要两天时间即可掌握android开发的基本知识,跨入android开发的大门,如想深入学习,请关注课程更新:授课时,我们也注重展示android开发中的实际技巧,课上代码都由老师一行一行实际敲出,真实展现了android开发中的细节. 在学习本课程时,你应该掌握基本java开发知识.数据库知识.XML技术等.这些技术我们再12小时学android课程中不再赘

★android开发--ListView+Json+异步网络图片加载+滚动翻页的例子(图片能缓存,图片不错乱)

例子中用于解析Json的Gson请自己Google下载 主Activity: package COM.Example.Main; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; import COM.Example.Main.R; import COM.Example.Main.stringG

Android开发之使用HTTP访问网络资源

使用HTTP访问网络资源 前面介绍了 URLConnection己经可以非常方便地与指定站点交换信息,URLConnection还有一个子类:HttpURLConnection,HttpURLConnection 在 LIRLConnection的基础上做了进一步改进,增加了一些用于操作http资源的便捷方法. 1.使用HttpURLConnection HttpURLConnection继承了URLConnection,因此也可用于向指定网站发送GET请求 POST请求.它在URLConnec

Android开发之基本控件和详解四种布局方式

Android中的控件的使用方式和iOS中控件的使用方式基本相同,都是事件驱动.给控件添加事件也有接口回调和委托代理的方式.今天这篇博客就总结一下Android中常用的基本控件以及布局方式.说到布局方式Android和iOS还是区别挺大的,在iOS中有Frame绝对布局和AutoLayout相对布局.而在Android中的布局方式就比较丰富了,今天博客中会介绍四种常用的布局方式.先总结一下控件,然后再搞一搞基本方式,开发环境还是用的Mac下的Android Studio.开始今天的正题, 虽然A