Handler的基本使用方法

来源:互联网 发布:mac应用快捷键设置 编辑:程序博客网 时间:2024/06/06 08:33
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);//设置按钮idstartbutton = (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>


 

原创粉丝点击