安卓開發(fā)(二) 計(jì)時(shí)器

計(jì)時(shí)器

MainActivity

package com.example.clock;

import android.os.Handler;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

import java.util.Date;

public class MainActivityextends AppCompatActivityimplements Button.OnClickListener{

Buttonstart, stop, clear;

? ? TextViewshow;

? ? Datebgtime;

? ? long bg =0;

? ? boolean isFirst=true, isStop=false;

? ? Handlerhandler =new Handler();

? ? private Runnablerunnable =new Runnable() {

@Override

? ? ? ? public void run() {

if(!isStop){

show.setText(timeFormat(bg + (new Date().getTime()) -bgtime.getTime()));

? ? ? ? ? ? ? ? handler.post(this);

? ? ? ? ? ? }

}

};

? ? @Override

? ? protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

? ? ? ? setContentView(R.layout.activity_main);

? ? ? ? start = (Button) findViewById(R.id.start);

? ? ? ? clear = (Button) findViewById(R.id.clear);

? ? ? ? stop = (Button) findViewById(R.id.stop);

? ? ? ? start.setOnClickListener(this);

? ? ? ? stop.setOnClickListener(this);

? ? ? ? clear.setOnClickListener(this);

? ? ? ? show = (TextView) findViewById(R.id.show);

? ? }

@Override

? ? public void onClick(View view) {

switch (view.getId()){

case R.id.start:

if(isFirst){

isFirst =false;

? ? ? ? ? ? ? ? ? ? isStop =false;

? ? ? ? ? ? ? ? ? ? bgtime =new Date();

? ? ? ? ? ? ? ? ? ? new Thread(runnable).run();

? ? ? ? ? ? ? ? }

break;

? ? ? ? ? ? case R.id.stop:

bg += (new Date().getTime()) -bgtime.getTime();

? ? ? ? ? ? ? ? isStop =true;

? ? ? ? ? ? ? ? isFirst =true;

break;

? ? ? ? ? ? case R.id.clear:

show.setText("00:00:00.00");

? ? ? ? ? ? ? ? bg =0;

? ? ? ? ? ? ? ? isStop =true;

? ? ? ? ? ? ? ? isFirst =true;

break;

? ? ? ? }

}

public StringtimeFormat(long ms)

{

int sm = (int) (ms%1000)/10;

? ? ? ? ms = (int) ms/1000;

? ? ? ? int sec = (int) ms%60;

? ? ? ? int min = (int) ms/60;

? ? ? ? int h = (int) ms/3600;

? ? ? ? return String.format("%02d:%02d:%02d.%02d",h, min, sec, sm);

? ? }

}


layout.xml


? ? 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:rowCount="4"

? ? android:columnCount="3"

? ? tools:context="com.example.clock.MainActivity"

? ? >

? ? ? ? android:id="@+id/textView"

? ? ? ? android:layout_width="wrap_content"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:layout_column="1"

? ? ? ? android:layout_gravity="center_horizontal"

? ? ? ? android:layout_row="0"

? ? ? ? android:text="計(jì)時(shí)器"

? ? ? ? android:textSize="30dp"/>

? ? ? ? android:id="@+id/show"

? ? ? ? android:layout_columnSpan="3"

? ? ? ? android:text="00:00:00.00"

? ? ? ? android:textSize="40dp"

? ? ? ? android:layout_gravity="center_horizontal"/>

? ? ? ? android:id="@+id/clear"

? ? ? ? android:text="清零"

? ? ? ? android:layout_row="2"

? ? ? ? android:layout_column="0"/>

? ? ? ? android:id="@+id/start"

? ? ? ? android:text="計(jì)時(shí)"

? ? ? ? android:layout_row="2"

? ? ? ? android:layout_column="1"

? ? ? ? android:layout_gravity="center_horizontal"/>

? ? ? ? android:id="@+id/stop"

? ? ? ? android:text="停止"

? ? ? ? android:layout_row="2"

? ? ? ? android:layout_column="2"/>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 本人初學(xué)Android,最近做了一個(gè)實(shí)現(xiàn)安卓簡單音樂播放功能的播放器,收獲不少,于是便記錄下來自己的思路與知識(shí)總結(jié)...
    落日柳風(fēng)閱讀 19,454評(píng)論 2 41
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 7,335評(píng)論 0 17
  • 版權(quán)聲明:本文為Yiwent原創(chuàng)文章,轉(zhuǎn)載必須注明出處。 1.登陸界面 對(duì)于一個(gè)網(wǎng)絡(luò)APP來說,剛開始首先必須登錄...
    文采逸逸閱讀 1,595評(píng)論 6 13
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,058評(píng)論 25 709
  • 她問,如果春暖花開,柳絮會(huì)不會(huì)隨風(fēng)飄散。 她問,如果炎夏逝去,夏蟬會(huì)不會(huì)再不鳴叫。 她問,如果秋風(fēng)瑟瑟,楓葉會(huì)不會(huì)...
    爾之語閱讀 354評(píng)論 0 1

友情鏈接更多精彩內(nèi)容