`
IT阿狸
  • 浏览: 65556 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Android——显示时间和暂停时间(Handler类操作线程)

阅读更多

依旧使用Android 2.3.3版本

 

一、Handler类:

1.主要接受子线程发送的数据,并用此数据配合主线程更新UI。

2.当应用程序启动时,Android首先会开启一个主线程(也就是UI线程) , 主线程为管理界面中的UI控件,进行事件分发。

 

 

二、界面


 

 

三、main.xml

<?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" >

    <TextView
        android:id="@+id/info"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="当前时间"
        android:textColor="#0f0"
        android:textStyle="bold" />

    <Button
        android:id="@+id/startButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始" />

    <Button
        android:id="@+id/endButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="停止" />

</LinearLayout>

 

 

四、Activity类

package org.e276.time;

import java.sql.Time;

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.TextView;

/**
 * Handler类操作线程的实例
 * 
 * @author miao
 * 
 */
public class HandleDemoActivity extends Activity {

	private Button startButton;// 开始
	private Button endButton;// 结束
	private TextView textView;// 显示文本

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// 根据id获得控件对象
		startButton = (Button) findViewById(R.id.startButton);
		endButton = (Button) findViewById(R.id.endButton);
		textView = (TextView) findViewById(R.id.info);
		// 为控件设置监听器
		startButton.setOnClickListener(new StartButtonListener());
		endButton.setOnClickListener(new EndButtonListener());
	}

	// 创建Handler对象
	Handler handler = new Handler();
	// 新建一个线程对
	Runnable updateThread = new Runnable() {
		// 将要执行的曹祖哦写在线程对象的run方法当中
		public void run() {
			// 输出当前时间
			textView.setText("当前时间:" + new Time(System.currentTimeMillis()));
			/*
			 * 调用Handler的postDelayed()方法
			 * 这个方法的作用是:将要执行的线程对象放入到队列当中,待时间结束后,运行指定的线程对象
			 * 第一个参数是Runnable类型:将要执行的线程对象,第二个参数是long类型:延迟的时间,以毫秒为单位
			 */
			handler.postDelayed(updateThread, 1000);
		}
	};

	// 开始按钮
	class StartButtonListener implements OnClickListener {

		public void onClick(View v) {
			// 调用Handler的post()方法,将要执行的线程对象放到队列当中,如果队列中没有其他线程,就马上运行
			handler.post(updateThread);
		}

	}

	// 结束按钮
	class EndButtonListener implements OnClickListener {

		public void onClick(View v) {
			// 调用Handler的removeCallbacks()方法,删除队列当中未执行的线程对象
			handler.removeCallbacks(updateThread);
		}

	}

}

 

 

五、demo

Android-ShowTime.zip

  • 大小: 19.2 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics