`
暴风雪
  • 浏览: 377163 次
  • 性别: Icon_minigender_2
  • 来自: 杭州
社区版块
存档分类
最新评论

[Android]简单的Handler样例,通过线程队列和消息队列操作一个ProgressBar

阅读更多

      Handler主要用于异步消息的处理。

      这里点击了按钮之后在run方法中设置message的alg1的值,每次通过sendMessage把message对象压入消息队列。在handleMessage方法中取出消息队列中的message对象,并设置progressBar的值,然后再把线程对象加入线程队列。这个循环一直运行到进度条走到头~~。

 

package com.example.prograssbarhandle;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends Activity {
	private ProgressBar pb1=null;
	private Button bt1=null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt1=(Button)findViewById(R.id.but);
        pb1=(ProgressBar)findViewById(R.id.firstBar);
        pb1.setVisibility(View.VISIBLE);
        bt1.setOnClickListener(new bt1listener());
    }	

    class bt1listener implements OnClickListener{

		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			hl1.post(run1);
		}
    	
    }
    
    Handler hl1=new Handler()
    {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			pb1.setProgress(msg.arg1);
			hl1.post(run1);
		}
    	
    };
    
    Runnable run1=new Runnable()
    {
    	int i=0;
		public void run() {
			// TODO Auto-generated method stub
			i+=10;
			//得到一个消息对象
			Message msg=hl1.obtainMessage();
			msg.arg1=i;
			try{
				Thread.sleep(1000);
			}catch(InterruptedException e){
				e.printStackTrace();
			}
			if(i>=100){
				i=0;
				pb1.setProgress(i);
		//		bt1.setText("dawda");
				hl1.removeCallbacks(run1);
				return;
			}
			hl1.sendMessage(msg);
		}
    	
    };
     
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />
    <Button 
        android:id="@+id/but"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="start"
        />
    <ProgressBar
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:id="@+id/firstBar"
        style="?android:attr/progressBarStyleHorizontal" 
        />

</LinearLayout>
 
0
0
分享到:
评论

相关推荐

    asp.net知识库

    制作一个简单的多页Tab功能 一完美的关于请求的目录不存在而需要url重写的解决方案! 在C#中实现MSN消息框的功能 XmlHttp实现无刷新三联动ListBox 鼠标放在一个连接上,会显示图片(类似tooltip) 使用microsoft.web.ui...

    C#全能速查宝典

    《C#全能速查宝典》共分为8章,分别介绍了C#语言基础、Windows窗体及常用控件、Windows高级控件、控件公共属性、方法及事件、数据库开发、文件、数据流与注册表...,共包含562个C#编程中常用的属性、方法、类和各种技术...

    WPF编程宝典 part1

    6.6.2 ProgressBar 168 6.7 日期控件 169 6.8 小结 171 第7章 Application类 173 7.1 应用程序的生命周期 173 7.1.1 创建Application对象 173 7.1.2 派生自定义的Application类 174 7.1.3 应用程序的关闭方式 176 ...

    WPF编程宝典 part2

    6.6.2 ProgressBar 168 6.7 日期控件 169 6.8 小结 171 第7章 Application类 173 7.1 应用程序的生命周期 173 7.1.1 创建Application对象 173 7.1.2 派生自定义的Application类 174 7.1.3 应用程序的关闭方式 176 ...

    C#编程经验技巧宝典

    72 &lt;br&gt;0102 将字符串首字母转换大写 72 &lt;br&gt;0103 如何进行字节数组和字符串的相互转换 72 &lt;br&gt;0104 如何把一个按空格分割的字符串存储在一个ArrayList数组中 73 &lt;br&gt;4.2 获取字符串信息 73 ...

Global site tag (gtag.js) - Google Analytics