举例说明android中ProgressBar的用法
2012-09-02 19:40
267 查看
在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>
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>
相关文章推荐
- 举例说明android中ImageView的用法
- 举例说明Android运行时权限用法
- 举例说明Android中ViewFlipper的用法
- 举例说明Android中Button中的OnClickListener更简便的用法
- 举例说明android中SmsManager的用法---用来实现手机发送短信的功能
- 举例说明这些键的用法
- ref和out的用法说明举例(转)
- wget参数说明和用法举例
- 举例说明如何在android中实现设置黑名单的功能--AudioManager、PhoneStateListener的使用
- android:id="@+id/android:empty属性的用法举例
- Linux 2.6 内核Epoll用法举例说明
- Android.mk和Application.mk文件语法规范说明及举例
- 举例说明android中ListPreference的使用方法
- Android.mk和Application.mk文件语法规范说明及举例
- linux 2.6内核epoll用法举例说明(续)--给echo服务器增加读线程池
- Android控件之ProgressBar用法实例分析
- android:id="@+id/android:empty属性的用法举例
- 简单举例说明C++中break和continue语句的用法
- 举例说明在android中使用正则表达式去掉字符串中标点符号及其他一些特殊符号的方法
- Android中ProgressBar的用法