Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 計時器 Chronometer 的使用方法

編輯:關於 Android 程式設計

Chronometer 是 Android 提供的計時元件,常用的幾個方法如下:

  • chronometer.start():開始計時
  • chronometer.stop():停止計時
  • chronometer.setBase(SystemClock.elapsedRealtime()):設定起始時間
  • chronometer.setFormat("Formatted time (%s)"):設定時間顯示格式
  • chronometer.setFormat(null):使用非格式化方式顯示

下面是一個包含「開始、停止、重置」三個按鈕的簡單範例。

一、Activity 程式碼

package com.example.xxxxxxxxxxxxxxxxxx;

import android.os.Bundle;
import android.os.SystemClock;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements Button.OnClickListener {
    private Chronometer chronometer;
    private Button mStartButton, mStopButton, mRestartButton;
    private TextView mtextView;
    boolean isRun = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        chronometer = (Chronometer) findViewById(R.id.chronometer1);
        mStartButton = (Button) findViewById(R.id.startButton);
        mStopButton = (Button) findViewById(R.id.stopbutton2);
        mRestartButton = (Button) findViewById(R.id.restartbutton3);
        mStartButton.setOnClickListener(this);
        mStopButton.setOnClickListener(this);
        mRestartButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.startButton:
                Log.d("zhangpeihang", "" + v.getId());
                Toast.makeText(MainActivity.this, "start", Toast.LENGTH_SHORT).show();
                myStart();
                break;
            case R.id.stopbutton2:
                myStop();
                break;
            case R.id.restartbutton3:
                myRestart();
                break;
            default:
                break;
        }
    }

    public void myStart() {
        System.out.println("--開始計時---");
        if (!isRun) {
            chronometer.start();
            isRun = true;
        }
    }

    public void myStop() {
        if (isRun) {
            chronometer.stop();
            isRun = false;
            mStartButton.setText("繼續");
        }
    }

    public void myRestart() {
        chronometer.setBase(SystemClock.elapsedRealtime());
        chronometer.start();
        isRun = true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

二、佈局檔

除了 Chronometer,還額外放進了 DigitalClock 與 AnalogClock 做示範(佈局是用拖曳方式排的,僅供參考):

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <DigitalClock
        android:id="@+id/digitalClock1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="17dp"
        android:layout_marginTop="20dp"
        android:text="DigitalClock" />
    <Chronometer
        android:id="@+id/chronometer1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/digitalClock1"
        android:layout_marginLeft="76dp"
        android:layout_toRightOf="@+id/digitalClock1"
        android:text="Chronometer" />
    <Button
        android:id="@+id/startButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/chronometer1"
        android:layout_alignTop="@+id/analogClock1"
        android:text="開始" />
    <Button
        android:id="@+id/stopbutton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/analogClock1"
        android:layout_alignLeft="@+id/startButton"
        android:layout_marginBottom="27dp"
        android:text="停止" />
    <Button
        android:id="@+id/restartbutton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/stopbutton2"
        android:layout_toRightOf="@+id/analogClock1"
        android:text="重置" />
</RelativeLayout>

透過 isRun 這個旗標即可控制計時的開始與暫停,而「重置」則是先重設基準時間再重新開始,是計時器應用中很典型的操作組合。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved