码迷,mamicode.com
首页 > 移动开发 > 详细

android之Chronometer

时间:2014-09-23 20:52:05      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   io   os   java   ar   for   

首先定义activity_main.xml文件

代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
	<Chronometer 
	    android:id="@+id/myChronometer"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"/>
	<LinearLayout 
	    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"/>
	</LinearLayout>
</LinearLayout>

  然后改写MainActivity类

代码如下:

package com.example.myclock;

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

public class MainActivity 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);
        setContentView(R.layout.activity_main);
        this.myChronometer = (Chronometer)super.findViewById(R.id.myChronometer);
        this.vibrator = (Vibrator)super.getApplication().getSystemService(Service.VIBRATOR_SERVICE);
        this.butStart = (Button)super.findViewById(R.id.butStart);
        this.butStop = (Button)super.findViewById(R.id.butStop);
        this.butStart.setOnClickListener(new OnClickListenerlmplStart());
        this.butStop.setOnClickListener(new OnClickListenerlmplStop());
        this.myChronometer.setFormat("当前计时时间:%s。");
        this.myChronometer.setOnChronometerTickListener(new OnChronometerTickListenerlmpl());
        
    }
    private class OnChronometerTickListenerlmpl implements OnChronometerTickListener{
    	public void onChronometerTick(Chronometer chronometer){
    		String time = chronometer.getText().toString().replaceAll("[^(\\d{2}:\\d{2})]", "");
    		if("01:00".equals(time)){
    			MainActivity.this.vibrator.vibrate(new long[]{1000,10,1000,100},0);
    		}		
    	}
    }
    private class OnClickListenerlmplStart implements OnClickListener{
    	public void onClick(View view){
    		MainActivity.this.myChronometer.start();
    	}
    }
    private class OnClickListenerlmplStop implements OnClickListener{
    	public void onClick(View view){
    		MainActivity.this.myChronometer.stop();
    		MainActivity.this.myChronometer.setBase(SystemClock.elapsedRealtime());
    		MainActivity.this.vibrator.cancel();
    	}
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

  最后改写AndroidMainfest.xml文件

代码如下:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myclock"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
	<uses-permission 
	    android:name="android.permission.VIBRATE"/>
</manifest>

  运行效果如下图:

bubuko.com,布布扣

android之Chronometer

标签:android   style   blog   http   io   os   java   ar   for   

原文地址:http://www.cnblogs.com/lxk2010012997/p/3989094.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!