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>

Android计时器实现

上一篇:[C程序设计语言]第四部分


下一篇:解决mysql 1062 主从错误