Android 秒表
生活随笔
收集整理的這篇文章主要介紹了
Android 秒表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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;}}};@Overridepublic 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;}});} @Overrideprotected void onPause() {// TODO Auto-generated method stubsuper.onPause();isPaused = true;}@Overrideprotected void onResume() {// TODO Auto-generated method stubsuper.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);}@Overridepublic 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 秒表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: kettle Excel模板
- 下一篇: 加密软件运用了哪些技术