Handler的基本使用方法

package com.example.handler;

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

public class HandlerActivity extends Activity {
	private Button startbutton = null;
	private Button endbutton = null;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	//设置布局文件
		setContentView(R.layout.handler_activity);
	//设置按钮id
		startbutton = (Button)findViewById(R.id.start);
		endbutton = (Button)findViewById(R.id.end);
	//设置按钮文字
		startbutton.setText(R.string.start);
		endbutton.setText(R.string.end);
	//设置按钮监听器
		startbutton.setOnClickListener(new StartListener());
		endbutton.setOnClickListener(new EndListener());
	}
	
	//定义Handler对象
	Handler handler = new Handler();
	//当点击startbutton按钮之后会执行下面操作
	class StartListener implements OnClickListener {
		public void onClick(View arg0) {
	//使用Handler的post方法把updataThread这个线程加入到队列中,在消息队列中自动执行这个线程
			handler.post(updateThread);
		}
	}
	
	//当点击endbutton按钮之后会执行下面操作
	class EndListener implements OnClickListener {
		public void onClick(View arg0) {
	//从队列中取出线程
			handler.removeCallbacks(updateThread);
		}
	}
	
	//使用Runnable对象实现线程
	Runnable updateThread = new Runnable() {
		public void run() {
			System.out.println("updateThread");
	//当updataThread线程开始后会执行run方法,使用Handler的postDelayed方法在把当前线程对象updataThread隔2000毫秒之后加入到队列当中,并开始执行这个线程对象,执行之后又会调用Handler的postDelayed方法隔2000毫秒之后把线程对象updataThread加入到队列当中,依次循环执行这个线程对象...
			handler.postDelayed(updateThread, 2000);
		}
	};
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <Button 
        android:id="@+id/start"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/end"
        android:layout_width="fill_parent"
		android:layout_height="wrap_content"/>
</LinearLayout>


全部代码

package com.example.handler;

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

public class HandlerActivity extends Activity {
	private Button startbutton = null;
	private Button endbutton = null;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.handler_activity);
		startbutton = (Button)findViewById(R.id.start);
		endbutton = (Button)findViewById(R.id.end);
		startbutton.setText(R.string.start);
		endbutton.setText(R.string.end);
		startbutton.setOnClickListener(new StartListener());
		endbutton.setOnClickListener(new EndListener());
	}
	
	class StartListener implements OnClickListener {
		public void onClick(View arg0) {
			handler.post(updateThread);
		}
	}
	
	class EndListener implements OnClickListener {
		public void onClick(View arg0) {
			handler.removeCallbacks(updateThread);
		}
	}
	
	Handler handler = new Handler();
	Runnable updateThread = new Runnable() {
		public void run() {
			System.out.println("updateThread");
			handler.postDelayed(updateThread, 2000);
		}
	};
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <Button 
        android:id="@+id/start"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/end"
        android:layout_width="fill_parent"
		android:layout_height="wrap_content"/>
</LinearLayout>


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值