怎样控制界面控件之进度条(ProgressBar)功能
一、基础知识:
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)功能的更多相关文章
- MFC控件编程进度条编写
MFC控件编程进度条编写 一丶进度条编程需要用到的方法 进度条MFC已经帮我们封装好类了. 叫做 CProgressCtrl 进度条编程也很简单. 封装的方法也就那个那几个. GetPos() 获 ...
- C# 根据BackgroundWoker异步模型和ProgressBar控件,自定义进度条控件
前言 程序开发过程中,难免会有的业务逻辑,或者算法之类产生让人能够感知的耗时操作,例如循环中对复杂逻辑处理;获取数据库百万乃至千万级数据;http请求的时候等...... 用户在使用UI操作并不知道程 ...
- Android自己定义控件:进度条的四种实现方式
前三种实现方式代码出自: http://stormzhang.com/openandroid/2013/11/15/android-custom-loading/ (源代码下载)http://down ...
- 2015.3.11 VS异步控件及进度条结合应用
1.在Form中添加 指针控件:BackgroundWorker-bgwork:进度条控件progressBar1 以及开始.取消按钮 2.开始按钮启动异步线程 private void button ...
- Winform之跨线程访问控件(在进度条上显示字体)
此文章对于遇到必须使用线程但是没有办法在线程内操作控件的问题的处理 有很好的解决方案(个人认为的.有更好的方案欢迎交流.) 在做跨线程访问之前我们先了解下我们所做的需要达到的效果: 这个是批量的将x ...
- 【转】VC 多线程中控制界面控件的几种方法
原文网址:https://software.intel.com/zh-cn/blogs/2010/11/30/vc-3 为了保证界面的用户体验经常要把数据处理等放到子线程中进行,然后把结果更新到主界面 ...
- Android中的常用控件之进度条(ProgressBar)
ProgressBar的常用属性:style,进度条的样式,默认为圆形,用style="?android:attr/progressBarStyleHorizontal"可以将进度 ...
- 【Qt开发】Qt控件之进度条
QT 进度条操作实例是本文要介绍的内容,在QT中可以用QProgressBar或着QProgressDialog来实现进度条. QProgressBar的使用 首先在designer中拖一个按钮和 ...
- Android自己定义控件--圆形进度条(中间有图diao)
智能家居越来越流行,在智能家居中我们常要表现一些数据的百分比 圆形度条中间加个图是一种很流行的自己定义View 1.第一步 你首先须要对类进行继承View public class CirclePro ...
随机推荐
- js轮盘抽奖
js轮盘抽奖 需求:实现中奖是否可控 思路:通过旋转角度来实现轮盘转动,根据角度来确定是否中奖 window.onload = function(){ var oTurn = document.get ...
- WCF技术剖析之十一:异步操作在WCF中的应用(下篇)
原文:WCF技术剖析之十一:异步操作在WCF中的应用(下篇) 说完了客户端的异步服务调用(参阅WCF技术剖析之十一:异步操作在WCF中的应用(上篇)),我们在来谈谈服务端如何通过异步的方式为服务提供实 ...
- TCP协议中的计时器
说明: 本文仅供学习交流.转载请标明出处,欢迎转载! 本文是下面文献相关内容的总结 [1] <TCP/IP具体解释 卷1:协议> [2] <TCP/IP协议族 第4版> [3 ...
- Single Image Haze Removal Using Dark Channel Prior翻译
这段时间在回顾以前做的去雾,顺便就把这篇文章翻译了一下,看看有没有同行交流.由于是用LaTex排版的,所以只能转为图片传上来了.另外,英语水平有限,大部分都是靠词典,所以只能勉强着看了.
- Qt 打包发布 不能动态打开图片显示问题
刚写完一个图片标注工具, 发现在我电脑可以实时打开照片显示出来,在他人的电脑上就不可以. 原来Qt默认只识别png 具体解决方案: 原地址:http://blog.csdn.net/goodlixue ...
- Fedora 17 下安装codeblocks
Fedora 17 下安装codeblocks: 1.直接从yum源安装: sudo yum install codeblocks 2.源码安装 ...
- css3 动画运动路径
1.cubic-bezier贝塞尔曲线CSS3动画工具 http://www.xuanfengge.com/cubic-bezier-bezier-css3-animation-tools.html ...
- Object-c @property的用法
property是一种代码生成机制,可以生成不同类型的getter/setter函数,特别是假设你想要用点(.)操作符号来存取变量的话,你就能必须使用property. 怎样使用? 使用方法如:@pr ...
- UML之九图概述
最近看了UML的九种图的讲解,这九种图在我们以后的学习中起着举足轻重的作用,不管是在写文档,还是在对系统的需求.设计进行分析时,都很重要,所以首先做一下概述,希望能和大家分享. 首先和大家展示一下我对 ...
- IOS 轻量级数据持久化 DataLite
开发的过程中我们经常要保存一些配置信息,一般简单的是用 NSUserDefaults [[NSUserDefaults standardUserDefaults] objectForKey:key]; ...