Android应用开发学习笔记之进度条

作者:刘昊昱 

博客:http://blog.csdn.net/liuhaoyutz

 

进度条ProgressBar是一个经常用到的组件,它的使用也很简单,只需要把进度条显示在前台,然后在后台启动一个线程,根据需要修改进度条的状态。我们来看一个例子,该程序运行效果如下图所示:

该程序主布局文件main.xml内容如下:

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

    <ProgressBar
        android:id="@+id/progressBar1"
	  	android:layout_width="wrap_content"
	  	android:layout_height="wrap_content"/>
    
    <ProgressBar 
        android:id="@+id/progressBar2"
	  	android:layout_width="wrap_content"
	  	android:layout_height="wrap_content"
	  	style="?android:attr/progressBarStyleLarge"/>
    
    <ProgressBar 
        android:id="@+id/progressBar3"
	  	android:layout_width="wrap_content"
	  	android:layout_height="wrap_content"
	  	style="?android:attr/progressBarStyleSmall"/>
    
    <ProgressBar 
        android:id="@+id/progressBar4"
	  	android:layout_width="match_parent"
	  	android:layout_height="wrap_content"
	  	style="?android:attr/progressBarStyleHorizontal"
	  	android:max="100"
	  	android:progress="0"
	  	android:secondaryProgress="70"/>
    
    <ProgressBar
    	android:id="@+id/progressBar5" 
    	style="@android:style/Widget.ProgressBar.Large"
    	android:layout_width="match_parent" 
    	android:layout_height="wrap_content"/>
    
    <ProgressBar
    	android:id="@+id/progressBar6" 
    	style="@android:style/Widget.ProgressBar.Small"
    	android:layout_width="match_parent" 
    	android:layout_height="wrap_content"/>
    
    <ProgressBar
    	android:id="@+id/progressBar7" 
    	android:max="100"
    	android:progress="0"
    	android:secondaryProgress="70"
    	style="@android:style/Widget.ProgressBar.Horizontal"
    	android:layout_width="match_parent" 
    	android:layout_height="wrap_content"/>

</LinearLayout>


Activity文件内容如下:

package com.liuhaoyu;

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

public class MainActivity extends Activity {
	private ProgressBar pb1, pb2, pb3, pb4, pb5, pb6, pb7;
	private Handler PbHandler;
	private int progress = 0;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        pb1 = (ProgressBar)findViewById(R.id.progressBar1);
        pb2 = (ProgressBar)findViewById(R.id.progressBar2);
        pb3 = (ProgressBar)findViewById(R.id.progressBar3);
        pb4 = (ProgressBar)findViewById(R.id.progressBar4);
        pb5 = (ProgressBar)findViewById(R.id.progressBar5);
        pb6 = (ProgressBar)findViewById(R.id.progressBar6);
        pb7 = (ProgressBar)findViewById(R.id.progressBar7);
        
        PbHandler = new Handler(){
        	public void handleMessage(Message msg){
        		if(msg.what == 0x0){
        			pb4.setProgress(progress);
        			pb7.setProgress(progress);
        		}
        		else
        		{
        			Toast.makeText(MainActivity.this, "任务执行成功!", Toast.LENGTH_SHORT).show();
        			pb1.setVisibility(View.GONE);
        			pb2.setVisibility(View.GONE);
        			pb3.setVisibility(View.GONE);
        			pb4.setVisibility(View.GONE);
        			pb5.setVisibility(View.GONE);
        			pb6.setVisibility(View.GONE);
        			pb7.setVisibility(View.GONE);
        		}
        	}
        };
        
        new Thread(new Runnable() {
			public void run() {
				while (true) {
					progress += Math.random()*10;
					try{
						Thread.sleep(200);
					}
					catch(InterruptedException e){
						e.printStackTrace();
					}
					
					Message m = new Message();
					if(progress < 100){
						m.what = 0x0;
						PbHandler.sendMessage(m);
					}else{
						m.what=0x1;
						PbHandler.sendMessage(m);
						break;
					}
				}

			}
		}).start();
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值