打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Android 控件之ProgressBar进度条
From: 毁灭2012 http://www.cnblogs.com/salam/archive/2010/10/06/1844703.html#2717174 随笔- 125  文章- 0  评论- 442
ProgressBar是Android的进度条。体验效果
源码下载
下面详细介绍ProgressBar
一、说明
在某些操作的进度中的可视指示器,为用户呈现操作的进度,还它有一个次要的进度条,用来显示中间进度,如在流媒体播放的缓冲区的进度。一个进度条也可不确定其进度。在不确定模式下,进度条显示循环动画。这种模式常用于应用程序使用任务的长度是未知的。
二、XML重要属性
android:progressBarStyle:默认进度条样式
android:progressBarStyleHorizontal:水平样式
三、重要方法
getMax():返回这个进度条的范围的上限
getProgress():返回进度
getSecondaryProgress():返回次要进度
incrementProgressBy(int diff):指定增加的进度
isIndeterminate():指示进度条是否在不确定模式下
setIndeterminate(boolean indeterminate):设置不确定模式下
setVisibility(int v):设置该进度条是否可视
四、重要事件
onSizeChanged(int w, int h, int oldw, int oldh):当进度值改变时引发此事件
五、实例
1.布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ProgressBar android:id="@+id/progress_horizontal"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:max="100"
android:progress="50"
android:secondaryProgress="75" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="默认进度条" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button android:id="@+id/decrease"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="减少" />
<Button android:id="@+id/increase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增加" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自定义进度条" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button android:id="@+id/decrease_secondary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二减少" />
<Button android:id="@+id/increase_secondary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二增加" />
</LinearLayout>
</LinearLayout>
2.Java代码
package wjq.WidgetDemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
public class ProgressBarDemo extends Activity {
/* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.probarpage);
setProgressBarVisibility(true);
final ProgressBar progressHorizontal = (ProgressBar) findViewById(R.id.progress_horizontal);
setProgress(progressHorizontal.getProgress() * 100);
setSecondaryProgress(progressHorizontal.getSecondaryProgress() * 100);
Button button = (Button) findViewById(R.id.increase);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
progressHorizontal.incrementProgressBy(1);
// Title progress is in range 0..10000
setProgress(100 * progressHorizontal.getProgress());
}
});
button = (Button) findViewById(R.id.decrease);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
progressHorizontal.incrementProgressBy(-1);
// Title progress is in range 0..10000
setProgress(100 * progressHorizontal.getProgress());
}
});
button = (Button) findViewById(R.id.increase_secondary);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
progressHorizontal.incrementSecondaryProgressBy(1);
// Title progress is in range 0..10000
setSecondaryProgress(100 * progressHorizontal.getSecondaryProgress());
}
});
button = (Button) findViewById(R.id.decrease_secondary);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
progressHorizontal.incrementSecondaryProgressBy(-1);
// Title progress is in range 0..10000
setSecondaryProgress(100 * progressHorizontal.getSecondaryProgress());
}
});
}
}
评论补充(常见错误案例):
发表评论
#1楼 2011-10-07 17:08 | 风月瑾  
hi,你好!
我在学习控件ProgressBar时出现了一个很奇怪的问题,程序运行到最后突然就出错了。想了很久,也看了你介绍的资料,但是还是没有弄出来,你能不能有空时帮我看一下。
源码如下,其他地方没有改动过。
class文件:
package guo.cn;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
public class TestLayout3 extends Activity implements Runnable{
/** Called when the activity is first created. */
private ProgressBar progressBar1 = null;
private ProgressBar progressBar2 = null;
private Button button1 = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
progressBar1 = (ProgressBar)findViewById(R.id.progressBar1);
progressBar2 = (ProgressBar)findViewById(R.id.progressBar2);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
progressBar1.setVisibility(View.VISIBLE);
progressBar2.setVisibility(View.VISIBLE);
Thread thread = new Thread(TestLayout3.this);
thread.start();
}
});
}
@Override
public void run() {
// TODO Auto-generated method stub
int sum = 0;
for(;;){
progressBar1.setProgress(sum);
progressBar1.setSecondaryProgress(sum);
sum += 10;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(progressBar1.getProgress()==progressBar1.getMax())
{
progressBar1.setVisibility(View.INVISIBLE);
System.out.println("ggggggggggggg");
break;
}
}
}
}
main.xml文件如下:
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:max="200"
android:visibility="gone"
/>
<ProgressBar
android:id="@+id/progressBar2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyle"
android:visibility="gone"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
谢谢你!
#2楼 2011-10-13 11:55 | Mical Steve  
@风月瑾
把错误贴出来看看
#3楼 2011-10-13 15:44 | 风月瑾  
@Mical Steve
当进度条满时程序就中止了!
错误信息如下:
10-13 07:37:56.311: WARN/dalvikvm(288): threadid=17: thread exiting with uncaught exception (group=0x4001aa28)
10-13 07:37:56.311: ERROR/AndroidRuntime(288): Uncaught handler: thread Thread-9 exiting due to uncaught exception
10-13 07:37:56.326: ERROR/AndroidRuntime(288): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
10-13 07:37:56.326: ERROR/AndroidRuntime(288): at android.view.ViewRoot.checkThread(ViewRoot.java:2629)
10-13 07:37:56.326: ERROR/AndroidRuntime(288): at android.view.ViewRoot.invalidateChild(ViewRoot.java:558)
10-13 07:37:56.326: ERROR/AndroidRuntime(288): at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:584)
10-13 07:37:56.326: ERROR/AndroidRuntime(288): at android.view.ViewGroup.invalidateChild(ViewGroup.java:2391)
10-13 07:37:56.326: ERROR/AndroidRuntime(288): at android.view.View.invalidate(View.java:4742)
10-13 07:37:56.326: ERROR/AndroidRuntime(288): at android.view.View.setFlags(View.java:4292)
10-13 07:37:56.326: ERROR/AndroidRuntime(288): at android.view.View.setVisibility(View.java:2964)
10-13 07:37:56.326: ERROR/AndroidRuntime(288): at android.widget.ProgressBar.setVisibility(ProgressBar.java:753)
10-13 07:37:56.326: ERROR/AndroidRuntime(288): at guo.cn.TestLayout3.run(TestLayout3.java:51)
10-13 07:37:56.326: ERROR/AndroidRuntime(288): at java.lang.Thread.run(Thread.java:1060)
10-13 07:38:34.336: WARN/UsageStats(31): Unexpected resume of com.android.launcher while already resumed in guo.cn
10-13 07:38:34.466: WARN/InputManagerService(31): Got RemoteException sending setActive(false) notification to pid 288 uid 10025
谢谢你。谢谢你的帮助!
#4楼 2011-10-13 15:52 | Mical Steve  
这句progressBar1.setVisibility(View.INVISIBLE);
改成progressBar1.dismiss();看看,不行就把程序让我看看
#5楼 2012-02-15 15:13 | 海阔#天空  
你好,我的问题和楼上那位的问题一模一样的,就是我程序控制进度条满了之后我要让进度条隐藏,可是当我调用下面这个方法时就会出错
myFirstProgressBar.setVisibility(View.GONE);
出错信息和他的是一样的你说的那个
myFirstProgressBar.dismiss();
这个方法为什么我的ProgressBar里面没那个方法呢?
(.)点不出来
调用 setVisibility(View.VISIBLE);是没有问题的
但是调用setVisibility(View.INVISIBLE);或者setVisibility(View.GONE);的时候就会出错。不知道什么情况。我的也是在线程里的run里面写的。
#6楼 2012-11-03 13:50 | 月明星希er  
@风月瑾
@海阔#天空
看到这篇博文,然后发现博主没有给出问题的答案。
不知道上面2位解决问题了没?
其实出问题的原因是子线程内面是不能直接操作UI的(这里就是setVisibility). android内面子线程不能直接去操作UI线程, 这里可以通过Handler 通过子线程间发送msg消息给UI线程,然后在UI线程内操作UI. 下面直接贴代码吧。
改动的地方不多,就加一个Handler消息传递就可以了。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.xinje;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
public class ProgressBarActivity extends Activity implements Runnable {
/** Called when the activity is first created. */
private ProgressBar progressBar1 = null;
private ProgressBar progressBar2 = null;
private Button button1 = null;
Handler handler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
progressBar2 = (ProgressBar) findViewById(R.id.progressBar2);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
progressBar1.setVisibility(View.VISIBLE);
progressBar2.setVisibility(View.VISIBLE);
Thread thread = new Thread(ProgressBarActivity.this);
thread.start();
}
});
handler = new Handler() {
@Override
public void handleMessage(Message msg)
{
super.handleMessage(msg);
progressBar1.setVisibility(View.INVISIBLE);
}
};
}
public void run() {
// TODO Auto-generated method stub
int sum = 0;
for (;;) {
progressBar1.setProgress(sum);
progressBar1.setSecondaryProgress(sum);
sum += 10;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (progressBar1.getProgress() == progressBar1.getMax()) {
handler.sendMessage(handler.obtainMessage());
System.out.println("ggggggggggggg");
break;
}
}
}
}
#7楼 2013-05-17 18:38 | Monsieurchak  
你好,看了你的文章学到了东西,但是有一点不懂,想请教一下。
这里?
1
2
setProgress(progressHorizontal.getProgress() * 100);
setSecondaryProgress(progressHorizontal.getSecondaryProgress() * 100);
为什么要通过
?
1
progressHorizontal.getProgress() * 100
的形式来设置progress啊?还有后面的imcrease也同理。
我是直接?
1
2
ProgressBar.setProgress(0);
ProgressBar.incrementProgressBy(10);
然后我发现增加到100的时候就不会增加了,所以不会出现溢出的情况,如果需要精度可以取出ProgressBar.getProgress()来转换即可,不明白楼主这种做法是什么原因?本人新手,还请多多指教!
#8楼 2013-07-01 12:26 | 梦书  
@风月瑾
用Handler来修改ProgressBar
只能在主线程上修改
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
多线程异步处理:AsyncTask异步更新UI界面(详细完整总结篇)
Android Trick 1: 使用View来制作专业的分隔线
android:visibility=“gone”
Android圆形进度条颜色的设置
Android 进度条
Android Spinner的五个部分
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服