Android开发之小程序-秒表

原文链接:http://blog.csdn.net/wwj_748/article/details/8120759

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

             

 

源码:

建立项目:Stopwatch

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

[java]  view plain copy
  1. package org.wwj.Stopwatch;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.os.Handler;  
  6. import android.view.Menu;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.TextView;  
  11.   
  12. public class Stopwatch extends Activity {  
  13.       
  14.     private TextView minText;       //分  
  15.     private TextView secText;       //秒  
  16.     private Button start;           //开始按钮  
  17.     private Button stop;            //停止按钮  
  18.     private boolean isPaused = false;  
  19.     private String timeUsed;  
  20.     private int timeUsedInsec;  
  21.       
  22.     private Handler uiHandle = new Handler(){  
  23.         public void handleMessage(android.os.Message msg) {  
  24.             switch(msg.what){  
  25.             case 1:  
  26.                 if(!isPaused) {  
  27.                     addTimeUsed();  
  28.                     updateClockUI();  
  29.                 }  
  30.                 uiHandle.sendEmptyMessageDelayed(11000);  
  31.                 break;  
  32.             defaultbreak;  
  33.             }  
  34.         }  
  35.     };  
  36.       
  37.       
  38.     @Override  
  39.     public void onCreate(Bundle savedInstanceState) {  
  40.         super.onCreate(savedInstanceState);  
  41.         setContentView(R.layout.activity_stopwatch);  
  42.           
  43.         //获取界面的控件  
  44.         minText = (TextView) findViewById(R.id.min);  
  45.         secText = (TextView) findViewById(R.id.sec);  
  46.         start = (Button) findViewById(R.id.start);  
  47.         stop = (Button) findViewById(R.id.stop);  
  48.           
  49.           
  50.         //为按钮Start注册监听器  
  51.         start.setOnClickListener(new OnClickListener() {  
  52.               
  53.             @Override  
  54.             public void onClick(View v) {  
  55.                 // TODO Auto-generated method stub  
  56.                 uiHandle.removeMessages(1);  
  57.                 startTime();  
  58.                 isPaused = false;  
  59.             }  
  60.         });  
  61.           
  62.         //为按钮stop注册监听器  
  63.         stop.setOnClickListener(new OnClickListener() {  
  64.               
  65.             @Override  
  66.             public void onClick(View v) {  
  67.                 // TODO Auto-generated method stub  
  68.                 isPaused = true;  
  69.                 timeUsedInsec = 0;  
  70.             }  
  71.         });  
  72.     }     
  73.       
  74.       
  75.     @Override  
  76.     protected void onPause() {  
  77.         // TODO Auto-generated method stub  
  78.         super.onPause();  
  79.         isPaused = true;  
  80.     }  
  81.       
  82.       
  83.     @Override  
  84.     protected void onResume() {  
  85.         // TODO Auto-generated method stub  
  86.         super.onResume();  
  87.         isPaused = false;  
  88.     }  
  89.       
  90.     private void startTime(){  
  91.         uiHandle.sendEmptyMessageDelayed(11000);  
  92.     }  
  93.       
  94.     /** 
  95.      * 更新时间的显示 
  96.      */  
  97.     private void updateClockUI(){  
  98.         minText.setText(getMin() + ":");  
  99.         secText.setText(getSec());  
  100.     }  
  101.       
  102.     public void addTimeUsed(){  
  103.         timeUsedInsec = timeUsedInsec + 1;  
  104.         timeUsed = this.getMin() + ":" + this.getSec();  
  105.     }  
  106.       
  107.     public CharSequence getMin(){  
  108.         return String.valueOf(timeUsedInsec / 60);  
  109.     }  
  110.       
  111.     public CharSequence getSec(){  
  112.         int sec = timeUsedInsec % 60;  
  113.         return sec < 10"0" + sec :String.valueOf(sec);  
  114.     }  
  115.       
  116.     @Override  
  117.     public boolean onCreateOptionsMenu(Menu menu) {  
  118.         getMenuInflater().inflate(R.menu.activity_stopwatch, menu);  
  119.         return true;  
  120.     }  
  121.   
  122.       
  123. }  


 

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

[html]  view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:id="@+id/LinearLayout1"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical" >  
  7.     <LinearLayout   
  8.         android:orientation="horizontal"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_gravity="center_horizontal">  
  12.         <TextView   
  13.             android:id="@+id/min"  
  14.             android:layout_width="wrap_content"  
  15.             android:layout_height="wrap_content"  
  16.             android:textSize="40sp"/>  
  17.         <TextView   
  18.             android:id="@+id/sec"  
  19.             android:layout_width="wrap_content"   
  20.             android:layout_height="wrap_content"  
  21.             android:textSize="40sp"  
  22.             android:textColor="#ff0000"/>  
  23.     </LinearLayout>  
  24.     <LinearLayout   
  25.         android:orientation="horizontal"  
  26.         android:layout_width="wrap_content"  
  27.         android:layout_height="wrap_content"  
  28.         android:layout_gravity="center_horizontal"  
  29.         >  
  30.         <Button   
  31.             android:id="@+id/start"  
  32.             android:layout_width="wrap_content"  
  33.             android:layout_height="wrap_content"  
  34.             android:text="@string/start"/>  
  35.         <Button   
  36.             android:id="@+id/stop"  
  37.             android:layout_width="wrap_content"  
  38.             android:layout_height="wrap_content"  
  39.             android:text="@string/stop"/>  
  40.     </LinearLayout>  
  41. </LinearLayout>  


  • 2
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值