现在的位置: 首页 > 综合 > 正文

进度条组件:ProgressBar

2018年03月31日 ⁄ 综合 ⁄ 共 6325字 ⁄ 字号 评论关闭
文章目录

主要用于表示程序的操作进度

 

 

  默认风格的进度条

 

 

 

在main.xml中:

 

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:background="#000000"

    android:orientation="vertical"

    android:gravity="center_horizontal">

  <ProgressBar

       android:id="@+id/myprobarA"

       android:layout_marginTop="18dp"

       style="?android:attr/progressBarStyle"

       android:layout_width="50dp"

       android:layout_height="50dp"/>

</LinearLayout>

 

 

 

 

 

 

 

隐藏进度条

 

在main.xml中:

 

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:background="#000000"

    android:orientation="vertical"

    android:gravity="center_horizontal">

  <ProgressBar

       android:id="@+id/myprobarA"

       android:layout_marginTop="18dp"

       style="?android:attr/progressBarStyle"

       android:layout_width="50dp"

       android:layout_height="50dp"

       android:visibility="gone"/>      //隐藏进度条

</LinearLayout>

 

 

 

 

 

 

 

第二进度条(显示)

 

在main.xml中:

 

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:background="#000000"

    android:orientation="vertical"

    android:gravity="center_horizontal">

  <ProgressBar

       android:id="@+id/myprobarA"

       android:layout_marginTop="18dp"

       style="?android:attr/progressBarStyle"

       android:layout_width="50dp"

       android:layout_height="50dp"/>

  <ProgressBar

       android:id="@+id/myprobarB"

       android:layout_margin="8dp"

       style="?android:attr/progressBarStyleHorizontal"

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"/>

  <ProgressBar

       android:id="@+id/myprobarC"

       android:layout_margin="8dp"

       style="?android:attr/progressBarStyleHorizontal"

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       android:max="120"

       android:progress="80"/>

</LinearLayout>

 

 

 

 

 

控制进度条

 

在main.xml中:

 

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:background="#000000"

    android:orientation="vertical"

    android:gravity="center_horizontal">

  <ProgressBar

     android:id="@+id/myprobarA"

     android:visibility="gone"

     android:layout_marginTop="8dp"

     style="?android:attr/progressBarStyle"

     android:layout_width="wrap_content"

     android:layout_height="wrap_content" />

  <ProgressBar

     android:id="@+id/myprobarB"

     android:visibility="gone"

     android:layout_margin="8dp"

     style="?android:attr/progressBarStyleHorizontal"

     android:layout_width="fill_parent"

     android:layout_height="wrap_content" />

  <ProgressBar

     android:id="@+id/myprobarC"

     android:visibility="gone"

     android:layout_margin="8dp"

     style="?android:attr/progressBarStyleHorizontal"

     android:max="120"

     android:progress="0"

     android:layout_width="fill_parent"

     android:layout_height="wrap_content" />

  <Button

     android:id="@+id/mybut"

     android:text="显示进度条"

     android:background="#3399ff"

     android:textColor="#ffffff"

     android:layout_marginTop="18dp"

     android:layout_width="120dp"

     android:layout_height="40dp" />

</LinearLayout>

 

 

 

 

 

 

在MyProgressBarDemo.java中:

 

package com.li.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 MyProgressBarDemo extends Activity {

  private static final int STOP = 1 ;

  private static final int CONTINUE = 2 ;

  private ProgressBar myprobarA, myprobarB, myprobarC;

  private Button mybut ;

  @Override

  public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);

     super.setContentView(R.layout.main);

     this.myprobarA = (ProgressBar) super.findViewById(R.id.myprobarA) ;

     this.myprobarB = (ProgressBar) super.findViewById(R.id.myprobarB) ;

     this.myprobarC = (ProgressBar) super.findViewById(R.id.myprobarC) ;

     this.mybut = (Button) super.findViewById(R.id.mybut) ;

     this.myprobarA.setIndeterminate(false) ;  // 不确定模式

     this.myprobarB.setIndeterminate(false) ;  // 不确定模式

     this.myprobarC.setIndeterminate(true) ; // 确定模式

     this.mybut.setOnClickListener(new OnClickListenerImpl()) ;  // 单击事件

  }

  private class OnClickListenerImpl implements OnClickListener {

 

     public void onClick(View v) {

       MyProgressBarDemo.this.myprobarB.setSecondaryProgress(50) ;

       MyProgressBarDemo.this.myprobarA.setVisibility(View.VISIBLE) ;

       MyProgressBarDemo.this.myprobarB.setVisibility(View.VISIBLE) ;

       MyProgressBarDemo.this.myprobarC.setVisibility(View.VISIBLE) ;

      

       MyProgressBarDemo.this.myprobarA.setMax(120) ;

       MyProgressBarDemo.this.myprobarB.setMax(120) ;

       MyProgressBarDemo.this.myprobarA.setProgress(0) ;

       MyProgressBarDemo.this.myprobarB.setProgress(0) ;

      

       new Thread(new Runnable() {

         public void run() {

            int count = 0; // 保存当前进度的值

            for (int i = 0; i < 10; i++) {

              count = (i + 1) * 10; // 进度的增长快一些

              try {// 每次操作延迟500MS

                Thread.sleep(500);

              } catch (InterruptedException e) {

              }

              if (i == 12) {  // 正好增长到120

                Message m = new Message() ;

                m.what = MyProgressBarDemo.STOP ;  // 停止

                MyProgressBarDemo.this.myMessageHandler.sendMessage(m) ;// 停止

              } else {

                Message m = new Message() ;

                m.what = MyProgressBarDemo.CONTINUE ;

                m.arg1 = count ;

                MyProgressBarDemo.this.myMessageHandler.sendMessage(m) ;

              }

            }

         }

       }).start();

     }

    

  }

  private Handler myMessageHandler = new Handler() {

 

     @Override

     public void handleMessage(Message msg) {

       switch(msg.what) {

       case MyProgressBarDemo.STOP:

         MyProgressBarDemo.this.myprobarA.setVisibility(View.GONE) ;

         MyProgressBarDemo.this.myprobarB.setVisibility(View.GONE) ;

         MyProgressBarDemo.this.myprobarC.setVisibility(View.GONE) ;

         Thread.currentThread().interrupt() ;

         break;

       case MyProgressBarDemo.CONTINUE:

         if (!Thread.currentThread().isInterrupted()) { // 线程没有中断

           MyProgressBarDemo.this.myprobarA.setProgress(msg.arg1) ;

           MyProgressBarDemo.this.myprobarB.setProgress(msg.arg1) ;

           MyProgressBarDemo.this.myprobarC.setProgress(msg.arg1) ;

         }

         break;

       }

     }

  } ;

}

 

 

【上篇】
【下篇】

抱歉!评论已关闭.