Handler和ProgressBar实现进度条的开始,暂停,停止,后退和循环

一,涉及的handler类方法

1,

post(Runnable r)

Causes the Runnable r to be added to the message queue.将要执行的线程对象加到队列当中 
2,
removeCallbacks(Runnable r)
Remove any pending posts of Runnable r that are in the message queue.移除队列当中未执行的线程对象
3,
postDelayed(Runnable r, long delayMillis)
Causes the Runnable r to be added to the message queue, to be run after the specified amount of time elapses.
将要执行的线程对象放入到队列当中,待时间结束后,运行制定的线程对象

二,编写程序

程序效果:实现进度条的开始,暂停,停止,后退和循环

主activity
[java]  view plain copy
  1. package com.song;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.os.Handler;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.ProgressBar;  
  10.   
  11. public class C93_Handler3Activity extends Activity {  
  12.     /** Called when the activity is first created. */  
  13.     ProgressBar bar;  
  14.     Button start,pause,back,stop;  
  15.     Handler handler;  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.         handler=new Handler();  
  21.         bar=(ProgressBar)findViewById(R.id.bar);  
  22.         start=(Button)findViewById(R.id.start);  
  23.         pause=(Button)findViewById(R.id.pause);  
  24.         back=(Button)findViewById(R.id.back);  
  25.         stop=(Button)findViewById(R.id.stop);  
  26.         start.setOnClickListener(new OnClickListener() {  
  27.               
  28.             @Override  
  29.             public void onClick(View v) {  
  30.                 // TODO Auto-generated method stub  
  31.                 //开始按钮,将要执行的线程对象放到队列当中   
  32.                 handler.post(runnable);  
  33.             }  
  34.         });  
  35.         pause.setOnClickListener(new OnClickListener() {  
  36.               
  37.             @Override  
  38.             public void onClick(View v) {  
  39.                 // TODO Auto-generated method stub  
  40.                 //暂停按钮,删除队列当中未执行的线程对象  
  41.                 handler.removeCallbacks(runnable);  
  42.             }  
  43.         });  
  44.         back.setOnClickListener(new OnClickListener() {  
  45.               
  46.             @Override  
  47.             public void onClick(View v) {  
  48.                 // TODO Auto-generated method stub  
  49.                 if(prolength!=0)  
  50.                 {  
  51.                     prolength=bar.getProgress()-1;  
  52.                     bar.setProgress(prolength);  
  53.                     setTitle(String.valueOf(prolength));  
  54.                 }  
  55.             }  
  56.         });  
  57.         stop.setOnClickListener(new OnClickListener() {  
  58.               
  59.             @Override  
  60.             public void onClick(View v) {  
  61.                 // TODO Auto-generated method stub  
  62.                 handler.removeCallbacks(runnable);  
  63.                 bar.setProgress(0);  
  64.                 setTitle(String.valueOf(0));  
  65.             }  
  66.         });  
  67.           
  68.     }  
  69.     int prolength=0;//定义进程度  
  70.     //定义线程  
  71.     Runnable runnable=new Runnable() {  
  72.           
  73.         @Override  
  74.         public void run() {  
  75.             // TODO Auto-generated method stub  
  76.             prolength=bar.getProgress()+1;  
  77.             bar.setProgress(prolength);  
  78.             setTitle(String.valueOf(prolength));  
  79.             //如果进度小于100,则延迟1000毫秒之后重复执行runnable  
  80.             if(prolength<100)  
  81.             {  
  82.                 handler.postDelayed(runnable, 1000);  
  83.             }  
  84.             //否则,都置零,线程重新执行  
  85.             else   
  86.             {  
  87.                 bar.setProgress(0);  
  88.                 setTitle(String.valueOf(0));  
  89.                 handler.post(runnable);  
  90.             }  
  91.         }  
  92.     };  
  93. }  

布局文件
[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.    <ProgressBar android:layout_width="fill_parent"  
  8.        android:layout_height="wrap_content"  
  9.        android:id="@+id/bar"  
  10.        style="@android :style/Widget.ProgressBar.Horizontal"  
  11.        />  
  12.    <LinearLayout android:layout_width="fill_parent"  
  13.        android:layout_height="fill_parent"  
  14.        android:orientation="horizontal">  
  15.        <Button android:layout_width="wrap_content"  
  16.        android:layout_height="wrap_content"  
  17.        android:layout_weight="2"  
  18.        android:id="@+id/start"  
  19.        android:text="开始"/>  
  20.        <Button android:layout_width="wrap_content"  
  21.        android:layout_height="wrap_content"  
  22.        android:layout_weight="2"  
  23.        android:id="@+id/pause"  
  24.        android:text="暂停"/>  
  25.        <Button android:layout_width="wrap_content"  
  26.        android:layout_height="wrap_content"  
  27.        android:layout_weight="2"  
  28.        android:id="@+id/back"  
  29.        android:text="后退"/>  
  30.        <Button android:layout_width="wrap_content"  
  31.            android:layout_height="wrap_content"  
  32.            android:layout_weight="2"  
  33.            android:id="@+id/stop"  
  34.            android:text="停止"/>  
  35.    </LinearLayout>  
  36.       
  37.   
  38. </LinearLayout>  

显示效果

转载于:https://my.oschina.net/zhangjie830621/blog/113452

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值