Android實現(xiàn)APP秒表功能
本文實例為大家分享了Android實現(xiàn)APP秒表功能的具體代碼,供大家參考,具體內(nèi)容如下
這幾天一直在看安卓,也正好趕上老師布置的作業(yè),所以就做了一個秒表。自己參考了一下別人的圖標(biāo),有了一些靈感所以順便也設(shè)計了一下界面。下面先貼一下秒表的界面:
打開秒表后的第一個界面

點擊開始計時,開始鍵變?yōu)闀和?,記錄和停止開始變實:

點擊記錄:

記錄滿了之后自動上移,通過滑動可以查看前面的:

點擊暫停:

停止:

重新開始和記錄:

雙擊返回鍵退出:

下面貼出Activity的代碼:
package com.example.stopwatch;
?
import java.util.Timer;
import java.util.TimerTask;
?
import android.R.bool;
?
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.AssetManager;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
?
?
public class MainActivity extends Activity {
?? ?
?? ?
?? ?private boolean mStart = false;
?? ?private long mStartTime;
?? ?private boolean mIsRecorded;
?? ?private LinearLayout linearLayout;
?? ?private int recordTimes;
?? ?private long currentTime;
?? ?private long lastTime = 0;
?? ?private long tmpTime;
?? ?private boolean isExit = false;
?? ?
?? ?//更新顯示時間的關(guān)鍵
?? ?private Handler mHandler = new Handler() {
?? ??? ?public void handleMessage(Message msg) {
?? ??? ??? ?switch (msg.what) {
?? ??? ??? ?case 1:
?? ??? ??? ??? ?if (mStart) {
?? ??? ??? ??? ??? ?updateTime();
?? ??? ??? ??? ??? ?mHandler.sendEmptyMessage(1);
?? ??? ??? ??? ?}
?? ??? ??? ??? ?break;
?? ??? ??? ?case 0:
?? ??? ??? ??? ?break;
?? ??? ??? ?default:
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ?}
?? ?};
?
?
?? ?@Override
?? ?protected void onCreate(Bundle savedInstanceState) {
?? ??? ?super.onCreate(savedInstanceState);
?? ??? ?setContentView(R.layout.activity_main);
?? ??? ?
?? ??? ?TextView textView1 = (TextView) findViewById(R.id.textView1);
?? ??? ?TextView textView2 = (TextView) findViewById(R.id.textView2);
?? ??? ?
?? ??? ?//修改時間的字體
?? ??? ?AssetManager mgr=getAssets();//得到AssetManager
?? ??? ?Typeface tf=Typeface.createFromAsset(mgr, "fonts/lanting.TTF");//根據(jù)路徑得到Typeface
?? ??? ?textView1.setTypeface(tf);
?? ??? ?textView2.setTypeface(tf);
?? ??? ?
?? ? ? ?final Button button_start = (Button) findViewById(R.id.button_start);
?? ? ? ?final Button button_record = (Button) findViewById(R.id.button_record);
?? ? ? ?final Button button_stop = (Button) findViewById(R.id.button_stop);
?? ? ? ?
?? ??? ?button_start.setText("開始");?? ?
?? ??? ?
?? ??? ?//監(jiān)聽開始按鈕
? ? ? ? button_start.setOnClickListener(new OnClickListener(){
?
?? ??? ??? ?public void onClick(View V)
?? ? ? ? ? ?{
?? ??? ??? ??? ?if(button_start.getText() == "開始") {
?? ??? ??? ??? ??? ?mStart = true;
?? ??? ??? ??? ??? ?mStartTime = System.currentTimeMillis();
?? ??? ??? ??? ? ? ?button_start.setText("暫停");
?? ??? ??? ??? ? ? ?button_record.setBackgroundResource(R.drawable.button_record_full);
?? ??? ??? ??? ? ? ?button_stop.setBackgroundResource(R.drawable.button_stop_full);
?? ??? ??? ??? ? ? ? ?
?? ? ? ? ??? ??? ? ? ?lastTime = 0;
?? ??? ??? ??? ? ? ?recordTimes = 0;
?? ??? ??? ??? ? ? ?linearLayout = (LinearLayout) findViewById(R.id.linearlayout1);
?? ??? ??? ??? ? ? ?linearLayout.removeAllViewsInLayout();
?? ? ? ? ??? ??? ? ? ?mHandler.sendEmptyMessage(1);
?? ? ? ? ? ? ? ?}
?? ??? ??? ??? ?else if(button_start.getText() == "暫停"){
?? ??? ??? ??? ??? ?mStart = false;
?? ??? ??? ??? ??? ?tmpTime = System.currentTimeMillis();
?? ??? ??? ??? ??? ?button_start.setText("繼續(xù)");
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?button_record.setBackgroundResource(R.drawable.button_record_half);
?? ??? ??? ??? ??? ?mHandler.sendEmptyMessage(0);
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else {
?? ??? ??? ??? ??? ?mStart = true;
?? ??? ??? ??? ??? ?long tmp = System.currentTimeMillis() - tmpTime;
?? ??? ??? ??? ??? ?mStartTime = mStartTime + tmp;
?? ??? ??? ??? ??? ?button_start.setText("暫停");
?? ??? ??? ??? ??? ?button_record.setBackgroundResource(R.drawable.button_record_full);
?? ??? ??? ??? ??? ?mHandler.sendEmptyMessage(1);
?? ??? ??? ??? ?}
?? ? ? ? ? ?}
?? ? ? ? });
? ? ? ??
? ? ? ? //監(jiān)聽停止按鈕
? ? ? ? button_stop.setOnClickListener(new OnClickListener() {
?? ??? ??? ?
?? ??? ??? ?public void onClick(View arg0) {
?? ??? ??? ??? ?// TODO Auto-generated method stub
?? ??? ??? ??? ?if(button_start.getText() != "開始"){
?? ??? ??? ??? ??? ?mStart = false;
?? ??? ??? ??? ??? ?button_start.setText("開始");
?? ??? ??? ??? ??? ?button_stop.setBackgroundResource(R.drawable.button_stop_half);
?? ??? ??? ??? ??? ?button_record.setBackgroundResource(R.drawable.button_record_half);
?? ??? ??? ??? ??? ?TextView textView1 = (TextView) findViewById(R.id.textView1);
?? ??? ??? ??? ??? ?TextView textView2 = (TextView) findViewById(R.id.textView2);
?? ??? ??? ??? ??? ?textView1.setText("00:00:00");
?? ??? ??? ??? ??? ?textView2.setText("00");?? ?
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?});
? ? ? ??
? ? ? ? //監(jiān)聽記錄按鈕
? ? ? ? button_record.setOnClickListener(new OnClickListener() {
?
?? ??? ??? ?public void onClick(View arg0) {
?? ??? ??? ??? ?// TODO Auto-generated method stub
?? ??? ??? ??? ?if(button_start.getText() == "暫停"){
?? ??? ??? ??? ??? ?mIsRecorded = true;
?? ??? ??? ??? ??? ?mHandler.sendEmptyMessage(1);
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?});
?? ?}
?? ?
?? ?//更新顯示時間和顯示記錄的時間
?? ?private void updateTime() {
?? ??? ?TextView textView1 = (TextView) findViewById(R.id.textView1);
?? ??? ?TextView textView2 = (TextView) findViewById(R.id.textView2);?? ??? ?
?? ??? ?currentTime = System.currentTimeMillis();
?? ??? ?long aTime = currentTime - mStartTime;
?? ??? ?StringBuilder[] sb1 = new StringBuilder[2];
??? ? ? ?sb1[0] = new StringBuilder();
??? ? ? ?sb1[1] = new StringBuilder();
?? ??? ?sb1 = getTimeFormat(aTime);
?? ??? ?String str;
?? ??? ?textView1.setText(sb1[0]);
?? ??? ?textView2.setText(sb1[1]);?? ??? ?
?? ??? ?
? ? ? ?? ?if(mIsRecorded) {
? ? ? ?? ??? ?recordTimes++;
? ? ? ?? ??? ?String rec;
? ? ? ?? ??? ?long bTime;
? ? ? ?? ??? ?if (recordTimes == 1) {
? ? ? ?? ??? ??? ?bTime = aTime;
? ? ? ?? ??? ??? ?
? ? ? ?? ??? ?}
? ? ? ?? ??? ?else {
?? ??? ??? ? ? ?bTime = currentTime - lastTime;
? ? ? ?? ??? ?}
? ? ? ?? ??? ?
? ? ? ?? ??? ?StringBuilder[] sb2 = new StringBuilder[2];
? ? ??? ? ? ?sb2[0] = new StringBuilder();
? ? ??? ? ? ?sb2[1] = new StringBuilder();
? ? ?? ??? ?sb2 = getTimeFormat(bTime);
? ? ?? ??? ?if(recordTimes < 10)
? ? ?? ??? ?{
? ? ?? ??? ??? ?rec = '0' + String.valueOf(recordTimes);
? ? ?? ??? ?}
? ? ?? ??? ?else {
? ? ?? ??? ??? ?rec = String.valueOf(recordTimes);
? ? ?? ??? ?}
? ? ? ?? ??? ?str = "<font color='orange'>" + rec + "</font>" + " <small>" + sb2[0].toString() +"." + sb2[1].toString() + "</small>" + " ";
? ? ? ?? ? ? ?str += "<b>" + sb1[0].toString() + ".<small>" + sb1[1].toString() + "</small>" + "</b>";
? ? ? ?? ??? ?CharSequence charSequence = Html.fromHtml(str);
? ? ? ?? ??? ?
? ? ? ?? ? ? ?TextView text1 = new TextView(this);
?? ? ? ? ? ?text1.setText(charSequence);
?? ? ? ? ? ?text1.setTextSize(23);
?? ? ? ? ? ?text1.setTextColor(Color.WHITE);
?? ? ? ? ? ?text1.setGravity(Gravity.CENTER);
?? ? ? ? ? ?AssetManager mgr=getAssets();//得到AssetManager
?? ??? ??? ?Typeface tf=Typeface.createFromAsset(mgr, "fonts/lanting.TTF");//根據(jù)路徑得到Typeface
?? ??? ??? ?text1.setTypeface(tf);
?? ? ? ? ? ?
?? ??? ??? ?TextView text2 = new TextView(this);
?? ??? ??? ?text2.setText(" ");
?? ??? ??? ?text2.setTextSize(10);
?? ??? ??? ?linearLayout.addView(text2);
?? ??? ??? ?linearLayout.addView(text1);
?? ??? ??? ??
?? ??? ??? ?final ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView1);
?? ??? ??? ?Runnable mScrollToBottom = new Runnable()?
?? ??? ? ? ?{ ??
?? ??? ? ? ? ? ?@Override ?
?? ??? ? ? ? ? ?public void run()
?? ??? ? ? ? ? ?{ ??
?? ??? ? ? ? ? ? ? ?int off = linearLayout.getMeasuredHeight() - scrollView.getHeight(); ??
?? ??? ? ? ? ? ? ? ?if (off > 0)?
?? ??? ? ? ? ? ? ? ?{ ??
?? ??? ? ? ? ? ? ? ??? ?scrollView.scrollTo(0, off); ??
?? ??? ? ? ? ? ? ? ?} ? ? ? ? ? ? ? ? ? ? ? ? ?
?? ??? ? ? ? ? ?} ??
?? ??? ? ? ?};?
?? ??? ? ? ?mHandler.post(mScrollToBottom);?
?? ? ? ? ? ?mIsRecorded =false;
?? ? ? ? ??? ?lastTime = currentTime;
? ? ? ?? ?}
?? ?}
?? ?
?? ?//把毫秒轉(zhuǎn)為要顯示的格式
?? ?public StringBuilder[] getTimeFormat(long time) {
?? ? ? ?long tmp = time;
?? ??? ?time = time / 1000;
? ?? ? ? ?int second = (int) (time % 60);
??? ? ? ?int minute = (int) (time / 60) % 60;
??? ? ? ?int hour = (int) (time / 3600);
??? ? ? ?int minsecond = (int) (tmp / 10 % 100);
??? ? ? ?StringBuilder[] sb = new StringBuilder[2];
??? ? ? ?sb[0] = new StringBuilder();
??? ? ? ?sb[1] = new StringBuilder();
??? ? ??
??? ? ? ?if(hour < 10) {
??? ? ? ??? ?sb[0].append('0');
??? ? ? ? ? ?sb[0].append(String.valueOf(hour));
??? ? ? ?}
??? ? ? ?else {
? ?? ??? ?sb[0].append(String.valueOf(hour));
? ?? ? ? ?}
? ?? ? ? ?sb[0].append(':');
? ?? ? ? ?if(minute < 10) {
? ?? ? ? ??? ?sb[0].append('0');
? ?? ??? ? ? ?sb[0].append(String.valueOf(minute));
? ?? ??? ?}
? ?? ? ? ?else {
? ?? ??? ? ? ?sb[0].append(String.valueOf(minute));
? ?? ? ? ?}
? ?? ? ? ?sb[0].append(':');
? ?? ? ? ?if(second < 10) {
? ?? ??? ? ? ?sb[0].append('0');
? ?? ??? ? ? ?sb[0].append(String.valueOf(second));
? ?? ? ? ?}
? ?? ? ? ?else {
? ?? ??? ?sb[0].append(String.valueOf(second));
? ?? ? ? ?}
? ?? ? ? ?if(minsecond < 10) {
? ?? ? ? ??? ?sb[1].append('0');
? ?? ? ? ??? ?sb[1].append(minsecond);
? ?? ? ? ?}
? ? ??? ?else {
? ? ??? ??? ?sb[1].append(minsecond);
? ? ??? ?}
? ?? ? ? ?return sb;
?? ?}
?? ?
?? ?//監(jiān)聽返回鍵,實現(xiàn)點擊返回鍵時彈出對話,連續(xù)兩次點擊退出
?? ?@Override
?? ?public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {
?? ??? ?if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
?? ??? ??? ?toast();
?? ??? ??? ?return false;
?? ??? ?}
?? ??? ?else if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 1) {
?? ??? ??? ?MainActivity.this.finish();?
?? ??? ?}
?? ??? ?return false;
?? ?};
?? ?
?? ?/*protected void gialog() {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?AlertDialog.Builder builder = new Builder(MainActivity.this);
?? ??? ?builder.setTitle("提示");
?? ??? ?builder.setMessage("確定要退出嗎?");
?? ??? ?builder.setPositiveButton("確認(rèn)",?
? ? ? ? ? ? ? ? new android.content.DialogInterface.OnClickListener() {?
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) {?
? ? ? ? ? ? ? ? ? ? ? ? dialog.dismiss();?
? ? ? ? ? ? ? ? ? ? ? ? MainActivity.this.finish();?
? ? ? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? });?
? ? ? ? builder.setNegativeButton("取消",?
? ? ? ? ? ? ? ? new android.content.DialogInterface.OnClickListener() {?
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) {?
? ? ? ? ? ? ? ? ? ? ? ? dialog.dismiss();?
? ? ? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? });?
? ? ? ? builder.create().show();?
?? ?}*/
?? ?
?? ?protected void toast() {
?? ??? ?Timer tExit = null;
?? ??? ?if (isExit == false) {
?? ??? ?isExit = true; // 準(zhǔn)備退出
?? ??? ?Toast textToast = Toast.makeText(this, "小樣!想退出?!", Toast.LENGTH_LONG);
?? ??? ?textToast.show();
?? ??? ?tExit = new Timer();
?? ? ? ?tExit.schedule(new TimerTask() {
?? ? ? ??? ?@Override
?? ??? ? ? ?public void run() {
?? ??? ? ? ?isExit = false; // 取消退出
?? ??? ? ? ?}
?? ??? ? ? ?}, 2000); // 如果2秒鐘內(nèi)沒有按下返回鍵,則啟動定時器取消掉剛才執(zhí)行的任務(wù)
?? ??? ? }?
?? ??? ?else {
?? ??? ??? ?finish();
?? ??? ? ? ?System.exit(0);
?? ??? ?}
?? ??? ?
?? ?}
?
?? ?@Override
?? ?public boolean onCreateOptionsMenu(Menu menu) {
?? ??? ?// Inflate the menu; this adds items to the action bar if it is present.
?? ??? ?getMenuInflater().inflate(R.menu.activity_main, menu);
?? ??? ?return true;
?? ?}
}布局文件的代碼:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" > ? ? ? <ImageView ? ? ? ? android:id="@+id/imageView1" ? ? ? ? android:layout_width="fill_parent" ? ? ? ? android:layout_height="fill_parent" ? ? ? ? android:layout_alignParentLeft="true" ? ? ? ? android:src="@drawable/backguand_new"? ? ? ? ? android:scaleType="fitCenter"/> ? ? ? <Button ? ? ? ? android:id="@+id/button_start" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_alignParentBottom="true" ? ? ? ? android:layout_centerHorizontal="true" ? ? ? ? android:layout_marginBottom="12dp" ? ? ? ? android:text="開始" ? ? ? ? android:textColor="#ffffff"? ? ? ? ? android:background="@drawable/button_start_full"/> ? ? ? <Button ? ? ? ? android:id="@+id/button_stop" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_alignBaseline="@+id/button_start" ? ? ? ? android:layout_alignBottom="@+id/button_start" ? ? ? ? android:layout_marginLeft="29dp" ? ? ? ? android:layout_toRightOf="@+id/button_start" ? ? ? ? android:background="@drawable/button_stop_half" ? ? ? ? android:text="停止" ? ? ? ? android:textColor="#ffffff" /> ? ? ? <Button ? ? ? ? android:id="@+id/button_record" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_alignBaseline="@+id/button_start" ? ? ? ? android:layout_alignBottom="@+id/button_start" ? ? ? ? android:layout_marginRight="28dp" ? ? ? ? android:layout_toLeftOf="@+id/button_start" ? ? ? ? android:background="@drawable/button_record_half" ? ? ? ? android:text="記錄" ? ? ? ? android:textColor="#ffffff" /> ? ? ? <ImageView ? ? ? ? android:id="@+id/imageView2" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_above="@+id/button_start" ? ? ? ? android:layout_centerHorizontal="true" ? ? ? ? android:layout_marginBottom="20dp" ? ? ? ? android:src="@drawable/showrecord_new" /> ? ? ? <ScrollView ? ? ? ? android:id="@+id/scrollView1" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="340dp" ? ? ? ? android:layout_alignLeft="@+id/imageView2" ? ? ? ? android:layout_alignRight="@+id/imageView2" ? ? ? ? android:layout_alignTop="@+id/imageView2"? ? ? ? ? android:scrollbars="none"> ? ? ? ? ? <LinearLayout ? ? ? ? ? ? android:id="@+id/linearlayout1" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? android:orientation="vertical" > ? ? ? ? </LinearLayout> ? ? </ScrollView> ? ? ? <TextView ? ? ? ? android:id="@+id/textView2" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_alignBaseline="@+id/textView1" ? ? ? ? android:layout_alignBottom="@+id/textView1" ? ? ? ? android:layout_toRightOf="@+id/textView1" ? ? ? ? android:text="00"? ? ? ? ? android:textColor="#ffffff" ? ? ? ? android:textSize="40dp"/> ? ? ? <TextView ? ? ? ? android:id="@+id/textView1" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_above="@+id/imageView2" ? ? ? ? android:layout_marginBottom="5dp" ? ? ? ? android:layout_alignLeft="@+id/imageView2" ? ? ? ? android:text="00:00:00" ? ? ? ? android:textAppearance="?android:attr/textAppearanceLarge" ? ? ? ? android:textColor="#ffffff" ? ? ? ? android:textSize="60dp" /> ? </RelativeLayout>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
音量控制鍵控制的音頻流(setVolumeControlStream)描述
當(dāng)開發(fā)多媒體應(yīng)用或者游戲應(yīng)用的時候,需要使用音量控制鍵來設(shè)置程序的音量大小,在Android系統(tǒng)中有多種音頻流,感興趣的朋友可以了解下2013-01-01
Android學(xué)習(xí)教程之2D繪圖基礎(chǔ)及繪制太極圖
這篇文章主要給大家介紹了Android中2D繪圖基礎(chǔ)的相關(guān)資料,文中介紹了繪圖的基礎(chǔ)內(nèi)容,以及通過Canvas和Paint實現(xiàn)繪制太極圖的詳細過程,對各位Android新手開發(fā)者們具有一定的參考價值,需要的朋友下面來一起看看吧。2017-04-04
新版Android Studio3.6找不到R.java怎么處理
這篇文章主要介紹了新版Android Studio3.6找不到R.java怎么處理,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Android自定義網(wǎng)絡(luò)連接工具類HttpUtil
這篇文章主要介紹了Android自定義網(wǎng)絡(luò)連接工具類HttpUtil,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11
Android使用Intent發(fā)送短信的實現(xiàn)方法
這篇文章主要介紹了Android使用Intent發(fā)送短信的實現(xiàn)方法,結(jié)合簡單實例形式分析了Android短信發(fā)送功能的實現(xiàn)技巧,需要的朋友可以參考下2016-07-07

