简易计时器

这个简易计时器,实现了简单的计时,点击开始按钮开始计时,点击停止按钮就停止,屏幕现实开始到停止的时间差。在run()方法里已换算成时分秒。


代码仅供参考:

strings.xml

    <string name="app_name">计时器</string> 
    <string name="text_time">00:00</string>
    <string name="button_start">记录</string>
    <string name="button_stop">取消</string>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/time"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="30sp"
        android:text="@string/text_time" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_margin="20dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button_start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_gravity="top|right"
            android:text="@string/button_start" />

        <Button
            android:id="@+id/button_stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_gravity="top|right"
            android:text="@string/button_stop" />
    </RelativeLayout>

</RelativeLayout>


MainActivity.java

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

/**
 * 计时器
 * @author JianDan
 */
public class MainActivity extends Activity implements OnClickListener {
	private TextView timer_time;
	
	private int count = 0;
	private int s = 0;
	private int m = 0;
	private int h = 0;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);
		timer_time = (TextView) findViewById(R.id.time);
<span style="white-space:pre">		</span>// 自动计时。
//		handler.postDelayed(runnable, 1000);
		
		this.findViewById(R.id.button_start).setOnClickListener(this);
		this.findViewById(R.id.button_stop).setOnClickListener(this);
	}

	private Handler handler = new Handler();
	private Runnable runnable = new Runnable() {
		@Override
		public void run() {
			String time = "";
			count++;
			if (count < 60) { // 小于60s
				if (count < 10) {
					time = "00:0" + count;
				} else {
					time = "00:" + count;
				}
			} else { // 大于等于60s
				m = count / 60; // 取整数,分钟
				s = count % 60; // 取余数,秒数
				if (m < 10) { // 小于10分钟
					if (s < 10) { 
						time = "0" + m + ":0" + s;
					} else {
						time = "0" + m + ":" + s;
					}
				} else {
					if (s < 10) {
						time = m + ":0" + s;
					} else {
						time = m + ":" + s;
					}
				}
				if (m > 60) {
					h = m / 60;
					time = h + ":" + m + ":" + s;
				}
			}
			timer_time.setText(time);
			
			handler.postDelayed(this, 1000);
		}
	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button_start:
			handler.postDelayed(runnable, 1000);
			break;
			
		case R.id.button_stop:
			count = 0;
			handler.removeCallbacks(runnable);
			break;

		default:
			break;
		}
	}
}

简易计时器源码下载 http://download.csdn.net/detail/xue_wei_love/7307737

参考博客:

http://blog.csdn.net/ithomer/article/details/6903084

http://blog.csdn.net/m_changgong/article/details/6844441

<pre code_snippet_id="327584" snippet_file_name="blog_20140505_4_7564325" name="code" class="java"><pre code_snippet_id="327584" snippet_file_name="blog_20140505_4_7564325" name="code" class="html"><pre code_snippet_id="327584" snippet_file_name="blog_20140505_4_7564325">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

                
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值