• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
火磷
Memory will fade,but not notes.
博客园    首页    新随笔    联系   管理    订阅  订阅
Android开发--ProgressBar的应用

1.简介

Progress为加载进度的进度条。

2.构建

构建如图所示的界面,当按+键按钮时进度条增加;当按-键按钮时进度条减少。

      

xml:

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context="example.progressbar.Activity1" >
10 
11     <ProgressBar
12         android:id="@+id/progressBar1"
13         style="?android:attr/progressBarStyleHorizontal"
14         android:layout_width="fill_parent"
15         android:layout_height="60dp"
16         android:progress="10"                //设置进度条初始长度为10% 
17         android:layout_alignParentStart="true"
18         android:layout_alignParentTop="true" />
19 
20     <ProgressBar
21         android:id="@+id/progressBar2"
22         style="?android:attr/progressBarStyleLarge"
23         android:layout_width="wrap_content"
24         android:layout_height="wrap_content"
25         android:layout_below="@+id/progressBar1"
26         android:layout_centerHorizontal="true"
27         android:layout_marginTop="47dp" />
28 
29     <Button
30         android:id="@+id/button1"
31         android:layout_width="wrap_content"
32         android:layout_height="wrap_content"
33         android:layout_below="@+id/progressBar2"
34         android:layout_marginTop="72dp"
35         android:layout_toStartOf="@+id/progressBar2"
36         android:text="@string/bt1" />
37 
38     <Button
39         android:id="@+id/button2"
40         android:layout_width="wrap_content"
41         android:layout_height="wrap_content"
42         android:layout_alignBottom="@+id/button1"
43         android:layout_toEndOf="@+id/progressBar2"
44         android:text="@string/bt2" />
45 </RelativeLayout>

3.代码

 1 public class Activity1 extends Activity implements OnClickListener{
 2     Button b1,b2;
 3     ProgressBar pb;   
 4     @Override
 5     public void onClick(View v) {
 6               //给按钮做监听,通过ID进行匹配
 7         if(v.getId()==R.id.button1){
 8              // getPorgress()获取初始进度条的长度,乘以2则每次增加一倍,再通过setProgress()设置进度条        
 9             pb.setProgress((int) (pb.getProgress()*2));
10         }
11         else{
12             pb.setProgress((int) (pb.getProgress()*0.6));
13         }        
14     }
15 
16     @Override
17     protected void onCreate(Bundle savedInstanceState) {
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.act1);
20         b1=(Button) findViewById(R.id.button1);
21         b2=(Button) findViewById(R.id.button2);
22         pb=(ProgressBar) findViewById(R.id.progressBar1);
23         //设置监听器
24         b1.setOnClickListener(this);
25         b2.setOnClickListener(this);
26     }

4.效果

posted on 2016-02-12 13:51  火磷  阅读(289)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3