本文實例講述了Android開發實現的計時器功能。分享給大家供大家參考,具體如下:
效果圖:

布局:
三個按鈕 加上一個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="繼續"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
實現:
四個監聽事件 三個按鈕 一個計時器
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);
//獲取計時器組件
ch = (Chronometer) findViewById(R.id.test);
//獲取開始按鈕
start = (Button) findViewById(R.id.start) ;
//暫停計時按鈕
pause = (Button) findViewById(R.id.pause);
//繼續計時按鈕
restart = (Button) findViewById(R.id.go_on);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//設置開始計時時間
ch.setBase(SystemClock.elapsedRealtime() );
//啟動計時器
ch.start();
pause.setEnabled(true);
restart.setEnabled(false);
start.setEnabled(false);
}
});
//暫停按鈕監聽器
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
start.setText("重新開始");
ch.stop();
start.setEnabled(true);
restart.setEnabled(true);
pause.setEnabled(false);
}
});
//暫停按鈕監聽器
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綁定事件監聽器
ch.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
//如果計時到現在超過了一小時秒
if ( SystemClock.elapsedRealtime() - ch.getBase() > 3600 * 1000) {
ch.stop();
start.setEnabled(true);
restart.setEnabled(false);
pause.setEnabled(false);
}
}
});
}
}
PS:這里再為大家推薦幾款相關的在線工具供大家參考:
在線秒表工具:
http://tools.jb51.net/bianmin/miaobiao
Unix時間戳(timestamp)轉換工具:
http://tools.jb51.net/code/unixtime
更多關于Android相關內容感興趣的讀者可查看本站專題:《Android日期與時間操作技巧總結》、《Android開發入門與進階教程》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。