Android 秒表

2012年10月28日,手机没有秒表,自己想做一个秒表来给自己用,现在马上做出一个实例来,这只是开始,以后做个界面漂亮的应用出来。废话不说,先上图:

             Android 秒表_第1张图片

 

源码:

建立项目:Stopwatch

代码清单:org/wwj/Stopwatch/Stopwatch.java

package org.wwj.Stopwatch;

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

public class Stopwatch extends Activity {
	
	private TextView minText;		//分
	private TextView secText;		//秒
	private Button start;			//开始按钮
	private Button stop;			//停止按钮
	private boolean isPaused = false;
	private String timeUsed;
	private int timeUsedInsec;
	
	private Handler uiHandle = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch(msg.what){
			case 1:
				if(!isPaused) {
					addTimeUsed();
					updateClockUI();
				}
				uiHandle.sendEmptyMessageDelayed(1, 1000);
				break;
			default: break;
			}
		}
	};
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stopwatch);
        
        //获取界面的控件
        minText = (TextView) findViewById(R.id.min);
        secText = (TextView) findViewById(R.id.sec);
        start = (Button) findViewById(R.id.start);
        stop = (Button) findViewById(R.id.stop);
        
        
        //为按钮Start注册监听器
        start.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				uiHandle.removeMessages(1);
				startTime();
				isPaused = false;
			}
		});
        
        //为按钮stop注册监听器
        stop.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				isPaused = true;
				timeUsedInsec = 0;
			}
		});
    }   
    
    
    @Override
    protected void onPause() {
    	// TODO Auto-generated method stub
    	super.onPause();
    	isPaused = true;
    }
    
    
    @Override
    protected void onResume() {
    	// TODO Auto-generated method stub
    	super.onResume();
    	isPaused = false;
    }
    
    private void startTime(){
    	uiHandle.sendEmptyMessageDelayed(1, 1000);
    }
    
    /**
     * 更新时间的显示
     */
    private void updateClockUI(){
    	minText.setText(getMin() + ":");
    	secText.setText(getSec());
    }
    
    public void addTimeUsed(){
    	timeUsedInsec = timeUsedInsec + 1;
    	timeUsed = this.getMin() + ":" + this.getSec();
    }
    
    public CharSequence getMin(){
    	return String.valueOf(timeUsedInsec / 60);
    }
    
    public CharSequence getSec(){
    	int sec = timeUsedInsec % 60;
    	return sec < 10? "0" + sec :String.valueOf(sec);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_stopwatch, menu);
        return true;
    }

    
}


 

界面布局:res/layout/layout_stopwatch.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
	<LinearLayout 
	    android:orientation="horizontal"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_gravity="center_horizontal">
	    <TextView 
	        android:id="@+id/min"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:textSize="40sp"/>
	    <TextView 
	        android:id="@+id/sec"
	        android:layout_width="wrap_content" 
	        android:layout_height="wrap_content"
	        android:textSize="40sp"
	        android:textColor="#ff0000"/>
	</LinearLayout>
	<LinearLayout 
	    android:orientation="horizontal"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_gravity="center_horizontal"
	    >
	    <Button 
	        android:id="@+id/start"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="@string/start"/>
	    <Button 
	        android:id="@+id/stop"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="@string/stop"/>
	</LinearLayout>
</LinearLayout>


 

你可能感兴趣的:(Android 秒表)