Handler和ProgressBar实现进度条的开始,暂停,停止,后退和循环
2012-05-06 18:34
363 查看
一,涉及的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
布局文件
显示效果
![](http://my.csdn.net/uploads/201205/06/1336300366_4184.jpg)
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
package com.song; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ProgressBar; public class C93_Handler3Activity extends Activity { /** Called when the activity is first created. */ ProgressBar bar; Button start,pause,back,stop; Handler handler; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); handler=new Handler(); bar=(ProgressBar)findViewById(R.id.bar); start=(Button)findViewById(R.id.start); pause=(Button)findViewById(R.id.pause); back=(Button)findViewById(R.id.back); stop=(Button)findViewById(R.id.stop); start.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //开始按钮,将要执行的线程对象放到队列当中 handler.post(runnable); } }); pause.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //暂停按钮,删除队列当中未执行的线程对象 handler.removeCallbacks(runnable); } }); back.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(prolength!=0) { prolength=bar.getProgress()-1; bar.setProgress(prolength); setTitle(String.valueOf(prolength)); } } }); stop.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub handler.removeCallbacks(runnable); bar.setProgress(0); setTitle(String.valueOf(0)); } }); } int prolength=0;//定义进程度 //定义线程 Runnable runnable=new Runnable() { @Override public void run() { // TODO Auto-generated method stub prolength=bar.getProgress()+1; bar.setProgress(prolength); setTitle(String.valueOf(prolength)); //如果进度小于100,则延迟1000毫秒之后重复执行runnable if(prolength<100) { handler.postDelayed(runnable, 1000); } //否则,都置零,线程重新执行 else { bar.setProgress(0); setTitle(String.valueOf(0)); handler.post(runnable); } } }; }
布局文件
<?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" > <ProgressBar android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/bar" style="@android:style/Widget.ProgressBar.Horizontal" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="2" android:id="@+id/start" android:text="开始"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="2" android:id="@+id/pause" android:text="暂停"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="2" android:id="@+id/back" android:text="后退"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="2" android:id="@+id/stop" android:text="停止"/> </LinearLayout> </LinearLayout>
显示效果
![](http://my.csdn.net/uploads/201205/06/1336300366_4184.jpg)
相关文章推荐
- Android Handler和ProgressBar实现进度条的开始,暂停,停止,后退和循环
- Handler和ProgressBar实现进度条的开始,暂停,停止,后退和循环
- Handler和ProgressBar实现开始停止循环进度条
- MFC 多线程拷贝文件开始暂停停止和进度条
- Android通过子线程和handler实现倒计时,可以开始暂停倒计时
- android Handler和Thread实现定时器开始计时和停止计时
- Unity3D 使用备忘(三) 实现相机自动漫游开始、暂停、停止及相机角度切换。
- progressBar和Handler实现进度条的加载 和RatingBar评分控件
- 在spring中实现quartz2.2.1的动态调度(开始、暂停、停止等)
- 使用Handler控制进度条,完成开始、暂停、继续、停止操作
- 安卓学习笔记---实现简易播放器(带有进度条,开始与结束时间,暂停与播放)
- ProgressBar进度条的实现
- 010给定一个循环链表,实现一个算法返回这个环的开始结点 (keep it up)
- Unity中游戏暂停和开始怎么实现?
- 利用js简单实现进度条功能,替换jquery progressbar
- ProgressBar的使用以及实现进度条的增减
- Unity简单计时器,实现随时播放,暂停,继续,停止
- progressBar两种实现方式Handler 和AsyncTask
- MediaElement视频控制:播放、暂停、停止、后退、快进、跳转、音量
- Android三种方式实现ProgressBar自定义圆形进度条