一、基础知识:

1.ProgressBar在界面文件XML中的布局:

[html]
<progressBar android:id="@+id/progressbar_updown" 
        android:layout_width="200dp"  
        android:layout_height="wrap_content" 
        style="?android:attr/progressBarStyleHorizontal" 
        android:layout_gravity="center_vertical"  
        android:max="100" 
        android:progress="50" 
        android:secondaryProgress="70"    >

<progressBar android:id="@+id/progressbar_updown"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_gravity="center_vertical"
        android:max="100"
        android:progress="50"
        android:secondaryProgress="70"    >

[plain]
style="?android:attr/progressBarStyleHorizontal"    设置风格为长形  
android:max="100"    最大进度值为100  
android:progress="50"   初始化的进度值  
android:secondaryProgress="70" 初始化的底层第二个进度值  
android:layout_gravity="center_vertical"    垂直居中

style="?android:attr/progressBarStyleHorizontal"    设置风格为长形
android:max="100"    最大进度值为100
android:progress="50"   初始化的进度值
android:secondaryProgress="70" 初始化的底层第二个进度值
android:layout_gravity="center_vertical"    垂直居中

2.ProgressBar在代码文件(.java)中的控制使用:

[java]
private ProgressBar myProgressBar; 
//定义ProgressBar  
 
myProgressBar = (ProgressBar) findViewById(R.id.progressbar_updown); 
//ProgressBar通过ID来从XML中获取  
 
myProgressBar.incrementProgressBy(5); 
//ProgressBar进度值增加5  
 
myProgressBar.incrementProgressBy(-5); 
//ProgressBar进度值减少5  
 
myProgressBar.incrementSecondaryProgressBy(5); 
//ProgressBar背后的第二个进度条 进度值增加5  
 
myProgressBar.incrementSecondaryProgressBy(-5); 
//ProgressBar背后的第二个进度条 进度值减少5

private ProgressBar myProgressBar;
//定义ProgressBar

myProgressBar = (ProgressBar) findViewById(R.id.progressbar_updown);
//ProgressBar通过ID来从XML中获取

myProgressBar.incrementProgressBy(5);
//ProgressBar进度值增加5

myProgressBar.incrementProgressBy(-5);
//ProgressBar进度值减少5

myProgressBar.incrementSecondaryProgressBy(5);
//ProgressBar背后的第二个进度条 进度值增加5

myProgressBar.incrementSecondaryProgressBy(-5);
//ProgressBar背后的第二个进度条 进度值减少5

3.XML重要属性

android:progressBarStyle:默认进度条样式

android:progressBarStyleHorizontal:水平样式

4.重要方法

[plain]
getMax():返回这个进度条的范围的上限 
 
getProgress():返回进度 
 
getSecondaryProgress():返回次要进度 
 
incrementProgressBy(int diff):指定增加的进度 
 
isIndeterminate():指示进度条是否在不确定模式下 
 
setIndeterminate(boolean indeterminate):设置不确定模式下 
 
setVisibility(int v):设置该进度条是否可视

getMax():返回这个进度条的范围的上限

getProgress():返回进度

getSecondaryProgress():返回次要进度

incrementProgressBy(int diff):指定增加的进度

isIndeterminate():指示进度条是否在不确定模式下

setIndeterminate(boolean indeterminate):设置不确定模式下

setVisibility(int v):设置该进度条是否可视

二、代码展示:

1."Activity_09\src\yan\activity_09\MainActivity.java"

[java]
package yan.activity_09; 
 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.ProgressBar; 
import android.app.Activity; 
 
public class MainActivity extends Activity { 
    // 声明变量  
    private ProgressBar firstBar = null; 
    private ProgressBar secondBar = null; 
    private Button myButton = null; 
    private int progress_vol = 0; 
     
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
         
        //映射控件ID到变量  
        firstBar = (ProgressBar)findViewById(R.id.firstBar); 
        secondBar = (ProgressBar)findViewById(R.id.secondBar); 
        myButton = (Button)findViewById(R.id.myButton); 
         
         
        myButton.setOnClickListener(new ButtonListenr()); 
    } 
     
    class ButtonListenr implements OnClickListener{ 
 
        @Override 
        public void onClick(View v) { 
            // TODO Auto-generated method stub  
            if(0 == progress_vol) 
            { 
                // 设置进度条的最大值  
                firstBar.setMax(200); 
                // 设置进度条为可见的状态  
                firstBar.setVisibility(View.VISIBLE); 
                secondBar.setVisibility(View.VISIBLE); 
            }else if(progress_vol < firstBar.getMax()){ 
                // 设置主进度条的当前值  
                firstBar.setProgress(progress_vol); 
                // 设置第二进度条的当前值  
                firstBar.setSecondaryProgress(progress_vol+10); 
                // 默认的进度条是无法显示进行的状态的  
                //secondBar.setProgress(progress_vol);  
            }else{ 
                // 设置进度条为不可见的状态  
                firstBar.setVisibility(View.GONE); 
                secondBar.setVisibility(View.GONE); 
            } 
            progress_vol +=10; 
        } 
         
    } 
}

package yan.activity_09;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.app.Activity;

public class MainActivity extends Activity {
 // 声明变量
 private ProgressBar firstBar = null;
 private ProgressBar secondBar = null;
 private Button myButton = null;
 private int progress_vol = 0;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  
  //映射控件ID到变量
  firstBar = (ProgressBar)findViewById(R.id.firstBar);
  secondBar = (ProgressBar)findViewById(R.id.secondBar);
  myButton = (Button)findViewById(R.id.myButton);
  
  
  myButton.setOnClickListener(new ButtonListenr());
 }
 
 class ButtonListenr implements OnClickListener{

@Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   if(0 == progress_vol)
   {
    // 设置进度条的最大值
    firstBar.setMax(200);
    // 设置进度条为可见的状态
    firstBar.setVisibility(View.VISIBLE);
    secondBar.setVisibility(View.VISIBLE);
   }else if(progress_vol < firstBar.getMax()){
    // 设置主进度条的当前值
    firstBar.setProgress(progress_vol);
    // 设置第二进度条的当前值
    firstBar.setSecondaryProgress(progress_vol+10);
    // 默认的进度条是无法显示进行的状态的
    //secondBar.setProgress(progress_vol);
   }else{
    // 设置进度条为不可见的状态
    firstBar.setVisibility(View.GONE);
    secondBar.setVisibility(View.GONE);
   }
   progress_vol +=10;
  }
  
 }
}

2."Activity_09\res\layout\main.xml"

[html]
<?xml version="1.0" encoding="utf-8"?>   
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
    android:orientation="vertical"   
    android:layout_width="fill_parent"   
    android:layout_height="fill_parent"  
    android:background="#00aaaa"   
    >   
   <TextView 
        android:id="@+id/firstText"   
        android:text="@string/hello_world"   
        android:gravity="center_vertical"   
        android:textSize="15pt"   
        android:background="#aa0000"   
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content"   
        android:singleLine="true"/>   
<ProgressBar 
    android:id="@+id/firstBar" 
    style="?android:attr/progressBarStyleHorizontal" 
    android:layout_width="200dp" 
    android:layout_height="wrap_content" 
    android:visibility="gone" 
    /> 
<ProgressBar 
    android:id="@+id/secondBar" 
    style="?android:attr/progressBarStyle" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:visibility="gone" 
    /> 
<Button 
    android:id="@+id/myButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="begin" 
    /> 
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
 android:background="#00aaaa" 
    > 
   <TextView
  android:id="@+id/firstText" 
  android:text="@string/hello_world" 
  android:gravity="center_vertical" 
  android:textSize="15pt" 
  android:background="#aa0000" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:singleLine="true"/> 
<ProgressBar
    android:id="@+id/firstBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:visibility="gone"
    />
<ProgressBar
    android:id="@+id/secondBar"
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone"
    />
<Button
    android:id="@+id/myButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="begin"
    />
</LinearLayout> 
3."Activity_09\res\values\strings.xml"

[html]
<?xml version="1.0" encoding="utf-8"?> 
<resources> 
 
    <string name="app_name">Activity_09</string> 
    <string name="hello_world">Hello world!</string> 
    <string name="menu_settings">Settings</string> 
 
</resources>

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">Activity_09</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>

</resources>

怎样控制界面控件之进度条(ProgressBar)功能的更多相关文章

  1. MFC控件编程进度条编写

    MFC控件编程进度条编写 一丶进度条编程需要用到的方法 进度条MFC已经帮我们封装好类了. 叫做 CProgressCtrl  进度条编程也很简单. 封装的方法也就那个那几个. GetPos()  获 ...

  2. C# 根据BackgroundWoker异步模型和ProgressBar控件,自定义进度条控件

    前言 程序开发过程中,难免会有的业务逻辑,或者算法之类产生让人能够感知的耗时操作,例如循环中对复杂逻辑处理;获取数据库百万乃至千万级数据;http请求的时候等...... 用户在使用UI操作并不知道程 ...

  3. Android自己定义控件:进度条的四种实现方式

    前三种实现方式代码出自: http://stormzhang.com/openandroid/2013/11/15/android-custom-loading/ (源代码下载)http://down ...

  4. 2015.3.11 VS异步控件及进度条结合应用

    1.在Form中添加 指针控件:BackgroundWorker-bgwork:进度条控件progressBar1 以及开始.取消按钮 2.开始按钮启动异步线程 private void button ...

  5. Winform之跨线程访问控件(在进度条上显示字体)

    此文章对于遇到必须使用线程但是没有办法在线程内操作控件的问题的处理  有很好的解决方案(个人认为的.有更好的方案欢迎交流.) 在做跨线程访问之前我们先了解下我们所做的需要达到的效果: 这个是批量的将x ...

  6. 【转】VC 多线程中控制界面控件的几种方法

    原文网址:https://software.intel.com/zh-cn/blogs/2010/11/30/vc-3 为了保证界面的用户体验经常要把数据处理等放到子线程中进行,然后把结果更新到主界面 ...

  7. Android中的常用控件之进度条(ProgressBar)

    ProgressBar的常用属性:style,进度条的样式,默认为圆形,用style="?android:attr/progressBarStyleHorizontal"可以将进度 ...

  8. 【Qt开发】Qt控件之进度条

    QT 进度条操作实例是本文要介绍的内容,在QT中可以用QProgressBar或着QProgressDialog来实现进度条. QProgressBar的使用   首先在designer中拖一个按钮和 ...

  9. Android自己定义控件--圆形进度条(中间有图diao)

    智能家居越来越流行,在智能家居中我们常要表现一些数据的百分比 圆形度条中间加个图是一种很流行的自己定义View 1.第一步 你首先须要对类进行继承View public class CirclePro ...

随机推荐

  1. PHP - 数学运算

    第4章 数学运算 学习要点: 1.数值数据类型 2.随机数 3.格式化数据 4.数学函数 在大多数程序设计语言中,数值运算都是最基本的元素之一.数值运算允许程序员完成加法到高级计算等各种操作.尽管PH ...

  2. TImage也有OnClick事件,可以当按钮使用,配上合适的图片(背景透明,效果前凸)更是几乎以假乱真

    本质上TImage与TSpeedButton没有什么区别,都是没有句柄的,但都可以执行OnClick事件.有空分析一下.

  3. WCF技术剖析之八:ClientBase<T>中对ChannelFactory<T>的缓存机制

    原文:WCF技术剖析之八:ClientBase<T>中对ChannelFactory<T>的缓存机制 和传统的分布式远程调用一样,WCF的服务调用借助于服务代理(Service ...

  4. java.lang.ClassNotFoundException与java.lang.NoClassDefFoundError的区别(转)

    ClassNotFoundException ClassNotFoundException这个错误,比较常见也好理解. 原因:就是找不到指定的class. 常见的场景就是: 1 调用class的for ...

  5. Hadoop: the definitive guide 第三版 拾遗 第十三章 之HBase起步

    指南上这一章的开篇即提出:HBase是一个分布式的.面向列的开源数据库.如果需要实时的随机读/写超大规模数据集,HBase无疑是一个好的选择. 简介 HBase 是一个高可靠性.高性能.面向列.可伸缩 ...

  6. android第一天-------环境搭建

    今天正式第一天学习android的. 1.昨晚下班后回家跟同事刘江龙打了四把dota.还好,都赢了把对面虐成狗了.大多都是1300到1450的局,玩的很爽. 2.dota打完后给在湖南常德的女朋友打了 ...

  7. perl uri_escape(urlencode ) 转换

    [root@wx03 lib]# cat a1.pl #引入模块 use URI::Escape; #urlencode $encoded = uri_escape("[中均]") ...

  8. Visual Studio 必备神器---转

    会使用工具是人类文明的一大进步,今天敏捷大行其道,好的工具可以大大的提高生产力,这里说的工具都是VS平台上的扩展工具,一些机械的部分可以交给工具去处理,自己多关注其他部分.下面分享下我觉得不错的工具, ...

  9. 关于在打包Jar文件时遇到的资源路径问题(一)

    当我们将程序写好,并进行打包成Jar文件时,通常都带有各种资源,这些资源可以是图像或者声音文件,也可以是别的如文本文件或二进制文件等,这些资源都和代码密切相关.例如在一个JPanel类上显示一些可能变 ...

  10. Beautiful Soup 中文文档

    Beautiful Soup 3.0 中文文档说明: http://www.crummy.com/software/BeautifulSoup/bs3/documentation.zh.html Be ...