Android 控件之ProgressBar进度条
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());
}
});
}
}
当进度条满时程序就中止了!
错误信息如下:
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
谢谢你。谢谢你的帮助!
myFirstProgressBar.setVisibility(View.GONE);
出错信息和他的是一样的你说的那个
myFirstProgressBar.dismiss();
这个方法为什么我的ProgressBar里面没那个方法呢?
(.)点不出来
调用 setVisibility(View.VISIBLE);是没有问题的
但是调用setVisibility(View.INVISIBLE);或者setVisibility(View.GONE);的时候就会出错。不知道什么情况。我的也是在线程里的run里面写的。
这里
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()来转换即可,不明白楼主这种做法是什么原因?本人新手,还请多多指教!
我在学习控件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>
谢谢你!