怎样控制界面控件之进度条(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 ...
随机推荐
- Fragment保持状态切换,fragment状态切换
在使用Activity管理多个Fragment时,每次切换Fragment使用的是replace,结果导致出现xxx is not currently in the FragmentManager异常 ...
- java.lang.ClassNotFoundException与java.lang.NoClassDefFoundError的区别(转)
ClassNotFoundException ClassNotFoundException这个错误,比较常见也好理解. 原因:就是找不到指定的class. 常见的场景就是: 1 调用class的for ...
- js遍历对象的数组
遍历数组: 1.js关键for遍历 2.jquery提供each功能 ----------------------------------- $.each(array, function(){ ...
- 监听手机晃动(摇一摇)SensorEventListener
import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; ...
- ios基础-编程规范
养成良好的编程习惯,是開始一门新语言的首要前提. (一)文档结构规范 1.建立Libs目录,存放第三方库 2.建立Tools目录,存放自己封装的类库 3.建立ViewControllers目录,存放全 ...
- Python爬虫入门三之Urllib库的基本使用
转自http://cuiqingcai.com/947.html 1.分分钟扒一个网页下来 怎样扒网页呢?其实就是根据URL来获取它的网页信息,虽然我们在浏览器中看到的是一幅幅优美的画面,但是其实是由 ...
- xcode6 中增加SDWebImage/SDWebImageDownloaderOperation.m报错解决方法
报错报错:Use of undeclared identifier '_executing' / '_finished': 解决方法例如以下:
- 我们究竟什么时候可以使用Ehcache缓存(转)
一.Ehcache是什么 EhCache是Hibernate的二级缓存技术之一,可以把查询出来的数据存储在内存或者磁盘,节省下次同样查询语句再次查询数据库,大幅减轻数据库压力. 二.Ehcache的使 ...
- 拍照图片滤镜sample
本文章主要介绍拍完照片后对图片的渲染进行处理 可以对拍出的照片进行选择不同的滤镜,令在图片上附有编辑框,供大家对图片进行描述,这是一个可以手动拖动的编辑框,在这里主要介绍下,手指放到控件上什么情况下视 ...
- sofa-pbrpc 1.1.1 发布,RPC 网络通信库
https://www.oschina.net/news/77372/sofa-pbrpc-1-1-1 https://www.oschina.net/p/sofa-pbrpc