zoukankan      html  css  js  c++  java
  • Android计时器实现

    Wyy.java

    package com.test;

    import android.app.Activity;
    import android.app.Service;
    import android.os.Bundle;
    import android.os.Vibrator;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Chronometer;
    import android.widget.AutoCompleteTextView.Validator;
    import android.widget.Chronometer.OnChronometerTickListener;

    public class Wyy extends Activity{

    private Chronometer ch1 = null;
    private Button chStart = null;
    private Button chStop = null;
    private Vibrator vibrator = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.setContentView(R.layout.wyy);
    this.ch1 = (Chronometer) super.findViewById(R.id.ch1);
    this.vibrator = (Vibrator) super.getApplication().getSystemService(Service.VIBRATOR_SERVICE);
    this.chStart = (Button) super.findViewById(R.id.chStart);
    this.chStop = (Button) super.findViewById(R.id.chStop);
    ch1.setOnChronometerTickListener(new OnChronometerTickListener() {
    public void onChronometerTick(Chronometer chronometer) {
    // TODO Auto-generated method stub
    String time = chronometer.getText().toString().replaceAll("[^(\d{2}:\d{2})]", "");
    if("00:01".equals(time)){
    vibrator.vibrate(new long[]{1000,1000,2000,3000},0);
    }
    }
    });
    this.chStop.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    // TODO Auto-generated method stub
    ch1.stop();
    }
    });
    this.chStart.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    // TODO Auto-generated method stub
    ch1.start();
    }
    });
    this.ch1.setFormat("当前计时时间:%s。");
    //this.ch1.setOnChronometerTickListener(new OnChronometerTickListenerImpl() );

        }
    }

    wyy.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <Chronometer
    android:id="@+id/ch1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
    <LinearLayout
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:orientation="horizontal"
    >
    <Button
    android:text="开始计时"
    android:id="@+id/chStart"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    </Button>
    <Button
    android:text="停止计时"
    android:id="@+id/chStop"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    </Button>
    </LinearLayout>
    </LinearLayout>

  • 相关阅读:
    SQL通用数据类型
    SQL基础
    软件测试相关(1)
    C语言——判断
    C语言新手教程——计算
    并查集
    洛谷-P1551 亲戚
    洛谷-P1536 村村通
    洛谷-P1525 [NOIP2010 提高组] 关押罪犯
    洛谷-P2814 家谱
  • 原文地址:https://www.cnblogs.com/alhy/p/6932381.html
Copyright © 2011-2022 走看看