android 计时器 Chronometer

android 计时器 Chronometer_第1张图片

 

 

计时器组件是TextView的子类,其主要功能还是显示文本操作。

 

 

在main.xml中

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical">

 

    <Chronometer

        android:id="@+id/myChronometer"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:layout_marginTop="8dp"

        android:gravity="center_horizontal"/>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

      android:layout_width="fill_parent"

      android:layout_height="fill_parent"

      android:orientation="horizontal"

      android:layout_marginTop="8dp"

      android:gravity="center_horizontal">

   

    <Button

        android:id="@+id/butStart"

        android:text="开始计时"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"/>

      <Button

          android:id="@+id/butStop"

          android:text="停止计时"

          android:layout_width="wrap_content"

          android:layout_height="wrap_content"/>

      <Button

          android:id="@+id/butBase"

          android:text="复位"

          android:layout_width="wrap_content"

          android:layout_height="wrap_content"/>

      <Button

          android:id="@+id/butFormat"

          android:text="格式化"

          android:layout_width="wrap_content"

          android:layout_height="wrap_content"/>

  </LinearLayout>

</LinearLayout>

 

 

 

在MyChronometerDemo.java程序中

package com.tarena.chronometer;

 

import android.app.Activity;

import android.os.Bundle;

import android.os.SystemClock;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Chronometer;

 

public class MyChronometerDemo extends Activity {

  private Chronometer myChronometer = null;

  private Button butStart = null;

  private Button butStop = null;

  private Button butBase = null;

  private Button butFormat = null;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        super.setContentView(R.layout.main);

        this.myChronometer = (Chronometer) super.findViewById(R.id.myChronometer);

        this.butStart = (Button) super.findViewById(R.id.butStart);

        this.butStop = (Button) super.findViewById(R.id.butStop);

        this.butBase = (Button) super.findViewById(R.id.butBase);

        this.butFormat = (Button) super.findViewById(R.id.butFormat);

        this.butStart.setOnClickListener(new OnClickListenerImplStart());

        this.butStop.setOnClickListener(new OnClickListenerImplStop());

        this.butBase.setOnClickListener(new OnClickListenerImplBase());

        this.butFormat.setOnClickListener(new OnClickListenerImplFormat());

       

    }

    private class OnClickListenerImplStart implements OnClickListener{

    public void onClick(View v){

        MyChronometerDemo.this.myChronometer.start();  //开始计时

    }

    }

    private class OnClickListenerImplStop implements OnClickListener{

    public void onClick(View v){

        MyChronometerDemo.this.myChronometer.stop(); //停止计时

    }

    }

 

    private class OnClickListenerImplBase implements OnClickListener{

    public void onClick(View v){

      MyChronometerDemo.this.myChronometer.setBase(SystemClock

            .elapsedRealtime());        //复位时间

    }

    }

 

    private class OnClickListenerImplFormat implements OnClickListener{

    public void onClick(View v){

        MyChronometerDemo.this.myChronometer.setFormat("新的显示格式:%s");  //格式化时间

    }

    }

 

}



你可能感兴趣的:(计时器,Chronometer)