Android详细教程(基础篇):十九、View组件高级篇:Chronometer 计时器组件详解

7.14. Chronometer(计时器组件)

继承结构:

java.lang.Object

   

android.view.View

 

   

android.widget.TextView

 

 

   

android.widget.Chronometer

 

 

 

Android详细教程(基础篇):十九、View组件高级篇:Chronometer 计时器组件详解_第1张图片

 

Android详细教程(基础篇):十九、View组件高级篇:Chronometer 计时器组件详解_第2张图片

配置:

"1.0" encoding="utf-8"?>

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

     android:orientation="vertical"

     android:layout_width="fill_parent"

     android:layout_height="fill_parent">

    

          android:id="@+id/myChronometer"

          android:layout_width="wrap_content"

          android:layout_height="wrap_content"/>

    

          android:orientation="horizontal"

          android:layout_width="fill_parent"

          android:layout_height="fill_parent">

          <Button

               android:id="@+id/butStart"

               android:text="开始计时"

               android:textSize="12px"

               android:layout_width="wrap_content"

               android:layout_height="wrap_content"/>

          <Button

               android:id="@+id/butStop"

               android:text="停止计时"

               android:textSize="12px"

               android:layout_width="wrap_content"

               android:layout_height="wrap_content"/>

          <Button

               android:id="@+id/butBase"

               android:text="复位"

               android:textSize="12px"

               android:layout_width="wrap_content"

               android:layout_height="wrap_content"/>

          <Button

               android:id="@+id/butFormat"

               android:text="格式化显示"

               android:textSize="12px" 

               android:layout_width="wrap_content"

               android:layout_height="wrap_content"/>

    

 

Activity:

package com.makyan.demo;

 

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 ChronometerActivity extends Activity {

     private Chronometer chronometer = null;

     private Button buttonStart = null;

     private Button buttonStop = null;

     private Button buttonBase = null;

     private Button buttonFormat = null;

 

     @Override

     protected void onCreate(Bundle savedInstanceState) {

          super.onCreate(savedInstanceState);

          setContentView(R.layout.activity_chronometer);

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

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

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

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

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

          buttonStart.setOnClickListener(new OnClickListenerStart());

          buttonStop.setOnClickListener(new OnClickListenerStop());

          buttonBase.setOnClickListener(new OnClickListenerBase());

          buttonFormat.setOnClickListener(new OnClickListenerFormat());

     }

     //开始计时

     private class OnClickListenerStart implements OnClickListener{

 

          @Override

          public void onClick(View v) {

               chronometer.start();

          }

     }

     //停止计时

     private class OnClickListenerStop implements OnClickListener{

 

          @Override

          public void onClick(View v) {

               chronometer.stop();

          }

     }

     //还原计时

     private class OnClickListenerBase implements OnClickListener{

 

          @Override

          public void onClick(View v) {

               chronometer.setBase(SystemClock.elapsedRealtime());

          }

     }

     //修改时间显示格式

     private class OnClickListenerFormat implements OnClickListener{

 

          @Override

          public void onClick(View v) {

               chronometer.setFormat("新的显示格式:%s。");// 格式化显示

          }

     }

}

 

Android详细教程(基础篇):十九、View组件高级篇:Chronometer 计时器组件详解_第3张图片

 

手机震动:Vibrator

通过Vibrator对象的vibrate方法设置震动,Vibrator对象可以通过Service.VIBRATOR_SERVICE来设置服务

Vibrator    vibrator = (Vibrator) super.getApplication().getSystemService(Service.VIBRATOR_SERVICE);                                   // 取得震动服务

 

 

Android详细教程(基础篇):十九、View组件高级篇:Chronometer 计时器组件详解_第4张图片

设置震动属于系统上的东西,所以必须要给予权限:

配置:

"1.0" encoding="utf-8"?>

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

     android:orientation="vertical"

     android:layout_width="fill_parent"

     android:layout_height="fill_parent">

    

          android:id="@+id/myChronometer"

          android:layout_width="wrap_content"

          android:layout_height="wrap_content"/>

    

          android:orientation="horizontal"

          android:layout_width="fill_parent"

          android:layout_height="fill_parent">

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

    

 

Activity:

package com.makyan.demo;

import android.app.Activity;

import android.app.Service;

import android.os.Bundle;

import android.os.SystemClock;

import android.os.Vibrator;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Chronometer;

import android.widget.Chronometer.OnChronometerTickListener;

public class VibratorActivity extends Activity {

     private Chronometer myChronometer = null;                             // 计时组件

     private Button butStart = null;                                            // 按钮组件

     private Button butStop = null;                                        // 按钮组件

     private Vibrator vibrator = null ;                                    // 设置震动

 

     @Override

     public void onCreate(Bundle savedInstanceState) {

          super.onCreate(savedInstanceState);

          super.setContentView(R.layout.activity_vibrator);                           // 调用默认布局管理器

          myChronometer = (Chronometer) super

                    .findViewById(R.id.myChronometer);                     // 取得组件

          vibrator = (Vibrator) super.getApplication().getSystemService(

                    Service.VIBRATOR_SERVICE);                                  // 取得震动服务

          butStart = (Button) super.findViewById(R.id.butStart);  // 取得组件

          butStop = (Button) super.findViewById(R.id.butStop);    // 取得组件

          butStart.setOnClickListener(new OnClickListenerImplStart()) ;     // 设置监听

          butStop.setOnClickListener(new OnClickListenerImplStop()) ;  // 设置监听

          myChronometer.setFormat("当前计时时间:%s。");           // 格式化文本

          myChronometer.setOnChronometerTickListener(new OnChronometerTickListenerImpl());                // 设置监听

     }

     private class OnChronometerTickListenerImpl implements OnChronometerTickListener {

          @Override

          public void onChronometerTick(Chronometer chronometer) {

               // 取出时间

               String time = chronometer.getText().toString().replaceAll("[^(\\d{2}:\\d{2})]", "");               

               // 满一分钟

               if ("01:00".equals(time)) {                                

                    // 设置震动周期,以及循环震动

                    vibrator.vibrate(new long[] { 1000, 10,1000, 100 }, 0);                    

               }

          }

     }

     private class OnClickListenerImplStart implements OnClickListener {

 

          @Override

          public void onClick(View view) {

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

          }

     }

     private class OnClickListenerImplStop implements OnClickListener {

 

          @Override

          public void onClick(View view) {

               myChronometer.stop() ;             // 结束计时

               myChronometer.setBase(SystemClock.elapsedRealtime());                                 // 复位

               vibrator.cancel() ;                // 取消震动

          }

     }

}

 

Android详细教程(基础篇):十九、View组件高级篇:Chronometer 计时器组件详解_第5张图片

你可能感兴趣的:(Android详解,Android移动开发详解)