android中的progressbar,Android中ProgressBar用法简单实例

本文提供了一个简单的Android ProgressBar使用示例,介绍了如何在活动中显示和更新进度条,包括矩形和圆形两种样式,并展示了相关XML布局文件的配置。通过一个线程动态改变进度并设置最大值,有助于理解ProgressBar在Android应用中的基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文实例讲述了android中progressbar用法。分享给大家供大家参考,具体如下:

在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如下所示:

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello" />

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"

/>

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"

/>

android:id="@+id/showprogressbar"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="show progressbar"

/>

更多关于android控件相关内容感兴趣的读者可查看本站专题:《android控件用法总结》

希望本文所述对大家android程序设计有所帮助。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值