<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
本文範例為大家分享了Android實現APP秒錶功能的具體程式碼,供大家參考,具體內容如下
這幾天一直在看安卓,也正好趕上老師佈置的作業,所以就做了一個秒錶。自己參考了一下別人的圖示,有了一些靈感所以順便也設計了一下介面。下面先貼一下秒錶的介面:
開啟秒錶後的第一個介面
點選開始計時,開始鍵變為暫停,記錄和停止開始變實:
點選記錄:
記錄滿了之後自動上移,通過滑動可以檢視前面的:
點選暫停:
停止:
重新開始和記錄:
雙擊返回鍵退出:
下面貼出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; //更新顯示時間的關鍵 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");//根據路徑得到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("開始"); //監聽開始按鈕 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("繼續"); 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); } } }); //監聽停止按鈕 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"); } } }); //監聽記錄按鈕 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");//根據路徑得到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; } } //把毫秒轉為要顯示的格式 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; } //監聽返回鍵,實現點選返回鍵時彈出對話,連續兩次點選退出 @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("確認", 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; // 準備退出 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秒鐘內沒有按下返回鍵,則啟動定時器取消掉剛才執行的任務 } 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>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45