Android 秒表
来源:互联网 发布:淘宝提醒买家付款 编辑:程序博客网 时间:2024/05/01 14:16
2012年10月28日,手机没有秒表,自己想做一个秒表来给自己用,现在马上做出一个实例来,这只是开始,以后做个界面漂亮的应用出来。废话不说,先上图:
源码:
建立项目: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() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubuiHandle.removeMessages(1);startTime();isPaused = false;}}); //为按钮stop注册监听器 stop.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubisPaused = 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 秒表
- android秒表计时代码
- Android 秒表计时器
- 简易秒表Android源代码
- Android APP:秒表
- android 秒表倒计时
- 秒表
- 秒表
- 秒表
- 秒表
- Android自定义View---秒表/时钟
- Android实现秒表计时器的程序设计
- Android实现秒表计时器的程序设计
- Android实现秒表计时器的程序设计
- Android开发之小程序-秒表
- android下的一个简单秒表
- Android实现秒表计时器的程序设计
- android自定义的一个时间秒表的布局
- 10-23学习笔记
- 处理ASProtect的Advanced Import Protect的一些想法和过程(1)
- 命令行运行android模拟器
- POJ 2250解题报告
- 加勒比的伤感爱情日志分享:如果你知道我也爱你
- Android 秒表
- Java编程中“为了性能”需做的26件事
- HDU 2579 Dating with girls(2)
- 指针数组和数组指针的使用
- 小结构与小算法:利用树结构求集合的幂集
- 花花公子推荐伤感qq日志:乖不哭,我拜你
- 【作废】Inventor 二次开发学习指南入门到精通(含Inventor最新二次开发教程下载)
- 苏沫沫的原创伤感日志发布:我爱你比爱自己更过分
- SQLite学习