您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章将为大家详细讲解有关怎么在Android中使用ProgressBar实现一个进度条功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
具体方法如下:
public class MainActivity extends Activity {
//记录ProgressBar的完成进度
private int sum1=0,sum2 = 0 ;
ProgressBar bar1,bar2;
//创建一个负责更新进度的Handler
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
//表明消息是本程序发送的
if (msg.what == 0x111){
bar1.setProgress(sum1);
bar2.setProgress(sum2);
}
}
};
//模拟耗时
Thread thread = new Thread(){
@Override
public void run() {
while (sum2 < 100){
//bar1获取完成工作的百分比
if (sum1 > 100){
sum1 = 100;
if (sum2<100){
sum2 += (int) (Math.random()*25);
}else {
sum2 = 100;
thread.stop();
}
sum1=0;
}else {
sum1 = sum1 + (int) (Math.random()*25);
}
try{
Thread.sleep(1000);
}catch (InterruptedException e){
e.printStackTrace();
}
//更新ProgressBar
mHandler.sendEmptyMessage(0x111);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bar1 = (ProgressBar) findViewById(R.id.bar);
bar2 = (ProgressBar) findViewById(R.id.bar2);
thread.start();
}
}最后在给出布局文件:
<?xml version="1.0" encoding="utf-8" ?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical"> <android.support.v7.widget.Toolbar android:layout_width="match_parent" android:layout_height="wrap_content" app:contentInsetStart="0dp" android:background="#9FB6CD"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <ProgressBar android:id="@+id/toolbar_progress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" /> </RelativeLayout> </android.support.v7.widget.Toolbar> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <!--定义一个大环型进度条--> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" /> <!--定义一个中等大小环形进度条--> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" /> <!--定义一个小进度条--> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="任务完成的进度"/> <!--定义一个大水平进度条--> <ProgressBar android:id="@+id/bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" /> <!--顶一个水平进度条,并改变轨道外观--> <ProgressBar android:id="@+id/bar2" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progressDrawable="@drawable/my_bar" /> </LinearLayout>
关于怎么在Android中使用ProgressBar实现一个进度条功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。