Android開發(fā)實(shí)現(xiàn)的計(jì)時(shí)器功能示例
本文實(shí)例講述了Android開發(fā)實(shí)現(xiàn)的計(jì)時(shí)器功能。分享給大家供大家參考,具體如下:
效果圖:

布局:
三個(gè)按鈕 加上一個(gè)Chronometer
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:gravity="center_horizontal">
<Chronometer
android:id="@+id/test"
android:textSize="25pt"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="開始"
android:layout_weight="1"/>
<Button
android:id="@+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暫停"
android:layout_weight="1"/>
<Button
android:id="@+id/go_on"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="繼續(xù)"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
實(shí)現(xiàn):
四個(gè)監(jiān)聽事件 三個(gè)按鈕 一個(gè)計(jì)時(shí)器
package com.example.a30797.androidtest;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
public class MainActivity extends AppCompatActivity {
Chronometer ch ;
Button start ;
Button pause ;
Button restart ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲取計(jì)時(shí)器組件
ch = (Chronometer) findViewById(R.id.test);
//獲取開始按鈕
start = (Button) findViewById(R.id.start) ;
//暫停計(jì)時(shí)按鈕
pause = (Button) findViewById(R.id.pause);
//繼續(xù)計(jì)時(shí)按鈕
restart = (Button) findViewById(R.id.go_on);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//設(shè)置開始計(jì)時(shí)時(shí)間
ch.setBase(SystemClock.elapsedRealtime() );
//啟動(dòng)計(jì)時(shí)器
ch.start();
pause.setEnabled(true);
restart.setEnabled(false);
start.setEnabled(false);
}
});
//暫停按鈕監(jiān)聽器
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
start.setText("重新開始");
ch.stop();
start.setEnabled(true);
restart.setEnabled(true);
pause.setEnabled(false);
}
});
//暫停按鈕監(jiān)聽器
restart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
start.setText("重新開始");
ch.start();
start.setEnabled(true);
pause.setEnabled(true);
restart.setEnabled(false);
}
});
//為Chronomter綁定事件監(jiān)聽器
ch.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
//如果計(jì)時(shí)到現(xiàn)在超過了一小時(shí)秒
if ( SystemClock.elapsedRealtime() - ch.getBase() > 3600 * 1000) {
ch.stop();
start.setEnabled(true);
restart.setEnabled(false);
pause.setEnabled(false);
}
}
});
}
}
PS:這里再為大家推薦幾款相關(guān)的在線工具供大家參考:
在線秒表工具:
http://tools.jb51.net/bianmin/miaobiao
Unix時(shí)間戳(timestamp)轉(zhuǎn)換工具:
http://tools.jb51.net/code/unixtime
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android日期與時(shí)間操作技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android系統(tǒng)聯(lián)系人全特效實(shí)現(xiàn)(上)分組導(dǎo)航和擠壓動(dòng)畫(附源碼)
本文將為大家講解下Android系統(tǒng)聯(lián)系人全特效實(shí)現(xiàn)之分組導(dǎo)航和擠壓動(dòng)畫,具體實(shí)現(xiàn)及源代碼如下,感興趣的朋友可以參考下哈,希望對(duì)大家學(xué)習(xí)有所幫助2013-06-06
android實(shí)現(xiàn)人臉識(shí)別技術(shù)的示例代碼
本篇文章主要介紹了android人臉識(shí)別技術(shù)的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
Android onKeyDown監(jiān)聽返回鍵無效的解決辦法
這篇文章主要介紹了 Android onKeyDown監(jiān)聽返回鍵無效的解決辦法的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android Kotlin的使用及簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android Kotlin的使用及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-05-05
詳解Android開啟OTG功能/USB?Host?API功能
這篇文章主要介紹了Android開啟OTG功能/USB?Host?API功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07
android RecyclerView實(shí)現(xiàn)條目Item拖拽排序與滑動(dòng)刪除
本篇文章主要介紹了android RecyclerView實(shí)現(xiàn)條目Item拖拽排序與滑動(dòng)刪除,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03
android app判斷是否有系統(tǒng)簽名步驟詳解
這篇文章主要為大家介紹了android app判斷是否有系統(tǒng)簽名步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
基于android示例程序(bitmapfun) 高效加載圖片讓人無語地方
嘗試了使用git上的一個(gè)開源項(xiàng)目afinal(bitmapfun的封裝版)來加載圖片,但是在測(cè)試的時(shí)候發(fā)現(xiàn)了一個(gè)問題,新的圖片加載器(bitmapfun)比之前用的ImageDownloader要慢很多,特別是在網(wǎng)絡(luò)狀況不好的時(shí)候,那簡(jiǎn)直是太讓人無語了2013-04-04

