欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android中ProgressBar用法简单实例

程序员文章站 2024-02-21 14:43:58
本文实例讲述了android中progressbar用法。分享给大家供大家参考,具体如下: 在android中会经常用到progressbar,下面通过举例来说明如何使用...

本文实例讲述了android中progressbar用法。分享给大家供大家参考,具体如下:

在android中会经常用到progressbar,下面通过举例来说明如何使用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 a03activity extends activity {
 private progressbar rectangle,circle;
 private button showprogressbar;
 private final static int stop=0x10000;
 private final static int next=0x10001;
 private int count=0;
  /** called when the activity is first created. */
  @override
  public void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.main);
    rectangle=(progressbar)findviewbyid(r.id.rectangle);
    circle=(progressbar)findviewbyid(r.id.circle);
    showprogressbar=(button)findviewbyid(r.id.showprogressbar);
    rectangle.setindeterminate(false);
    circle.setindeterminate(false);
    showprogressbar.setonclicklistener(new onclicklistener(){
  @override
  public void onclick(view v) {
  // todo auto-generated method stub
  rectangle.setvisibility(view.visible);
  circle.setvisibility(view.visible);
  rectangle.setmax(100);
  rectangle.setprogress(0);
  circle.setprogress(0);
  thread t=new thread(new runnable(){
   @override
   public void run() {
   // todo auto-generated method stub
   for(int i=0;i<20;i++){
    try {
    count=(i+1)*5;
    thread.sleep(1000);
    if(count==19){
     message msg=new message();
     msg.what=stop;
     handler.sendmessage(msg);
     break;
    }
    else{
     message msg=new message();
     msg.what=next;
     handler.sendmessage(msg);
    }
    } catch (interruptedexception e) {
    // todo auto-generated catch block
    e.printstacktrace();
    }
   }
   }   
  });
  t.start();
  }
  });
  }
  private handler handler=new handler(){
   @suppresswarnings("static-access")
 public void handlemessage(message msg){
   switch(msg.what){
   case stop:
    rectangle.setvisibility(view.gone);
    circle.setvisibility(view.gone);
    thread.currentthread().interrupt();
    break;
   case next:
    if(!thread.currentthread().interrupted()){
    rectangle.setprogress(count);
    circle.setprogress(count);
    }
    break;
   }
   }
  };
}

res/layout/main.xml如下所示:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >
  <textview
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />
  <progressbar 
    android:id="@+id/rectangle"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    style="?android:attr/progressbarstylehorizontal"
    mce_style="?android:attr/progressbarstylehorizontal"
    android:visibility="gone"
    />
  <progressbar 
    android:id="@+id/circle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="?android:attr/progressbarstylelarge"
    mce_style="?android:attr/progressbarstylelarge"
    android:visibility="gone"    
    />
  <button 
    android:id="@+id/showprogressbar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="show progressbar"
    />
</linearlayout>

更多关于android控件相关内容感兴趣的读者可查看本站专题:《android控件用法总结

希望本文所述对大家android程序设计有所帮助。