标签: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>
运行效果如下图:
标签:android style blog http io os java ar for
原文地址:http://www.cnblogs.com/lxk2010012997/p/3989094.html