Chinaunix首页 | 论坛 | 博客
  • 博客访问: 710986
  • 博文数量: 160
  • 博客积分: 8847
  • 博客等级: 中将
  • 技术积分: 1656
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-25 16:46
个人简介

。。。。。。。。。。。。。。。。。。。。。。

文章分类

全部博文(160)

文章存档

2015年(1)

2013年(1)

2012年(4)

2011年(26)

2010年(14)

2009年(36)

2008年(38)

2007年(39)

2006年(1)

分类: Java

2011-08-24 22:41:45

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3.     android:orientation="vertical" android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent">
  5.     <ProgressBar android:layout_height="wrap_content"
  6.         android:layout_width="fill_parent" style="?android:attr/progressBarStyleHorizontal"
  7.         android:id="@+id/progressBar"
  8.         android:visibility="gone"
  9.         ></ProgressBar>
  10.     <Button android:text="Start" android:id="@+id/startButton"
  11.         android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
  12.     <Button android:text="End" android:id="@+id/endButton"
  13.         android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
  14. </LinearLayout>

  1. package cn.youhap.handler;

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

  10.     public class HandlerTestDriveActivity extends Activity {
  11.         /** Called when the activity is first created. */
  12.         
  13.         private ProgressBar progressBar;
  14.         private Button startButton;
  15.         private Button endButton;
  16.         @Override
  17.         public void onCreate(Bundle savedInstanceState) {
  18.             super.onCreate(savedInstanceState);
  19.             setContentView(R.layout.main);
  20.             progressBar = (ProgressBar)findViewById(R.id.progressBar);
  21.             startButton = (Button)findViewById(R.id.startButton);
  22.             endButton = (Button)findViewById(R.id.endButton);
  23.             startButton.setOnClickListener(new StartButtonListener());
  24.             endButton.setOnClickListener(new EndButtonListener());
  25.             enableStartButton();
  26.         }
  27.         
  28.         class StartButtonListener implements OnClickListener{
  29.             @Override
  30.             public void onClick(View arg0) {
  31.                 progressBar.setVisibility(View.VISIBLE);
  32.                 updateHandler.post(updateThread);//线程被加入线程队列
  33.                 disableStartButton();
  34.             }
  35.             
  36.         }
  37.         
  38.         private void enableStartButton(){
  39.             startButton.setEnabled(true);
  40.             endButton.setEnabled(false);
  41.         }
  42.         
  43.         private void disableStartButton(){
  44.             startButton.setEnabled(false);
  45.             endButton.setEnabled(true);
  46.         }
  47.         
  48.         private void initProgressBar(){
  49.             progressBar.setVisibility(View.GONE);
  50.             progressBar.setProgress(0);
  51.         }
  52.         
  53.         class EndButtonListener implements OnClickListener{

  54.             @Override
  55.             public void onClick(View arg0) {
  56.                 updateHandler.removeCallbacks(updateThread);
  57.                 initProgressBar();
  58.                 enableStartButton();
  59.             }
  60.             
  61.         }
  62.         
  63.         Handler updateHandler = new Handler(){

  64.             @Override
  65.             public void handleMessage(Message msg) {
  66.                 progressBar.setProgress(msg.arg1);
  67.                 updateHandler.post(updateThread); //线程再次被加入线程队列
  68.             }
  69.             
  70.         };
  71.         
  72.         Runnable updateThread = new Runnable(){
  73.             int i = 0;
  74.             @Override
  75.             public void run() {
  76.                 i += 10;
  77.                 Message msg = updateHandler.obtainMessage();
  78.                 msg.arg1 = i;
  79.                 
  80.                 try {
  81.                     Thread.sleep(1000L);
  82.                 } catch (InterruptedException e) {
  83.                     e.printStackTrace();
  84.                 }
  85.                 
  86.                 updateHandler.sendMessage(msg);//消息队列
  87.                 if (i >= progressBar.getMax()){
  88.                     i = 0;
  89.                     msg.arg1 = 0;
  90.                     initProgressBar();
  91.                     enableStartButton();
  92.                     updateHandler.removeCallbacks(updateThread);
  93.                     updateHandler.removeMessages(msg.arg1);
  94.                 }
  95.                 
  96.             }
  97.         };
  98.     }

阅读(790) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~