在android中会经常用到ProgressBar,下面通过举例来说明如何使用ProgressBar。

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

public class A03Activity extends Activity {
private ProgressBar rectangle,circle;
private Button showProgressBar;
private final static int STOP=0x10000;
private final static int NEXT=0x10001;
private int count=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
rectangle=(ProgressBar)findViewById(R.id.rectangle);
circle=(ProgressBar)findViewById(R.id.circle);
showProgressBar=(Button)findViewById(R.id.showProgressBar);
rectangle.setIndeterminate(false);
circle.setIndeterminate(false);

showProgressBar.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
rectangle.setVisibility(View.VISIBLE);
circle.setVisibility(View.VISIBLE);
rectangle.setMax(100);
rectangle.setProgress(0);
circle.setProgress(0);
Thread t=new Thread(new Runnable(){

@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<20;i++){
try {
count=(i+1)*5;
Thread.sleep(1000);
if(count==19){
Message msg=new Message();
msg.what=STOP;
handler.sendMessage(msg);
break;
}
else{
Message msg=new Message();
msg.what=NEXT;
handler.sendMessage(msg);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
t.start();
}

});
}
private Handler handler=new Handler(){
@SuppressWarnings("static-access")
public void handleMessage(Message msg){
switch(msg.what){
case STOP:
rectangle.setVisibility(View.GONE);
circle.setVisibility(View.GONE);
Thread.currentThread().interrupt();
break;
case NEXT:
if(!Thread.currentThread().interrupted()){
rectangle.setProgress(count);
circle.setProgress(count);
}
break;
}
}
};
}

res/layout/main.xml如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ProgressBar
android:id="@+id/rectangle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
mce_style="?android:attr/progressBarStyleHorizontal"
android:visibility="gone"
/>
<ProgressBar
android:id="@+id/circle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleLarge"
mce_style="?android:attr/progressBarStyleLarge"
android:visibility="gone"
/>
<Button
android:id="@+id/showProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show ProgressBar"
/>

</LinearLayout>

更多相关文章

  1. AndroidStudio用gradle编译中文乱码
  2. AndroidStudio打包apk出现的问题 `Error:Execution failed for t
  3. android Imageview 图片覆盖具体实现
  4. Android(安卓)4.4报错,Android(安卓)7.0运行正常 Window type can
  5. android获得屏幕高度和宽度
  6. Android(安卓)studio 签名 报错:Could not download groovy-all.j
  7. 【android】shape的使用
  8. Android自学笔记(番外篇):全面搭建Linux环境(二)——VMware Workstati
  9. widget(5、DatePicker)

随机推荐

  1. C语言中的文件类型只有哪两种
  2. c#语言为什么用的人很少
  3. 三分钟带你了解选择排序和冒泡排序的用法
  4. c语言strcmp函数用法详解
  5. c语言中int范围是多少?
  6. C语言中switch语句的case后能否是一个关
  7. c语言中return的用法是什么?
  8. #include和#define是C语句吗?
  9. c语言中标识符不能与保留字同名吗?
  10. C语言中有且唯一的函数是什么