Android實(shí)現(xiàn)簡(jiǎn)易秒表功能
本文實(shí)例為大家分享了Android實(shí)現(xiàn)秒表功能的具體代碼,供大家參考,具體內(nèi)容如下
今天為了給師弟們講安卓,花了10分鐘寫了一個(gè)簡(jiǎn)易的秒表app,現(xiàn)貼出代碼,供各位剛?cè)腴T以及還未入門的同學(xué)們參考
第一步:布局activity_main.xml:
<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" ? ? android:paddingBottom="@dimen/activity_vertical_margin" ? ? android:paddingLeft="@dimen/activity_horizontal_margin" ? ? android:paddingRight="@dimen/activity_horizontal_margin" ? ? android:paddingTop="@dimen/activity_vertical_margin" ? ? tools:context=".MainActivity" > ? ? ? <RelativeLayout ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_centerInParent="true" > ? ? ? ? ? <LinearLayout ? ? ? ? ? ? android:id="@+id/top" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:layout_centerHorizontal="true" ? ? ? ? ? ? android:orientation="horizontal" > ? ? ? ? ? ? ? <TextView ? ? ? ? ? ? ? ? android:id="@+id/mint" ? ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:text="00" ? ? ? ? ? ? ? ? android:textSize="30dp" /> ? ? ? ? ? ? ? <TextView ? ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:text=":" ? ? ? ? ? ? ? ? android:textSize="30dp" /> ? ? ? ? ? ? ? <TextView ? ? ? ? ? ? ? ? android:id="@+id/sec" ? ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:text="00" ? ? ? ? ? ? ? ? android:textSize="30dp" /> ? ? ? ? </LinearLayout> ? ? ? ? ? <LinearLayout ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:layout_below="@+id/top" ? ? ? ? ? ? android:layout_centerHorizontal="true" ? ? ? ? ? ? android:orientation="horizontal" > ? ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/start" ? ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:text="start" /> ? ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/reset" ? ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:text="reset" /> ? ? ? ? </LinearLayout> ? ? </RelativeLayout> ? </RelativeLayout>
第二步:實(shí)現(xiàn)秒表功能
package com.example.second; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { ? ? private TextView mint; ? ? private TextView sec; ? ? private Button start; ? ? private Button reset; ? ? private long timeusedinsec; ? ? private boolean isstop = false; ? ? private Handler mHandler = new Handler() { ? ? ? ? /* ? ? ? ? ?* edit by yuanjingchao 2014-08-04 19:10 ? ? ? ? ?*/ ? ? ? ? @Override ? ? ? ? public void handleMessage(Message msg) { ? ? ? ? ? ? // TODO Auto-generated method stub ? ? ? ? ? ? super.handleMessage(msg); ? ? ? ? ? ? switch (msg.what) { ? ? ? ? ? ? case 1: ? ? ? ? ? ? ? ? // 添加更新ui的代碼 ? ? ? ? ? ? ? ? if (!isstop) { ? ? ? ? ? ? ? ? ? ? updateView(); ? ? ? ? ? ? ? ? ? ? mHandler.sendEmptyMessageDelayed(1, 1000); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? case 0: ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? } ? ? ? ? } ? ? ? }; ? ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? initViews(); ? ? } ? ? private void initViews() { ? ? ? ? mint = (TextView) findViewById(R.id.mint); ? ? ? ? sec = (TextView) findViewById(R.id.sec); ? ? ? ? reset = (Button) findViewById(R.id.reset); ? ? ? ? start = (Button) findViewById(R.id.start); ? ? ? ? reset.setOnClickListener(new OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View arg0) { ? ? ? ? ? ? ? ? // TODO Auto-generated method stub ? ? ? ? ? ?? ? ? ? ? ? ? ? ? mint.setText("00"); ? ? ? ? ? ? ? ? sec.setText("00"); ? ? ? ? ? ? ? ? start.setText("start"); ? ? ? ? ? ? ? ? timeusedinsec=0; ? ? ? ? ? ? ? ? isstop=true; ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? start.setOnClickListener(new OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View arg0) { ? ? ? ? ? ? ? ? // TODO Auto-generated method stub ? ? ? ? ? ? ? ? mHandler.removeMessages(1); ? ? ? ? ? ? ? ? String aaa=start.getText().toString(); ? ? ? ? ? ? ? ? if(aaa.equals("start")){ ? ? ? ? ? ? ? ? ? ? mHandler.sendEmptyMessage(1); ? ? ? ? ? ? ? ? ? ? isstop = false; ? ? ? ? ? ? ? ? ? ? start.setText("pause"); ? ? ? ? ? ? ? ? }else { ? ? ? ? ? ? ? ? ? ? mHandler.sendEmptyMessage(0); ? ? ? ? ? ? ? ? ? ? isstop = true; ? ? ? ? ? ? ? ? ? ? start.setText("start"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ?? ? ? ? ? ? ? } ? ? ? ? }); ? ? } ? ? private void updateView() { ? ? ? ? timeusedinsec += 1; ? ? ? ? int minute = (int) (timeusedinsec / 60)%60; ? ? ? ? int second = (int) (timeusedinsec % 60); ? ? ? ? if (minute < 10) ? ? ? ? ? ? mint.setText("0" + minute); ? ? ? ? else ? ? ? ? ? ? mint.setText("" + minute); ? ? ? ? if (second < 10) ? ? ? ? ? ? sec.setText("0" + second); ? ? ? ? else ? ? ? ? ? ? sec.setText("" + second); ? ? } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android開(kāi)發(fā)環(huán)境搭建詳解(eclipse + android sdk)
這篇文章主要介紹了android開(kāi)發(fā)環(huán)境搭建詳解(eclipse + android sdk),需要的朋友可以參考下2014-05-05android實(shí)現(xiàn)支付寶咻一咻的幾種思路方法
本篇文章主要介紹了android實(shí)現(xiàn)支付寶咻一咻的幾種思路方法,詳解的介紹了幾種實(shí)現(xiàn)咻一咻的思路和方法,有需要的可以了解一下。2016-11-11Android開(kāi)發(fā)中Activity創(chuàng)建跳轉(zhuǎn)及傳值的方法
這篇文章主要介紹了Android開(kāi)發(fā)中Activity創(chuàng)建跳轉(zhuǎn)及傳值的方法的相關(guān)資料,需要的朋友可以參考下2016-05-05Android利用GridView實(shí)現(xiàn)單選效果
本篇文章主要介紹了Android利用GridView實(shí)現(xiàn)單選效果的相關(guān)知識(shí),具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-05-05Android 圖文詳解Binder進(jìn)程通信底層原理
Android系統(tǒng)中,多進(jìn)程間的通信都是依賴于底層Binder IPC機(jī)制,Binder機(jī)制是一種RPC方案。例如:當(dāng)進(jìn)程A中的Activity與進(jìn)程B中的Service通信時(shí),就使用了binder機(jī)制2021-10-10Android獲取設(shè)備CPU核數(shù)、時(shí)鐘頻率以及內(nèi)存大小的方法
這篇文章主要介紹了Android獲取設(shè)備CPU核數(shù)、時(shí)鐘頻率以及內(nèi)存大小的方法,涉及Android針對(duì)系統(tǒng)硬件相關(guān)操作技巧,需要的朋友可以參考下2016-07-07Android recyclerview實(shí)現(xiàn)縱向虛線時(shí)間軸的示例代碼
本文主要介紹了Android recyclerview實(shí)現(xiàn)縱向虛線時(shí)間軸的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07Android開(kāi)發(fā)之HTTP訪問(wèn)網(wǎng)絡(luò)
這篇文章主要介紹了Android開(kāi)發(fā)之HTTP訪問(wèn)網(wǎng)絡(luò)的相關(guān)資料,需要的朋友可以參考下2016-07-07