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(模拟时钟)
布局文件是鼠标托的,可能不够好就凑合着看吧