android計時器Chronometer類的使用

主要用到的幾個:

chronometer.start();開始計時

chronometer.stop();停止計時

chronometer.setBase(SystemClock.elapsedRealtime());設置初始時間

chronometer.setFormat("Formatted time (%s)");設置時間顯示格式

chronometer.setFormat(null);使用非格式顯示字符

下面是我寫的一個簡單的計時器,有三個按鍵,分別是:開始,停止,重置;

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) {
		// mStartButton.setEnabled(!isRun);
		// mStopButton.setEnabled(isRun);
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.startButton:
			Log.d("zhangpeihang", "" + v.getId());
			Toast.makeText(MainActivity.this, "start", Toast.LENGTH_SHORT);
			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.setBase(SystemClock.elapsedRealtime());
			// 開始記時
			chronometer.start();
			isRun = true;
		}
		// isRun = !isRun;
	}

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

	public void myRestart() {

		// 設置開始講時時間
		// if(!isRun){
		chronometer.setBase(SystemClock.elapsedRealtime());
		// 開始記時
		chronometer.start();
		isRun = true;
		// }
		// isRun = !isRun;

	}

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

}

下面是佈局文件:

<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" >

    <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" />

    <AnalogClock
        android:id="@+id/analogClock1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/digitalClock1"
        android:layout_marginTop="27dp" />

    <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>

當然了,我在佈局裏面還加上了DigitalClock(數字時鐘),AnalogClock(模擬時鐘)

佈局文件是鼠標託的,可能不夠好就湊合着看吧

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章