Android控件之ProgressBar探究

来源:互联网 发布:.com域名申请 编辑:程序博客网 时间:2024/06/06 02:07

 ProgressBar位于android.widget包下,其继承于View,主要用于显示一些操作的进度。应用程序可以修改其长度表示当前后台操作的完成情况。因为进度条会移动,所以长时间加载某些资源或者执行某些耗时的操作时,不会使用户界面失去响应。ProgressBar类的使用非常简单,只需将其显示到前台,然后启动一个后台线程定时更改表示进度的数值即可。

以下ProgressBar跟Handle结合,模拟进度条的使用,当进度条完成时会跳转到TestActivity

main.xml布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent">
  6.     <!-- 长方形进度条,一开始不可见,直到点击按钮时才出现进度条 -->
  7.     <ProgressBar android:id="@+id/progressBar"
  8.         style="?android:attr/progressBarStyleHorizontal"
  9.         mce_style="?android:attr/progressBarStyleHorizontal"
  10.         android:layout_width="fill_parent"
  11.         android:layout_height="wrap_content"
  12.         android:visibility="gone"
  13.         android:max="100" />
  14.     <!-- 圆形进度条 -->
  15.     <!--<ProgressBar android:id="@+id/progressBar"
  16.         style="?android:attr/progressBarStyleLarge"
  17.         mce_style="?android:attr/progressBarStyleLarge"
  18.         android:layout_width="wrap_content"
  19.         android:layout_height="wrap_content" />-->
  20.     <Button android:id="@+id/start"
  21.         android:text="启动进度条"
  22.         android:layout_width="wrap_content"
  23.         android:layout_height="wrap_content" />
  24.     <Button android:id="@+id/stop"
  25.         android:text="停止进度条"
  26.         android:layout_width="wrap_content"
  27.         android:layout_height="wrap_content" />
  28. </LinearLayout>
复制代码



PbActivity类

  1. package com.ljq.pb;

  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.os.Handler;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.ProgressBar;

  9. public class PbActivity extends Activity {
  10.     private ProgressBar progressBar = null;
  11.     private Button start = null, stop = null;
  12.     // 定义Handler对象
  13.     private Handler handler = new Handler();


  14.     @Override
  15.     public void onCreate(Bundle savedInstanceState) {
  16.         super.onCreate(savedInstanceState);
  17.         setContentView(R.layout.main);

  18.         progressBar = (ProgressBar) findViewById(R.id.progressBar);
  19.         progressBar.setProgress(0);
  20.         
  21.         start = (Button) findViewById(R.id.start);
  22.         start.setOnClickListener(new View.OnClickListener() {

  23.             public void onClick(View v) {
  24.                 handler.post(runnable); //开始执行
  25.             }
  26.                
  27.         });
  28.         stop=(Button)findViewById(R.id.stop);
  29.         stop.setOnClickListener(new View.OnClickListener() {

  30.             public void onClick(View v) {
  31.                 handler.removeCallbacks(runnable);//停止执行
  32.                 progressBar.setProgress(0);
  33.             }
  34.                
  35.         });
  36.     }

  37.     int pro=0;
  38.     Runnable runnable=new Runnable(){
  39.         public void run() {
  40.             progressBar.setVisibility(View.VISIBLE);
  41.             pro=progressBar.getProgress()+10;
  42.             progressBar.setProgress(pro);
  43.             //如果进度小于100,,则延迟1000毫秒后重复执行runnable
  44.             if(pro<100){
  45.                 handler.postDelayed(runnable, 1000);
  46.             }else{
  47.                 progressBar.setVisibility(View.GONE);
  48.                 startActivity(new Intent(PbActivity.this, TestActivity.class));
  49.                 handler.removeCallbacks(runnable);
  50.                 progressBar.setProgress(0);
  51.             }
  52.         }
  53.     };
  54. }
复制代码



运行结果

1.png

2011-5-14 08:57:25 上传
下载附件(27.07 KB)



分类: android常用控件
标签: android ProgressBar, android Handle
原创粉丝点击